Changed caption system to comments + rate limiting issues (fixed i think)

This commit is contained in:
Bofh 2021-02-08 15:02:28 +01:00
parent 228267524a
commit 01d4a8539f
1 changed files with 42 additions and 15 deletions

View File

@ -157,15 +157,13 @@ def pixelfed_dlposts(acc_id, data):
print('I| skipping IG post {}:{}. Already added'.format(acc_id, item['shortcode']))
continue
print('I| processing IG post {}:{}'.format(acc_id, item['shortcode']))
print('I| >>>> {}:{}'.format(acc_id, item['shortcode']))
ig_url = 'https://www.instagram.com/p/{}/'.format(item['shortcode'])
title = item['title'] if 'title' in item else None
caption = item['edge_media_to_caption']['edges'][0]['node']['text'] \
if len(item['edge_media_to_caption']['edges']) > 0 else ''
altcaption = item['accessibility_caption'] if 'accessibility_caption' in item else None
full_caption = caption
full_altcaption = altcaption
# add support for posts with multiple images
# get the data from the post URL. (we need all images, as IG can have not only 1 image in the post)
@ -183,6 +181,7 @@ def pixelfed_dlposts(acc_id, data):
failed = False
# we add support to multiple media here
print('I| uploading media for {}:{}... '.format(acc_id, item['shortcode']), end='')
media2iterate = [a['node']['display_url'] for a in multmedia] if multiple else [item['display_url']]
for media in media2iterate:
_token, jsdata = pixelfed_postimage(acc_id, media, accdata)
@ -193,9 +192,9 @@ def pixelfed_dlposts(acc_id, data):
jsdata_items.append(jsdata)
if failed:
continue
print('done')
# add the accesibility captions
caption = caption[0:136]+'...' if len(caption) > 140 else caption
i = 0
for jsdata in jsdata_items:
jsdata['description'] = ig_url
@ -210,22 +209,50 @@ def pixelfed_dlposts(acc_id, data):
jsdata['alt'] = altcaption[0:136]+'...' if len(altcaption) > 140 else altcaption
i += 1
_headers = {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': _token,
'X-Requested-With': 'XMLHttpRequest',
'X-XSRF-TOKEN': accdata['cookie']['XSRF-TOKEN']
}
# publish the post using Pixelfed API
# the caption will be the original instagram URL
print('I| publishing post for {}:{}... '.format(acc_id, item['shortcode']), end='')
r = requests.post('https://'+config()['instance']+'/api/compose/v0/publish',\
json={"media": jsdata_items, "caption": caption, "visibility": "public", "cw": False,\
json={"media": jsdata_items, "caption": ig_url, "visibility": "public", "cw": False,\
"comments_disabled": False, "place": False, "tagged": [],"optimize_media": True},\
cookies=accdata['cookie'],\
headers={
'Content-Type': 'application/json',
'X-CSRF-TOKEN': _token,
'X-Requested-With': 'XMLHttpRequest',
'X-XSRF-TOKEN': accdata['cookie']['XSRF-TOKEN']
}
cookies=accdata['cookie'], headers=_headers
)
accposts.append(item['shortcode'])
print('I| uploaded media for {}:{} : {}'.format(acc_id, item['shortcode'], r.status_code))
print('I| done uploading media for {}'.format(acc_id))
# do a comment as it supports larger descriptions
if len(r.text) > 5:
ps = r.text.strip('/').split('/')
status_id = ps[len(ps)-1]
print('done | StatusID -> {}'.format(status_id))
print('I| publishing comments containing caption for {}:{}... '.format(acc_id, item['shortcode']), end='')
i = 1
failed = False
for comment in [caption[i:i+495] for i in range(0, len(caption), 495)]:
r2 = requests.post('https://'+config()['instance']+'/i/comment',\
json={'comment': '('+str(i)+') '+comment, 'item': status_id, 'sensitive': False},\
cookies=accdata['cookie'], headers=_headers
)
if not r2.status_code == 200:
failed = True
print('err. CODE -> {}'.format(r2.status_code))
print(r2.text)
break
i += 1
if not failed:
print('done')
accposts.append(item['shortcode'])
print('I| uploaded post {}:{} : OK'.format(acc_id, item['shortcode']))
# to avoid Pixelfed rate-limiting
time.sleep(10)
db_set('posts', acc_id, accposts)
# upload media and return data