Commit 242ea087 authored by ntome's avatar ntome Committed by Romain Bignon

youtube: use googleapi module instead of gdata which is obsolete

parent 8c6eac80
...@@ -145,8 +145,8 @@ echo. ...@@ -145,8 +145,8 @@ echo.
echo -- mechanize echo -- mechanize
%PythonPath%Scripts\easy_install.exe mechanize || goto :InstallFailed %PythonPath%Scripts\easy_install.exe mechanize || goto :InstallFailed
echo. echo.
echo -- gdata echo -- google-api-python-client
%PythonPath%Scripts\easy_install.exe gdata || goto :InstallFailed %PythonPath%Scripts\easy_install.exe google-api-python-client || goto :InstallFailed
echo. echo.
echo -- feedparser echo -- feedparser
%PythonPath%Scripts\easy_install.exe feedparser || goto :InstallFailed %PythonPath%Scripts\easy_install.exe feedparser || goto :InstallFailed
......
...@@ -18,11 +18,6 @@ ...@@ -18,11 +18,6 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
try:
import gdata.youtube.service
except ImportError:
raise ImportError("Please install python-gdata")
import datetime import datetime
import re import re
import urllib import urllib
...@@ -38,6 +33,12 @@ from weboob.tools.value import ValueBackendPassword, Value ...@@ -38,6 +33,12 @@ from weboob.tools.value import ValueBackendPassword, Value
from .browser import YoutubeBrowser from .browser import YoutubeBrowser
from .video import YoutubeVideo from .video import YoutubeVideo
try:
# weboob modifies HTTPSConnection, which conflicts with apiclient
# so apiclient must be imported after
from apiclient.discovery import build as ytbuild
except ImportError:
raise ImportError("Please install python-googleapi")
__all__ = ['YoutubeModule'] __all__ = ['YoutubeModule']
...@@ -64,18 +65,14 @@ class YoutubeModule(Module, CapVideo, CapCollection): ...@@ -64,18 +65,14 @@ class YoutubeModule(Module, CapVideo, CapCollection):
def _entry2video(self, entry): def _entry2video(self, entry):
""" """
Parse an entry returned by gdata and return a Video object. Parse an entry returned by googleapi and return a Video object.
""" """
video = YoutubeVideo(to_unicode(entry.id.text.split('/')[-1].strip())) snippet = entry['snippet']
video.title = to_unicode(entry.media.title.text.strip()) video = YoutubeVideo(to_unicode(entry['id']['videoId']))
video.duration = datetime.timedelta(seconds=int(entry.media.duration.seconds.strip())) video.title = to_unicode(snippet['title'].strip())
video.thumbnail = Thumbnail(entry.media.thumbnail[0].url.strip()) # duration does not seem to be available with api
video.thumbnail.url = to_unicode(video.thumbnail.id) video.thumbnail = Thumbnail(snippet['thumbnails']['default']['url'])
video.author = to_unicode(snippet['channelTitle'].strip())
if entry.author[0].name.text:
video.author = to_unicode(entry.author[0].name.text.strip())
if entry.media.name:
video.author = to_unicode(entry.media.name.text.strip())
return video return video
def _set_video_url(self, video): def _set_video_url(self, video):
...@@ -100,16 +97,15 @@ class YoutubeModule(Module, CapVideo, CapCollection): ...@@ -100,16 +97,15 @@ class YoutubeModule(Module, CapVideo, CapCollection):
if m: if m:
_id = m.group(1) _id = m.group(1)
yt_service = gdata.youtube.service.YouTubeService() params = {'id': _id, 'part': 'id,snippet'}
yt_service.ssl = True
try: youtube = self._build_yt()
entry = yt_service.GetYouTubeVideoEntry(video_id=_id) response = youtube.videos().list(**params).execute()
except gdata.service.Error as e: items = response.get('items', [])
if e.args[0]['status'] == 400: if not items:
return None return None
raise
video = self._entry2video(entry) video = self._entry2video(items[0])
self._set_video_url(video) self._set_video_url(video)
video.set_empty_fields(NotAvailable) video.set_empty_fields(NotAvailable)
...@@ -120,34 +116,41 @@ class YoutubeModule(Module, CapVideo, CapCollection): ...@@ -120,34 +116,41 @@ class YoutubeModule(Module, CapVideo, CapCollection):
return video return video
def _build_yt(self):
DEVELOPER_KEY = "AIzaSyApVVeZ03XkKDYHX8T5uOn8Eizfe9CMDbs"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
return ytbuild(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
def search_videos(self, pattern, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False): def search_videos(self, pattern, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False):
YOUTUBE_MAX_RESULTS = 50 YOUTUBE_MAX_RESULTS = 50
YOUTUBE_MAX_START_INDEX = 500 YOUTUBE_MAX_START_INDEX = 500
yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True
start_index = 1 youtube = self._build_yt()
params = {'part': 'id,snippet', 'maxResults': YOUTUBE_MAX_RESULTS}
if pattern is not None:
if isinstance(pattern, unicode):
pattern = pattern.encode('utf-8')
params['q'] = pattern
params['safeSearch'] = 'none' if nsfw else 'strict' # or 'moderate'
params['order'] = ('relevance', 'rating', 'viewCount', 'date')[sortby]
nb_yielded = 0 nb_yielded = 0
while True: while True:
query = gdata.youtube.service.YouTubeVideoQuery() search_response = youtube.search().list(**params).execute()
if pattern is not None: items = search_response.get('items', [])
if isinstance(pattern, unicode): for entry in items:
pattern = pattern.encode('utf-8') if entry["id"]["kind"] != "youtube#video":
query.vq = pattern continue
query.orderby = ('relevance', 'rating', 'viewCount', 'published')[sortby]
query.racy = 'include' if nsfw else 'exclude'
query.max_results = YOUTUBE_MAX_RESULTS
if start_index >= YOUTUBE_MAX_START_INDEX:
return
query.start_index = start_index
start_index += YOUTUBE_MAX_RESULTS
feed = yt_service.YouTubeQuery(query)
for entry in feed.entry:
yield self._entry2video(entry) yield self._entry2video(entry)
nb_yielded += 1 nb_yielded += 1
params['pageToken'] = search_response.get('nextPageToken')
if not params['pageToken']:
return
if nb_yielded < YOUTUBE_MAX_RESULTS: if nb_yielded < YOUTUBE_MAX_RESULTS:
return return
......
...@@ -130,6 +130,7 @@ def install_weboob(): ...@@ -130,6 +130,7 @@ def install_weboob():
'python-dateutil', 'python-dateutil',
'PyYAML', 'PyYAML',
'prettytable', 'prettytable',
'google-api-python-client',
] ]
try: try:
import Image import Image
...@@ -143,7 +144,6 @@ def install_weboob(): ...@@ -143,7 +144,6 @@ def install_weboob():
requirements.append('PIL') requirements.append('PIL')
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
requirements.append('gdata')
requirements.append('mechanize') requirements.append('mechanize')
if sys.version_info < (3, 2): if sys.version_info < (3, 2):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment