-
-
Save zph/c83c3e91ee3f9df21686bb50b4fbf904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import os | |
import re | |
import logging | |
import cloudconvert | |
import requests | |
usage = "python {} tweet_link_with_gif [outputfilename]".format(sys.argv[0]) | |
logging.basicConfig(level=0) | |
logging.getLogger('requests').setLevel(logging.ERROR) | |
logger = logging.getLogger(__name__) | |
if len(sys.argv) < 2: | |
print(usage) | |
sys.exit(1) | |
twitter_page = sys.argv[1] | |
output = sys.argv[2] | |
logger.info("Checking Twitter Page: {}".format(twitter_page)) | |
home = os.getenv("HOME") | |
req = requests.get(twitter_page) | |
link_matcher = re.compile("https://pbs.twimg.com/tweet_video_thumb/(.*).jpg") | |
matches = link_matcher.search(req.content) | |
img_id = matches.group(1) | |
with open("{}/.config/cloudconvert".format(home)) as f: | |
token = f.read().rstrip() | |
api = cloudconvert.Api(token) | |
link = "https://video.twimg.com/tweet_video/{}.mp4".format(img_id) | |
logger.info("Found MP4 Link: {}".format(link)) | |
process = api.convert({ | |
"inputformat": "mp4", | |
"outputformat": "gif", | |
"input": "download", | |
"file": link | |
}) | |
process.wait() # wait until conversion finished | |
filename = link.split("/")[-1].split(".")[0] | |
if output == "": | |
output = filename + ".gif" | |
process.download("{}".format(output)) | |
logger.info("Saved gif to {}".format(output)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment