Skip to content

Commit

Permalink
Merge pull request #31 from twoscomplement/twoscomplement-multiple-im…
Browse files Browse the repository at this point in the history
…ages

Output all attached images
  • Loading branch information
timhutton authored Nov 13, 2022
2 parents 68e720a + 53b8ff3 commit 91c9452
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,34 @@ def tweet_json_to_markdown(tweet, username, archive_media_folder, output_media_f
if 'url' in url and 'expanded_url' in url:
body = body.replace(url['url'], url['expanded_url'])
# replace image URLs with markdown image links to local files
if 'entities' in tweet and 'media' in tweet['entities']:
for media in tweet['entities']['media']:
if 'entities' in tweet and 'media' in tweet['entities'] and 'extended_entities' in tweet and 'media' in tweet['extended_entities']:
original_url = tweet['entities']['media'][0]['url']
markdown = ''
for media in tweet['extended_entities']['media']:
if 'url' in media and 'media_url' in media:
original_url = media['url']
original_expanded_url = media['media_url']
original_filename = os.path.split(original_expanded_url)[1]
local_filename = os.path.join(archive_media_folder, tweet_id_str + '-' + original_filename)
new_url = output_media_folder_name + tweet_id_str + '-' + original_filename
markdown += '' if not markdown and body == original_url else '\n\n'
if os.path.isfile(local_filename):
# Found a matching image, use this one
if not os.path.isfile(new_url):
shutil.copy(local_filename, new_url)
markdown = f'![]({new_url})'
markdown += f'![]({new_url})'
else:
# Is there any other file that includes the tweet_id in its filename?
media_filenames = glob.glob(os.path.join(archive_media_folder, tweet_id_str + '*'))
if len(media_filenames) > 0:
markdown = ''
for media_filename in media_filenames:
media_url = f'{output_media_folder_name}{os.path.split(media_filename)[-1]}'
if not os.path.isfile(media_url):
shutil.copy(media_filename, media_url)
markdown += f'\n\n<video controls><source src="{media_url}">Your browser does not support the video tag.</video>\n{media_url}'
markdown += f'<video controls><source src="{media_url}">Your browser does not support the video tag.</video>\n{media_url}'
else:
print(f'Warning: missing local file: {local_filename}. Using original link instead: {original_url} (expands to {original_expanded_url})')
markdown = f'![]({original_url})'
body = body.replace(original_url, markdown)
markdown += f'![]({original_url})'
body = body.replace(original_url, markdown)
# append the original Twitter URL as a link
body += f'\n\n(Originally on Twitter: [{timestamp_str}](https://twitter.com/{username}/status/{tweet_id_str}))'
return timestamp, body
Expand Down

0 comments on commit 91c9452

Please sign in to comment.