Skip to content

Instantly share code, notes, and snippets.

@agscala
Created January 30, 2013 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agscala/4677188 to your computer and use it in GitHub Desktop.
Save agscala/4677188 to your computer and use it in GitHub Desktop.
python vine2gif.py [vine_url] [output_file] Scrapes vine url, downloads video, and converts the file to a gif with mplayer
from pyquery import PyQuery as pq
import sys
import subprocess
import os
from urllib2 import urlopen, URLError, HTTPError
def convert(input_file, output_gif):
print "CONVERT", input_file, " => ", output_gif
subprocess.call(["mplayer", input_file, "-ao", "null", "-ss", "0:0:0", "-endpos", "6", "-vo", "gif89a:fps=8:output=%s" % output_gif, "-vf", "scale=240:180"])
def dlfile(url):
# Open the url
try:
f = urlopen(url)
print "downloading " + url
# Open our local file for writing
output_file = os.path.basename(url)
with open(output_file, "wb") as local_file:
local_file.write(f.read())
return output_file
#handle errors
except HTTPError, e:
print "HTTP Error:", e.code, url
except URLError, e:
print "URL Error:", e.reason, url
if __name__ == "__main__":
try:
url = sys.argv[1]
output = sys.argv[2]
except IndexError:
sys.exit(1)
print "Creating [%s] from [%s]" % (url, output)
d = pq(url=url)
video_url = d("video#post source").attr("src")
video_url = video_url.partition("?")[0]
filename = dlfile(video_url)
convert(filename, output)
@ZacSweers
Copy link

Line 42 doesn't work anymore, it should be this instead:

video_url = d("meta[property=twitter\\:player\\:stream]").attr['content']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment