Improved async functions

This commit is contained in:
Bofh 2021-02-07 20:13:47 +01:00
parent 44b6295017
commit 3cae3a8f37
2 changed files with 12 additions and 5 deletions

View File

@ -55,11 +55,14 @@ def add_igaccount(acc_id):
pixelfed_setpic(acc_id, data['graphql']['user']['profile_pic_url'])
pixelfed_setinfo(acc_id, data['graphql']['user']['biography'],\
data['graphql']['user']['external_url'])
threading.Thread(target=pixelfed_dlposts, args=(acc_id, data['graphql']['user'])).start()
update_igaccount_async(acc_id)
else:
print('W| User "{}" already exists in local database'.format(acc_id))
return 0
def update_igaccount_async(acc_id):
threading.Thread(target=update_igaccount, args=(acc_id)).start()
def update_igaccount(acc_id):
# if account does not exist, we stop the mirroring process
accfile = './db/accounts/{}'.format(acc_id)
@ -69,14 +72,18 @@ def update_igaccount(acc_id):
# do it on a thread because it might take long to download the latest posts
data = getig_user_data(acc_id)
threading.Thread(target=pixelfed_dlposts, args=(acc_id, data['graphql']['user'])).start()
pixelfed_dlposts(acc_id, data['graphql']['user'])
def update_allaccounts_async():
threading.Thread(target=update_allaccounts).start()
def update_allaccounts():
# update all accounts with a timeout of 20 seconds
for acc_id in os.listdir('./db/accounts'):
print('I| mirroring account "{}"...'.format(acc_id))
update_igaccount(acc_id)
time.sleep(30)
print('I| timeout 20 seconds')
time.sleep(20)
print()

View File

@ -17,7 +17,7 @@ class MyServer(BaseHTTPRequestHandler):
# a wilcard select all accounts
if parts[0] == '*':
if parts[1].lower() == 'update':
igmirror.update_allaccounts()
igmirror.update_allaccounts_async()
else:
# make sure account name contains only safe characters
# i think IG usernames can only have this characters:
@ -25,7 +25,7 @@ class MyServer(BaseHTTPRequestHandler):
if parts[1].lower() == 'add':
igmirror.add_igaccount(accname)
elif parts[1].lower() == 'update':
igmirror.update_igaccount(accname)
igmirror.update_igaccount_async(accname)
self.wfile.write(bytes('{"status": "ok"}', "utf-8"))
else:
self.wfile.write(bytes('{"status": "parameters are not correct"}', "utf-8"))