Skip to content

Instantly share code, notes, and snippets.

@saga
Created December 2, 2012 01:23
Show Gist options
  • Save saga/4186392 to your computer and use it in GitHub Desktop.
Save saga/4186392 to your computer and use it in GitHub Desktop.
appfog youtube python
#!/usr/bin/env python
import simplejson
import logging
import random
import re
import datetime
import string
import time
import sys
import urllib
import xmlrpclib
import base64
import os
import json
from flask import Flask, Request, Response
from werkzeug.routing import BaseConverter
import urllib2
from urllib import quote, unquote, urlencode
import argparse
from os import path
from urlparse import parse_qs
from urllib2 import URLError
class VideoInfo(object):
def __init__(self, video_url):
request_url = 'http://www.youtube.com/get_video_info?video_id='
if 'http://www.youtube.com/watch?v' in parse_qs(video_url).keys():
request_url += parse_qs(video_url)['http://www.youtube.com/watch?v'][0]
elif 'v' in parse_qs(video_url).keys():
request_url += parse_qs(video_url)['v'][0]
request = urllib2.Request(request_url)
try:
self.video_info = parse_qs(urllib2.urlopen(request).read())
except URLError :
pass
def video_file_urls(videoinfo):
url_encoded_fmt_stream_map = videoinfo.video_info['url_encoded_fmt_stream_map'][0].split(',')
entrys = [parse_qs(entry) for entry in url_encoded_fmt_stream_map]
url_maps = [dict(url=entry['url'][0]+'&signature='+entry['sig'][0], type=entry['type']) for entry in entrys]
return url_maps
def GetYoutubeUrlMp4_1(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/mp4':
url = entry['url']
break
return url
def GetYoutubeUrlMp4_2(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/mp4':
url = entry['url']
index = index + 1
if index >= 2:
break
return url
def GetYoutubeUrlMp4_3(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/mp4':
url = entry['url']
index = index + 1
if index >= 3:
break
return url
def GetYoutubeUrlMp4_4(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/mp4':
url = entry['url']
index = index + 1
if index >= 4:
break
return url
def GetYoutubeUrlMp4_Last(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/mp4':
url = entry['url']
return url
def GetYoutubeUrl2(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/webm':
url = entry['url']
break
return url
def GetYoutubeUrl2_2(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/webm':
url = entry['url']
index = index + 1
if index >= 2:
break
return url
def GetYoutubeUrl2_3(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/webm':
url = entry['url']
index = index + 1
if index >= 3:
break
return url
def GetYoutubeUrl2_4(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
index = 0
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/webm':
url = entry['url']
index = index + 1
if index >= 4:
break
return url
def GetYoutubeUrl3(url_str):
video_info = VideoInfo(url_str)
video_url_map = video_file_urls(video_info)
url = ''
for entry in video_url_map:
entry_type = entry['type'][0]
entry_type = entry_type.split(';')[0]
if entry_type.lower() == 'video/x-flv':
url = entry['url']
break
return url
application = app = Flask('wsgi')
@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp
@app.route('/aaa2')
def welcome():
return GetYoutubeUrl1("http://www.youtube.com/watch?v=DXFEYNH1pLc")
class RegexConverter(BaseConverter):
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
app.url_map.converters['regex'] = RegexConverter
@app.route('/webm1/<regex(".*"):filename>')
def FetchWebm1(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrl2("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/webm")
@app.route('/webm2/<regex(".*"):filename>')
def FetchWebm2(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrl2_2("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/webm")
@app.route('/webm3/<regex(".*"):filename>')
def FetchWebm3(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrl2_3("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/webm")
@app.route('/webm4/<regex(".*"):filename>')
def FetchWebm4(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrl2_4("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/webm")
@app.route('/atestaaafads/<regex("[abcABC0-9]{4,6}"):uid>-<slug>/')
def example1(uid, slug):
return "uid: %s, slug: %s" % (uid, slug)
@app.route('/m4v/<regex(".*"):filename>')
def FetchM4V(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="video/m4v")
@app.route('/mp4/<regex(".*"):filename>')
def FetchMP4(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="video/mp4")
@app.route('/mp3/<regex(".*"):filename>')
def FetchMP3(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="audio/mp3")
@app.route('/htm/<regex(".*"):filename>')
def FetchHTM(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="text/html")
@app.route('/jpg/<regex(".*"):filename>')
def FetchJPGMain(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="image/jpeg")
@app.route('/img/<regex(".*"):filename>')
def FetchImageImg(filename):
urlnew = base64.urlsafe_b64decode(filename)
response1 = urllib2.urlopen(urlnew)
content = response1.read()
return Response(content, content_type="image/jpeg")
@app.route('/jpg1/<regex(".*"):filename>')
def FetchJPG1(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrlMp4_1("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/mp4")
@app.route('/jpg2/<regex(".*"):filename>')
def FetchJPG2(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrlMp4_2("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/mp4")
@app.route('/jpg3/<regex(".*"):filename>')
def FetchJPG3(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrlMp4_3("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/mp4")
@app.route('/jpg4/<regex(".*"):filename>')
def FetchJPG4(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrlMp4_4("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/mp4")
@app.route('/jpg5/<regex(".*"):filename>')
def FetchJPG5(filename):
import StringIO
output = StringIO.StringIO()
content1 = GetYoutubeUrlMp4_Last("http://www.youtube.com/watch?v=" + filename)
response1 = urllib2.urlopen(content1, timeout=80)
buff_size = 64000000
while True:
buffer = response1.read(buff_size)
if not buffer:
break
output.write(buffer)
contents = output.getvalue()
return Response(contents, content_type="video/mp4")
@app.route('/pod/<regex(".*"):filename>')
def Podfeed(filename):
response1 = urllib2.urlopen(filename)
content = response1.read()
return Response(content, content_type="text/xml")
@app.route('/nbcnightlynews')
def RawFetchMSNBCNightly():
thisUrl = "http://podcastfeeds.nbcnews.com/audio/podcast/MSNBC-Nightly.xml"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/topratedvideo')
def RawFetchTOPRATEDVIDEO():
thisUrl = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/googletechtalks')
def RawFetchFeedGoogleTechTalks():
thisUrl = "http://gdata.youtube.com/feeds/api/users/GoogleTechTalks/uploads"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/Confreaks')
def RawFetchFeedConfreaks():
thisUrl = "http://gdata.youtube.com/feeds/api/users/Confreaks/uploads"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/MarakanaTechTV')
def RawFetchFeedMarakanaTechTV():
thisUrl = "http://gdata.youtube.com/feeds/api/users/MarakanaTechTV/uploads"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/GoogleDevelopers')
def RawFetchFeedGoogleDevelopers():
thisUrl = "http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/uploads"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/userfeed/<regex(".*"):filename>')
def FetchFeedUserYoutube(filename):
thisUrl = "http://gdata.youtube.com/feeds/api/users/" + filename +"/uploads"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/playlist/<regex(".*"):filename>')
def FetchFeedPlaylist(filename):
thisUrl = "http://gdata.youtube.com/feeds/api/playlists/" + filename
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/playlists/<regex(".*"):filename>')
def FetchFeedAllPlaylists(filename):
thisUrl = "http://gdata.youtube.com/feeds/api/users/" + filename + "/playlists"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/youtube')
def RawFetchYOUTUBEWEB():
thisUrl = "http://www.youtube.com"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/html")
@app.route('/mostrecentvideo')
def RawFetchMOSTRECENTVIDEO():
thisUrl = "http://gdata.youtube.com/feeds/api/standardfeeds/most_recent"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/nbctoday')
def RawFetchMSNBCToday():
thisUrl = "http://podcastfeeds.nbcnews.com/audio/podcast/MSNBC-TDY-PODCAST-MP3.xml"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
# change enclosure url=" to enclosure url="/mp3/
except:
pass
return Response(content, content_type="text/xml")
@app.route('/businessenglishpod')
def RawFetchBEP():
thisUrl = "http://feeds2.feedburner.com/BusinessEnglishPod"
content = u""
try:
result = urllib2.urlopen(thisUrl)
content = result.read()
except:
pass
return Response(content, content_type="text/xml")
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment