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