@@ -15,27 +15,23 @@ class OnlineContent(object):
15
15
Object to represent a single content on Internet which can be accessed through a link.
16
16
After initiating the `OnlineContent` object using its URL, embeddable code can be generated.
17
17
Embeddable code generation is offline, by default.
18
- However, by using oEmbed protocol, it can be generated from the host site using its own API.
18
+
19
+ However, by using oEmbed protocol, it can be generated from the host site using its own API. (Experimental)
20
+ In some cases, e.g. Flickr images, embed code must be generated online.
19
21
20
22
>>> from embedx import OnlineContent
21
23
>>> content = OnlineContent('http://www.youtube.com/watch?v=_lOT2p_FCvA')
22
- >>> content.extract_id ()
24
+ >>> content.get_content_uid ()
23
25
'_lOT2p_FCvA'
24
26
>>> content.get_embed_code()
25
- "<div class='embed-container'><iframe src='http://www.youtube.com/embed/_lOT2p_FCvA' 'frameborder='0'allowfullscreen></iframe></div>"
26
-
27
- >>> content = OnlineContent('https://twitter.com/codinghorror/status/686254714938200064')
28
- >>> content.extract_id()
29
- '686254714938200064'
30
- >>> content.get_embed_code()
31
- "<div id='embedx-twt' align='center'></div><script async src='https://platform.twitter.com/widgets.js'></script><script> window.onload=(function(){twttr.widgets.createTweet(686254714938200064, document.getElementById('embedx-twt'),{});});</script>"
32
-
33
- """
27
+ "<div class='embedx-yt'><iframe src='http://www.youtube.com/embed/_lOT2p_FCvA' 'frameborder='0' allowfullscreen></iframe></div>"
28
+ >>> content.check_if_alive()
29
+ True
30
+ """
34
31
35
32
# pointer to current `OnlineContent` object
36
33
instance = None
37
-
38
- # these templates must be defined by providing subclasses
34
+ # these templates must be defined by implementor subclasses
39
35
hostnames = []
40
36
EMBED_SCRIPT = ""
41
37
STATUS_LINK = "" # it is needed for hosts which generate HTTP:200 even if CONTENT_ID is invalid e.g. Youtube
@@ -65,6 +61,7 @@ def get_embed_code(self):
65
61
return self .instance .EMBED_SCRIPT % ({'content_uid' : self .get_content_uid ()})
66
62
elif len (self .instance .OEMBED_LINK ):
67
63
oembed_req_url = self .instance .EMBED_SCRIPT % ({'URL' : self .instance .url })
64
+ return oembed_req_url
68
65
else :
69
66
raise NotImplementedError
70
67
@@ -109,10 +106,16 @@ def check_if_alive(self):
109
106
110
107
111
108
class YouTube (OnlineContent ):
109
+ """ Use `OnlineContent` object to instatiate or use this class
110
+
111
+ >>> from embedx import OnlineContent
112
+ >>> content = OnlineContent('http://www.youtube.com/watch?v=_lOT2p_FCvA')
113
+ >>> content.get_content_uid()
114
+ '_lOT2p_FCvA'
115
+ """
116
+
112
117
hostnames = ['youtube' , 'youtu.be' ]
113
- EMBED_SCRIPT = ("<div class='embedx-yt'>"
114
- "<iframe src='http://www.youtube.com/embed/%(content_uid)s' 'frameborder='0' allowfullscreen>"
115
- "</iframe></div>" )
118
+ EMBED_SCRIPT = '''<iframe id="embedx-yt" type="text/html" width="640" height="390" position="center" src="http://www.youtube.com/embed/%(content_uid)s" frameborder="0"/>'''
116
119
STATUS_LINK = '''http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%(content_uid)s&format=json'''
117
120
LINK_TEMPLATE = '''https://www.youtube.com/watch?v=%(content_uid)s'''
118
121
@@ -137,10 +140,17 @@ def extract_id(self):
137
140
elif 'youtu.be' in parsed_url .hostname :
138
141
return parsed_url .path [1 :]
139
142
else :
140
- raise ValueError
143
+ raise ValueError ( "Invalid URL for a Youtube video" )
141
144
142
145
143
146
class Vimeo (OnlineContent ):
147
+ """Use `OnlineContent` object to instatiate or use this class
148
+
149
+ >>> from embedx import OnlineContent
150
+ >>> vimeo = OnlineContent('https://vimeo.com/92129360')
151
+ >>> vimeo.get_embed_code()
152
+ "<div class='embedx-vm'><iframe src='http://player.vimeo.com/video/92129360' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>"
153
+ """
144
154
hostnames = ['vimeo' , ]
145
155
146
156
LINK_TEMPLATE = '''https://vimeo.com/%(content_uid)s'''
@@ -161,8 +171,16 @@ def extract_id(self):
161
171
162
172
163
173
class Twitter (OnlineContent ):
164
- hostnames = ['twitter' , ]
174
+ """Use `OnlineContent` object to instatiate or use this class
175
+
176
+ >>> from embedx import OnlineContent
177
+ >>> twit = OnlineContent('https://twitter.com/jack/status/20')
178
+ >>> twit.get_embed_code()
179
+ "<div id='embedx-twt' align='center'></div><script async src='https://platform.twitter.com/widgets.js'></script><script> window.onload=(function(){twttr.widgets.createTweet('20', document.getElementById('embedx-twt'),{});});</script>"
180
+
181
+ """
165
182
183
+ hostnames = ['twitter' , ]
166
184
EMBED_SCRIPT = ("<div id='embedx-twt' align='center'></div>"
167
185
"<script async src='https://platform.twitter.com/widgets.js'></script>"
168
186
"<script> window.onload=(function(){twttr.widgets.createTweet('%(content_uid)s',"
@@ -179,11 +197,15 @@ def extract_id(self):
179
197
180
198
181
199
class Github (OnlineContent ):
200
+ """Use `OnlineContent` object to instatiate or use this class
201
+
202
+ >>> from embedx import OnlineContent
203
+ >>> gist = OnlineContent('https://gist.github.com/kmonsoor/2a1afba4ee127cce50a0')
204
+ >>> gist.get_embed_code()
205
+ "<script src='https://gist.github.com/2a1afba4ee127cce50a0.js'></script>"
182
206
"""
183
- Generate embed script for Github gist
184
- """
185
- hostnames = ['github' , ]
186
207
208
+ hostnames = ['github' , ]
187
209
EMBED_SCRIPT = "<script src='https://gist.github.com/%(content_uid)s.js'></script>"
188
210
189
211
def __init__ (self , url ):
@@ -199,7 +221,6 @@ def extract_id(self):
199
221
class Flickr (OnlineContent ):
200
222
hostnames = ['flickr' , ]
201
223
202
- # EMBED_SCRIPT = ""
203
224
OEMBED_LINK = "https://www.flickr.com/services/oembed/?url=%(URL)s&format=json"
204
225
205
226
def extract_id (self ):
@@ -216,6 +237,7 @@ def extract_id(self):
216
237
raise NotImplementedError
217
238
218
239
240
+ # for quick overview testing
219
241
if __name__ == '__main__' :
220
242
test_urls = ['https://twitter.com/thepodcastdude/status/686258030229336064' ,
221
243
'youtube.com/watch?v=_lOT2p_FCvA' ,
@@ -227,7 +249,7 @@ def extract_id(self):
227
249
try :
228
250
ov = OnlineContent (a_url )
229
251
print (ov .get_content_uid ())
230
- # print ov.check_if_alive()
252
+ # print( ov.check_if_alive() )
231
253
print (ov .get_embed_code ())
232
254
except NotImplementedError :
233
255
pass
0 commit comments