Merge lp:~stefanor/ibid/youtube-561684 into lp:~ibid-core/ibid/old-trunk-1.6

Proposed by Stefano Rivera
Status: Merged
Approved by: Jonathan Hitchcock
Approved revision: 912
Merged at revision: 924
Proposed branch: lp:~stefanor/ibid/youtube-561684
Merge into: lp:~ibid-core/ibid/old-trunk-1.6
Diff against target: 63 lines (+30/-13)
1 file modified
ibid/plugins/urlinfo.py (+30/-13)
To merge this branch: bzr merge lp:~stefanor/ibid/youtube-561684
Reviewer Review Type Date Requested Status
Jonathan Hitchcock Approve
Michael Gorven Approve
Keegan Carruthers-Smith Approve
Review via email: mp+23261@code.launchpad.net

Commit message

Update youtube plugin to cope with site redesign

To post a comment you must log in.
Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) wrote :

Query: http://www.youtube.com/watch?v=MG5vMDFFceo
ERROR:scripts.ibid-plugin:Exception occured in Youtube processor of urlinfo plugin
Traceback (most recent call last):
  File "./scripts/ibid-plugin", line 131, in <module>
    processor.process(event)
  File "./ibid/plugins/__init__.py", line 144, in process
    method(event, *match.groups())
  File "./ibid/plugins/urlinfo.py", line 95, in youtube
    'title': info['title'][0].decode('utf-8'),
KeyError: 'title'

review: Needs Fixing
lp:~stefanor/ibid/youtube-561684 updated
910. By Stefano Rivera

Supply el=detailpage parameter to get_video_info for no-embedding videos

911. By Stefano Rivera

Handle YouTube errors appropriately

Revision history for this message
Stefano Rivera (stefanor) wrote :

> KeyError: 'title'

r911

Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) wrote :

Fixed my previous comment's traceback, but

Query: http://www.youtube.com/watch?v=Bb3ep6YW0JI
ERROR:scripts.ibid-plugin:Exception occured in Youtube processor of urlinfo plugin
Traceback (most recent call last):
  File "./scripts/ibid-plugin", line 131, in <module>
    processor.process(event)
  File "./ibid/plugins/__init__.py", line 144, in process
    method(event, *match.groups())
  File "./ibid/plugins/urlinfo.py", line 98, in youtube
    'title': info['title'][0].decode('utf-8'),
KeyError: 'title'

review: Needs Fixing
lp:~stefanor/ibid/youtube-561684 updated
912. By Stefano Rivera

Try the three el_types that youtube-dl does. Copy other parameters too.

Revision history for this message
Stefano Rivera (stefanor) wrote :

> Fixed my previous comment's traceback, but

r912. Dammit Instead of trying to second-guess youtube-dl, I just re-implemented their get_video_info algorithm.

Revision history for this message
Keegan Carruthers-Smith (keegan-csmith) :
review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve

review: Approve
Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/urlinfo.py'
2--- ibid/plugins/urlinfo.py 2010-03-27 15:50:58 +0000
3+++ ibid/plugins/urlinfo.py 2010-04-12 21:53:28 +0000
4@@ -1,12 +1,13 @@
5 # Copyright (c) 2009-2010, Michael Gorven, Stefano Rivera
6 # Released under terms of the MIT/X/Expat Licence. See COPYING for details.
7 #
8-# The youtube Processor uses code from youtube-dl:
9+# The youtube Processor is inspired by (and steals the odd RE from) youtube-dl:
10 # Copyright (c) 2006-2008 Ricardo Garcia Gonzalez
11 # Released under MIT Licence
12
13+from cgi import parse_qs
14 from urllib import urlencode
15-from urllib2 import urlopen, build_opener, HTTPError, HTTPRedirectHandler, HTTPCookieProcessor
16+from urllib2 import urlopen, build_opener, HTTPError, HTTPRedirectHandler
17 import logging
18 import re
19
20@@ -88,16 +89,32 @@
21 r'(?:v/|(?:watch(?:\.php)?)?\?(?:.+&)?v=)'
22 r'([0-9A-Za-z_-]+)(?(1)[&/].*)?$')
23 def youtube(self, event, id):
24- url = 'http://www.youtube.com/watch?v=' + id
25- opener = build_opener(HTTPCookieProcessor())
26- opener.addheaders = [('User-Agent', default_user_agent)]
27- video_webpage = opener.open(url).read()
28- title = re.search(r'<title>\s*YouTube\s+-\s+([^<]*)</title>',
29- video_webpage, re.M | re.I | re.DOTALL).group(1).strip()
30- t = re.search(r', "t": "([^"]+)"', video_webpage).group(1)
31- event.addresponse(u'%(title)s: %(url)s', {
32- 'title': title,
33- 'url': 'http://www.youtube.com/get_video?video_id=%s&t=%s' % (id, t),
34- })
35+ for el_type in ('embedded', 'detailpage', 'vevo'):
36+ url = 'http://www.youtube.com/get_video_info?' + urlencode({
37+ 'video_id': id,
38+ 'el': el_type,
39+ 'ps': 'default',
40+ 'eurl': '',
41+ 'gl': 'US',
42+ 'hl': 'en',
43+ })
44+ info = parse_qs(urlopen(url).read())
45+ if info.get('status', [None])[0] == 'ok':
46+ break
47+
48+ if info.get('status', [None])[0] == 'ok':
49+ event.addresponse(u'%(title)s: %(url)s', {
50+ 'title': info['title'][0].decode('utf-8'),
51+ 'url': 'http://www.youtube.com/get_video?' + urlencode({
52+ 'video_id': id,
53+ 't': info['token'][0],
54+ }),
55+ })
56+ else:
57+ event.addresponse(u"Sorry, I couldn't retreive that, YouTube says: "
58+ u"%(status)s: %(reason)s", {
59+ 'status': info['status'][0],
60+ 'reason': info['reason'][0],
61+ })
62
63 # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches