Create a gist now

Instantly share code, notes, and snippets.

@zph /twitter-gif Secret
Created Mar 8, 2017

#!/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