-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathembedly.py
More file actions
100 lines (84 loc) · 2.79 KB
/
embedly.py
File metadata and controls
100 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Built in library imports
import re
import urllib
import urllib2
# JSON decoder
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
raise ImportError("Need a json decoder")
# Embed.ly Multi Provider API Endpoint
OEMBED_API_ENDPOINT = 'http://api.embed.ly/v1/api/oembed'
# URL Schemes Supported
URL_SCHEMES_RE = (
r'^http://www\.5min\.com/Video/.*',
r'^http://.*\.viddler\.com/explore/.*/videos/.*',
r'^http://qik\.*/video/.*',
r'^http://www\.hulu\.com/watch/.*',
r'^http://.*\.revision3\.com/.*',
r'^http://.*nfb\.ca/film/.*',
r'^http://.*dailymotion\.com/video/.*',
r'^http://blip\.tv/file/.*',
r'^http://.*\.scribd\.com/doc/.*',
r'^http:/.*\.movieclips\.com/watch/.*',
r'^http://screenr.com/.+',
r'^http://twitpic\.com/.*',
r'^http://.*\.youtube\.com/watch.*',
r'^http://yfrog.com.*',
r'^http://.*amazon.*/gp/product/.*$',
r'^http://.*amazon\..*/.*/dp/.*$',
r'^http://.*flickr.com/.*',
r'^http://www\.vimeo\.com/groups/.*/videos/.*',
r'^http://www\.vimeo\.com/.*',
r'^http://tweetphoto\.com/.*',
r'^http://www\.collegehumor\.com/video:.*',
r'^http://www\.funnyordie\.com/videos/.*',
r'^http://video\.google\.com/videoplay?.*',
r'^http://www\.break\.com/.*/.*',
r'^http://www\.slideshare\.net/.*/.*',
r'^http://www\.ustream\.tv/recorded/.*',
r'^http://www\.ustream\.tv/channel/.*',
r'^http://www\.twitvid\.com/.*',
r'^http://www\.justin.tv/clip/.*',
r'^http://www\.justin.tv/.*',
r'^http://vids\.myspace\.com/index.cfm.*$',
r'^http://www\.metacafe\.com/watch/.*',
r'^http://.*crackle\.com/c/.*',
r'^http://www\.veoh\.com/.*/watch/.*',
r'^http://www\.fancast\.com/.*/.*/videos',
r'^http://.*imgur\.com/.*$',
r'^http://.*imgur\.com/.*$',
)
def is_pattern_match(url):
for pat_re in URL_SCHEMES_RE:
if re.search(pat_re, url):
return True
return False
# returns a dictionary of the oembed object
def get_oembed(url, format='json', maxwidth=None, maxheight=None):
# make sure embed.ly supports the url scheme
if not is_pattern_match(url):
return None
# gather url, format, maxwidth or maxheight options for embed sizing
params = {"url": url}
if maxwidth is not None:
params['maxwidth'] = maxwidth
if maxheight is not None:
params['maxheight'] = maxwidth
if format is not None:
params['format'] = format
# generate query string
query = urllib.urlencode(params)
# api endpoint url
fetch_url = "%s?%s" %(OEMBED_API_ENDPOINT,query)
try:
r = urllib2.urlopen(fetch_url).read()
obj = json.loads(r)
except urllib2.HTTPError, e:
return None
except urllib2.URLError:
return None
return obj