1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 08:46:15 +00:00

Merge commit 'e75aa21' into stable

This commit is contained in:
Fabian Schlenz 2018-04-04 21:11:58 +02:00
commit e3708c00ad
3 changed files with 46 additions and 36 deletions

View File

@ -40,6 +40,9 @@ git commit -m "Bumping the version to $VERSION" Dockerfile
echo "Tagging the new version..." echo "Tagging the new version..."
git tag -a "$VERSION" -m "Version $VERSION" || error git tag -a "$VERSION" -m "Version $VERSION" || error
echo "Building it..."
gradle build || error "Build failed. What did you do?!"
echo "Checking out stable..." echo "Checking out stable..."
git checkout stable || error git checkout stable || error
@ -52,18 +55,15 @@ git push --all || error
echo "Pushing tags to Github..." echo "Pushing tags to Github..."
git push --tags || error git push --tags || error
echo "Building it..."
gradle build || error "Build failed. What did you do?!"
echo "Generating a release on Github..." echo "Generating a release on Github..."
json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', draft: true, body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github" json=$(ruby -e "require 'json'; puts({tag_name: '$VERSION', name: '$VERSION', body: \$stdin.read}.to_json)" <<< "$release_notes") || error "Couldn't generate JSON for Github"
json=$(curl $CURL_OPTS https://api.github.com/repos/fabianonline/telegram_backup/releases -XPOST -d "$json") || error "Github failure" json=$(curl $CURL_OPTS https://api.github.com/repos/fabianonline/telegram_backup/releases -XPOST -d "$json") || error "Github failure"
echo "Uploading telegram_backup.jar to Github..." echo "Uploading telegram_backup.jar to Github..."
upload_url=$(jq -r ".upload_url" <<< "$json") || error "Could not parse JSON from Github" upload_url=$(jq -r ".upload_url" <<< "$json") || error "Could not parse JSON from Github"
upload_url=$(sed 's/{.*}//' <<< "$upload_url") upload_url=$(sed 's/{.*}//' <<< "$upload_url")
release_url=$(jq -r ".url" <<< "$json") || error "Could not parse JSON from Github" release_url=$(jq -r ".html_url" <<< "$json") || error "Could not parse JSON from Github"
curl $CURL_OPTS --header "Content-Type: application/zip" "${upload_url}?name=telegram_backup.jar" --upload-file build/libs/telegram_backup.jar || error "Asset upload to github failed" curl $CURL_OPTS --header "Content-Type: application/zip" "${upload_url}?name=telegram_backup.jar" --upload-file build/libs/telegram_backup.jar || error "Asset upload to github failed"
echo "Building the docker image..." echo "Building the docker image..."
@ -73,12 +73,15 @@ echo "Pushing the docker image..."
docker push fabianonline/telegram_backup docker push fabianonline/telegram_backup
echo "Notifying the Telegram group..." echo "Notifying the Telegram group..."
release_notes=$(sed 's/\* /• /' <<< "$release_notes") release_notes=$(sed 's/\* /• /' | sed 's/&/&amp;/g' | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g' <<< "$release_notes")
message="Version $VERSION released"$'\n'$'\n'"$release_notes"$'\n'$'\n'"$release_url" message="<b>Version $VERSION was just released</b>"$'\n'$'\n'"$release_notes"$'\n'$'\n'"$release_url"
curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<-" --form-string "chat_id=${CHAT_ID}" <<< "$message" curl https://api.telegram.org/bot${BOT_TOKEN}/sendMessage -XPOST --form "text=<-" --form-string "chat_id=${CHAT_ID}" --form-string "parse_mode=HTML" --form-string "disable_web_page_preview=true" <<< "$message"
echo "Cleaning release_notes.txt..." echo "Cleaning release_notes.txt..."
> release_notes.txt > release_notes.txt
echo "Checking out master..."
git checkout master
echo "Done." echo "Done."

View File

@ -56,7 +56,9 @@ class Database private constructor(var client: TelegramClient) {
try { try {
val rs = stmt!!.executeQuery("SELECT MAX(message_id) FROM messages WHERE source_type IN ('group', 'dialog')") val rs = stmt!!.executeQuery("SELECT MAX(message_id) FROM messages WHERE source_type IN ('group', 'dialog')")
rs.next() rs.next()
return rs.getInt(1) val result = rs.getInt(1)
rs.close()
return result
} catch (e: SQLException) { } catch (e: SQLException) {
return 0 return 0
} }
@ -87,6 +89,7 @@ class Database private constructor(var client: TelegramClient) {
missing.add(i) missing.add(i)
} }
} }
rs.close()
return missing return missing
} catch (e: SQLException) { } catch (e: SQLException) {
e.printStackTrace() e.printStackTrace()
@ -111,16 +114,7 @@ class Database private constructor(var client: TelegramClient) {
} }
fun getMessagesFromUserCount(): Int { fun getMessagesFromUserCount() = queryInt("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId())
try {
val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.user!!.getId())
rs.next()
return rs.getInt(1)
} catch (e: SQLException) {
throw RuntimeException(e)
}
}
fun getMessageTypesWithCount(): HashMap<String, Int> = getMessageTypesWithCount(GlobalChat()) fun getMessageTypesWithCount(): HashMap<String, Int> = getMessageTypesWithCount(GlobalChat())
@ -164,7 +158,9 @@ class Database private constructor(var client: TelegramClient) {
try { try {
val rs = stmt!!.executeQuery("PRAGMA encoding") val rs = stmt!!.executeQuery("PRAGMA encoding")
rs.next() rs.next()
return rs.getString(1) val result = rs.getString(1)
rs.close()
return result
} catch (e: SQLException) { } catch (e: SQLException) {
logger.debug("SQLException: {}", e) logger.debug("SQLException: {}", e)
return "unknown" return "unknown"
@ -255,9 +251,7 @@ class Database private constructor(var client: TelegramClient) {
} }
fun getTopMessageIDForChannel(id: Int): Int { fun getTopMessageIDForChannel(id: Int): Int = queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')")
return queryInt("SELECT MAX(message_id) FROM messages WHERE source_id=$id AND source_type IN('channel', 'supergroup')")
}
fun logRun(start_id: Int, end_id: Int, count: Int) { fun logRun(start_id: Int, end_id: Int, count: Int) {
try { try {
@ -269,6 +263,7 @@ class Database private constructor(var client: TelegramClient) {
ps.setInt(2, end_id) ps.setInt(2, end_id)
ps.setInt(3, count) ps.setInt(3, count)
ps.execute() ps.execute()
ps.close()
} catch (e: SQLException) { } catch (e: SQLException) {
} }
@ -278,7 +273,9 @@ class Database private constructor(var client: TelegramClient) {
try { try {
val rs = stmt!!.executeQuery(query) val rs = stmt!!.executeQuery(query)
rs.next() rs.next()
return rs.getInt(1) val result = rs.getInt(1)
rs.close()
return result
} catch (e: SQLException) { } catch (e: SQLException) {
throw RuntimeException("Could not get count of messages.") throw RuntimeException("Could not get count of messages.")
} }
@ -431,6 +428,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_ignore.clearBatch() ps_insert_or_ignore.clearBatch()
conn!!.commit() conn!!.commit()
conn!!.setAutoCommit(true) conn!!.setAutoCommit(true)
ps.close()
ps_insert_or_ignore.close()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
throw RuntimeException("Exception shown above happened.") throw RuntimeException("Exception shown above happened.")
@ -486,6 +486,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_replace.clearBatch() ps_insert_or_replace.clearBatch()
conn!!.commit() conn!!.commit()
conn!!.setAutoCommit(true) conn!!.setAutoCommit(true)
ps_insert_or_ignore.close()
ps_insert_or_replace.close()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
throw RuntimeException("Exception shown above happened.") throw RuntimeException("Exception shown above happened.")
@ -535,6 +538,9 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_replace.clearBatch() ps_insert_or_replace.clearBatch()
conn!!.commit() conn!!.commit()
conn!!.setAutoCommit(true) conn!!.setAutoCommit(true)
ps_insert_or_ignore.close()
ps_insert_or_replace.close()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
throw RuntimeException("Exception shown above happened.") throw RuntimeException("Exception shown above happened.")
@ -564,6 +570,7 @@ class Database private constructor(var client: TelegramClient) {
while (rs.next()) { while (rs.next()) {
map.put("count.messages.type." + rs.getString(1), rs.getInt(2)) map.put("count.messages.type." + rs.getString(1), rs.getInt(2))
} }
rs.close()
return map return map
} catch (e: Exception) { } catch (e: Exception) {
throw RuntimeException(e) throw RuntimeException(e)
@ -586,6 +593,7 @@ class Database private constructor(var client: TelegramClient) {
map.put("count.messages.media_type.$s", rs.getInt(2)) map.put("count.messages.media_type.$s", rs.getInt(2))
} }
map.put("count.messages.media_type.any", count) map.put("count.messages.media_type.any", count)
rs.close()
return map return map
} catch (e: Exception) { } catch (e: Exception) {
throw RuntimeException(e) throw RuntimeException(e)
@ -625,6 +633,7 @@ class Database private constructor(var client: TelegramClient) {
} }
map.put("authors.count.others", count_others) map.put("authors.count.others", count_others)
map.put("authors.all", all_data) map.put("authors.all", all_data)
rs.close()
return map return map
} catch (e: Exception) { } catch (e: Exception) {
throw RuntimeException(e) throw RuntimeException(e)
@ -634,7 +643,9 @@ class Database private constructor(var client: TelegramClient) {
fun getMessageCountForExport(c: AbstractChat): Int { fun getMessageCountForExport(c: AbstractChat): Int {
val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE " + c.query); val rs = stmt!!.executeQuery("SELECT COUNT(*) FROM messages WHERE " + c.query);
rs.next() rs.next()
return rs.getInt(1) val result = rs.getInt(1)
rs.close()
return result
} }
fun getMessageTimesMatrix(c: AbstractChat): Array<IntArray> { fun getMessageTimesMatrix(c: AbstractChat): Array<IntArray> {
@ -647,6 +658,7 @@ class Database private constructor(var client: TelegramClient) {
while (rs.next()) { while (rs.next()) {
result[if (rs.getInt(1) == 0) 6 else rs.getInt(1) - 1][rs.getInt(2)] = rs.getInt(3) result[if (rs.getInt(1) == 0) 6 else rs.getInt(1) - 1][rs.getInt(2)] = rs.getInt(3)
} }
rs.close()
return result return result
} catch (e: Exception) { } catch (e: Exception) {
throw RuntimeException(e) throw RuntimeException(e)

View File

@ -37,25 +37,19 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
fun doUpdates() { fun doUpdates() {
try { try {
val stmt = conn.createStatement() val stmt = conn.createStatement()
var rs: ResultSet
logger.debug("DatabaseUpdate.doUpdates running") logger.debug("DatabaseUpdate.doUpdates running")
logger.debug("Getting current database version") logger.debug("Getting current database version")
var version: Int var version: Int
logger.debug("Checking if table database_versions exists") logger.debug("Checking if table database_versions exists")
rs = stmt.executeQuery("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'") val table_count = db.queryInt("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='database_versions'")
rs.next() if (table_count == 0) {
if (rs.getInt(1) == 0) {
logger.debug("Table does not exist") logger.debug("Table does not exist")
version = 0 version = 0
} else { } else {
logger.debug("Table exists. Checking max version") logger.debug("Table exists. Checking max version")
rs.close() version = db.queryInt("SELECT MAX(version) FROM database_versions")
rs = stmt.executeQuery("SELECT MAX(version) FROM database_versions")
rs.next()
version = rs.getInt(1)
} }
rs.close()
logger.debug("version: {}", version) logger.debug("version: {}", version)
System.out.println("Database version: " + version) System.out.println("Database version: " + version)
logger.debug("Max available database version is {}", maxPossibleVersion) logger.debug("Max available database version is {}", maxPossibleVersion)
@ -106,6 +100,9 @@ class DatabaseUpdates(protected var conn: Connection, protected var db: Database
} catch (e: SQLException) { } catch (e: SQLException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
println("Cleaning up the database (this might take some time)...")
try { stmt.executeUpdate("VACUUM") } catch (t: Throwable) { logger.debug("Exception during VACUUMing: {}", t) }
} else { } else {
logger.debug("No update necessary.") logger.debug("No update necessary.")
@ -441,7 +438,5 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn
} }
println() println()
logger.info("Converted ${i} of ${count} messages.") logger.info("Converted ${i} of ${count} messages.")
println(" Cleaning up the database (this might also take some time, sorry)...")
execute("VACUUM")
} }
} }