Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output all attached images #31

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,32 @@ 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 'extended_entities' in tweet and 'media' in tweet['extended_entities']:
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.startswith(original_url) else '\n\n'
twoscomplement marked this conversation as resolved.
Show resolved Hide resolved
if os.path.isfile(local_filename):
# Found a matching image, use this one
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]}'
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)
twoscomplement marked this conversation as resolved.
Show resolved Hide resolved
# 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