User fullname was not being updated on updates + code improvements

This commit is contained in:
Bofh 2021-02-11 01:09:45 +01:00
parent 5d6f4fc2df
commit df7537b80e
1 changed files with 22 additions and 8 deletions

View File

@ -12,14 +12,12 @@ import re
CONFIG = {}
def add_igaccount(acc_id):
accfile = './db/accounts/{}'.format(acc_id)
# user_create script must exist before running the API server
if not os.path.exists('./scripts/user_create'):
print('E| You may need to initialize the server environment first')
return 1
if not os.path.exists(accfile):
if not account_exists(acc_id):
# get all profile data from instagram acc
data = getig_user_data(acc_id)
@ -35,8 +33,7 @@ def add_igaccount(acc_id):
return 3
# get account display name to create it
name = data['graphql']['user']['full_name']
name = re.sub(r'[^a-zA-Z0-9_\s]', '', name)
name = getig_user_fullname(data)
account = {
'name': name,
'username': acc_id,
@ -58,23 +55,31 @@ def add_igaccount(acc_id):
update_igaccount_async(acc_id, False)
else:
print('W| User "{}" already exists in local database'.format(acc_id))
return 0
def update_igaccount_async(acc_id, profileset=True):
threading.Thread(target=update_igaccount, args=(acc_id, profileset,)).start()
def update_igaccount(acc_id, profileset=True):
# if account does not exist, we stop the mirroring process
accfile = './db/accounts/{}'.format(acc_id)
if not os.path.exists(accfile):
# if account does not exist, we stop the update process
if not account_exists(acc_id):
print('E| User "'+acc_id+'" has not been created yet, maybe you wanted to call /<username>/add ?')
return 1
data = getig_user_data(acc_id)
if profileset:
# update the fullname of the user on local DB
accdata = db_get('accounts', acc_id)
accdata['name'] = getig_user_fullname(data)
db_set('accounts', acc_id, accdata)
# set the information from IG to the Pixelfed Account info
pixelfed_setpic(acc_id, data['graphql']['user']['profile_pic_url'])
pixelfed_setinfo(acc_id, data['graphql']['user']['biography'],\
data['graphql']['user']['external_url'])
# sincronize posts (images/videos/stories...)
pixelfed_dlposts(acc_id, data['graphql']['user'])
def update_allaccounts_async():
@ -502,6 +507,12 @@ def getig_user_data(acc_id):
instagram_get('/{}/?__a=1'.format(acc_id), 1800)
)
def getig_user_fullname(data):
if data is None:
return '.ERROR.'
return re.sub(r'[^a-zA-Z0-9_\s]', '',\
data['graphql']['user']['full_name'])
# runs a basic GET request emulating Tor Browser
def instagram_get(url, CACHE_SECS=600):
headers = get_random_headers()
@ -548,6 +559,9 @@ def get_random_headers():
headers[reg.group(1).strip()] = reg.group(2).strip()
return headers
def account_exists(acc_id):
return os.path.exists('./db/accounts/{}'.format(acc_id))
def db_set(table, acc_id, accdata):
w = open('./db/{}/{}'.format(table, acc_id), 'w')
w.write(json.dumps(accdata))