Compare commits

..

No commits in common. "e6ec0b20d76491230c8118972e2d7931ea23edbd" and "f54ab85f1857a729f5aba28604e722b01e5f861c" have entirely different histories.

View File

@ -27,11 +27,11 @@ def unredir(redir):
r.headers.get('Location') r.headers.get('Location')
else: else:
redir = r.headers.get('Location') redir = r.headers.get('Location')
# print('redir', redir) print('redir', redir)
if '//ow.ly/' in redir or '//bit.ly/' in redir: if '//ow.ly/' in redir or '//bit.ly/' in redir:
redir = redir.replace('https://ow.ly/', 'http://ow.ly/') # only http redir = redir.replace('https://ow.ly/', 'http://ow.ly/') # only http
redir = requests.get(redir, allow_redirects=False).headers.get('Location') redir = requests.get(redir, allow_redirects=False).headers.get('Location')
# print('redir+', redir) print('redir+', redir)
try: try:
r = requests.get(redir, allow_redirects=False, timeout=5) r = requests.get(redir, allow_redirects=False, timeout=5)
except: except:
@ -223,31 +223,20 @@ else:
c = html.unescape(t['tweet']) c = html.unescape(t['tweet'])
# do not toot twitter replies # do not toot twitter replies
if 'reply_to' in t and len(t['reply_to'])>0: if 'reply_to' in t and len(t['reply_to'])>0:
# print('Reply:',c) print('Reply:',c)
continue continue
# do not toot twitter quoted RT # do not toot twitter quoted RT
if 'quote_url' in t and t['quote_url'] != '': if 'quote_url' in t and t['quote_url'] != '':
# print('Quoted:', c) print('Quoted:', c)
continue continue
# detect threads
in_reply_to = None
if 'conversation_id' in t and t['conversation_id'] not in t['link']:
db.execute('SELECT toot FROM tweets WHERE tweet = ? AND twitter = ?', (t['conversation_id'], source)) # noqa
thread = db.fetchone()
if thread:
print("Thread :", t['conversation_id'], t['link'], thread[0])
in_reply_to = thread[0]
# check if this tweet has been processed # check if this tweet has been processed
id = t['id'] # old id id = t['id']
db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (id, source, mastodon, instance)) # noqa db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (id, source, mastodon, instance)) # noqa
if db.fetchone(): last = db.fetchone()
continue
id = t['link'].split('/')[-1] # new id from status link to support threads # process only unprocessed tweets
db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (id, source, mastodon, instance)) # noqa if last:
if db.fetchone():
continue continue
if c[-1] == "": if c[-1] == "":
@ -265,21 +254,21 @@ else:
if 'photos' in t: if 'photos' in t:
for url in t['photos']: for url in t['photos']:
# print('photo', url) print('photo', url)
try: try:
media = requests.get(url.replace( media = requests.get(url.replace(
'https://pbs.twimg.com/', 'https://nitter.net/pic/orig/')) 'https://pbs.twimg.com/', 'https://nitter.net/pic/orig/'))
# print("received nitter", media.headers.get('content-type')) print("received nitter", media.headers.get('content-type'))
media_posted = mastodon_api.media_post( media_posted = mastodon_api.media_post(
media.content, mime_type=media.headers.get('content-type')) media.content, mime_type=media.headers.get('content-type'))
# print("posted") print("posted")
toot_media.append(media_posted['id']) toot_media.append(media_posted['id'])
except: except:
media = requests.get(url) media = requests.get(url)
# print("received twitter", media.headers.get('content-type')) print("received twitter", media.headers.get('content-type'))
media_posted = mastodon_api.media_post( media_posted = mastodon_api.media_post(
media.content, mime_type=media.headers.get('content-type')) media.content, mime_type=media.headers.get('content-type'))
# print("posted") print("posted")
toot_media.append(media_posted['id']) toot_media.append(media_posted['id'])
@ -297,30 +286,23 @@ else:
if m is None: if m is None:
c = c.replace(l, redir) c = c.replace(l, redir)
else: else:
print('lien:',l)
c = c.replace(l, '')
video = redir video = redir
# print('video:', video) print('video:', video)
video_json = subprocess.run('yt-dlp -s -j %s' % subprocess.run('rm -f out.mp4; yt-dlp -N 8 -o out.mp4 --recode-video mp4 --no-playlist %s --max-filesize 100M' %
(video,), shell=True, capture_output=True) (video,), shell=True, capture_output=False)
video_info = json.loads(video_json.stdout) print("received")
if video_info['duration'] < 600: try:
# print('lien:', l) file = open("out.mp4", "rb")
c = c.replace(l, '') video_data = file.read()
subprocess.run('rm -f out.*; yt-dlp -N 8 -o out.mp4 --recode-video mp4 --no-playlist --max-filesize 100M %s' % file.close()
(video,), shell=True, capture_output=False) media_posted = mastodon_api.media_post(video_data, mime_type='video/mp4')
# print("received") c = c.replace(video, '')
try: print("posted")
file = open("out.mp4", "rb") toot_media.append(media_posted['id'])
video_data = file.read() except:
file.close() pass
media_posted = mastodon_api.media_post(video_data, mime_type='video/mp4')
c = c.replace(video, '')
# print("posted")
toot_media.append(media_posted['id'])
os.remove("out.mp4")
except:
pass
else:
print("video duration > 600s : ", video_info['duration'])
# remove pic.twitter.com links # remove pic.twitter.com links
m = re.search(r"pic.twitter.com[^ \xa0]*", c) m = re.search(r"pic.twitter.com[^ \xa0]*", c)
@ -346,16 +328,16 @@ else:
if len(toot_media)>0: if len(toot_media)>0:
time.sleep(5) time.sleep(5)
toot = mastodon_api.status_post(c, toot = mastodon_api.status_post(c,
in_reply_to_id=in_reply_to, in_reply_to_id=None,
media_ids=toot_media, media_ids=toot_media,
sensitive=False, sensitive=False,
visibility='unlisted', visibility='unlisted',
spoiler_text=None) spoiler_text=None)
except: except:
print("delay") print("10s delay")
time.sleep(30) time.sleep(10)
toot = mastodon_api.status_post(c, toot = mastodon_api.status_post(c,
in_reply_to_id=in_reply_to, in_reply_to_id=None,
media_ids=toot_media, media_ids=toot_media,
sensitive=False, sensitive=False,
visibility='unlisted', visibility='unlisted',