migrate sqlite global database to local account directory

This commit is contained in:
cquest 2022-12-14 16:48:54 +01:00
parent af8ff73fd1
commit cb6267ef86

View File

@ -3,6 +3,7 @@ import sys
import re import re
import html import html
import time import time
import shutil
import sqlite3 import sqlite3
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -17,12 +18,6 @@ if len(sys.argv) < 4:
print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance [max_days [footer_tags [delay]]]") # noqa print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance [max_days [footer_tags [delay]]]") # noqa
sys.exit(1) sys.exit(1)
# sqlite db to store processed tweets (and corresponding toots ids)
sql = sqlite3.connect('tootbot.db')
db = sql.cursor()
db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text,
twitter text, mastodon text, instance text)''')
if len(sys.argv) > 4: if len(sys.argv) > 4:
instance = sys.argv[4] instance = sys.argv[4]
else: else:
@ -43,11 +38,28 @@ if len(sys.argv) > 7:
else: else:
delay = 0 delay = 0
source = sys.argv[1] source = sys.argv[1]
mastodon = sys.argv[2] mastodon = sys.argv[2]
passwd = sys.argv[3] passwd = sys.argv[3]
if 'http' not in source:
# switch to local account directory
try:
os.mkdir(source)
except:
pass
os.chdir(source)
# copy (old) global sqlite database to local account directory
if not os.path.exists('tootbot.db'):
shutil.copy('../tootbot.db', 'tootbot.db')
sql = sqlite3.connect('tootbot.db')
db = sql.cursor()
db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text,
twitter text, mastodon text, instance text)''')
# Create application if it does not exist # Create application if it does not exist
if not os.path.isfile(instance+'.secret'): if not os.path.isfile(instance+'.secret'):
if Mastodon.create_app( if Mastodon.create_app(
@ -171,11 +183,11 @@ if source[:4] == 'http':
sql.commit() sql.commit()
else: else:
try: # cleanup local database after migration from the global one
os.mkdir(source) db.execute("DELETE FROM tweets WHERE twitter != ?", (source,))
except: sql.commit()
pass db.execute("VACUUM")
os.chdir(source)
subprocess.run('rm -f tweets.*json; twint -u %s -tl --limit 10 --json -o tweets.sjson; jq -s . tweets.sjson > tweets.json' % subprocess.run('rm -f tweets.*json; twint -u %s -tl --limit 10 --json -o tweets.sjson; jq -s . tweets.sjson > tweets.json' %
(source,), shell=True, capture_output=True) (source,), shell=True, capture_output=True)
d = json.load(open('tweets.json','r')) d = json.load(open('tweets.json','r'))