Commit ad37c416 authored by Laurent Bachelier's avatar Laurent Bachelier 🐧 Committed by ntome
parent 054b2aef
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .module import NolifeTVModule
__all__ = ['NolifeTVModule']
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.deprecated.browser import Browser, BrowserIncorrectPassword
from weboob.tools.compat import urlencode
from weboob.deprecated.browser.decorators import id2url
from .video import NolifeTVVideo
from .pages import VideoPage, VideoListPage, FamilyPage, AboPage, LoginPage, HomePage
__all__ = ['NolifeTVBrowser']
class NolifeTVBrowser(Browser):
USER_AGENT = Browser.USER_AGENTS['desktop_firefox']
DOMAIN = 'mobile.nolife-tv.com'
PROTOCOL = 'http'
PAGES = { r'http://mobile.nolife-tv.com/online/familles-\w+/': FamilyPage,
r'http://mobile.nolife-tv.com/online/emission-(?P<id>\d+)/': VideoPage,
'http://mobile.nolife-tv.com/do.php': VideoListPage,
'http://mobile.nolife-tv.com/online/': VideoListPage,
'http://mobile.nolife-tv.com/abonnement/': AboPage,
'http://mobile.nolife-tv.com/login': LoginPage,
'http://mobile.nolife-tv.com/': HomePage,
}
AVAILABLE_VIDEOS = ['[Gratuit]']
def is_logged(self):
return (self.username is None or (not self.is_on_page(HomePage)) or self.page.is_logged())
def login(self):
if self.username is None:
return
if not self.is_on_page(LoginPage):
self.location('/login', no_login=True)
self.page.login(self.username, self.password)
if self.is_on_page(LoginPage):
raise BrowserIncorrectPassword()
self.location('/abonnement/', no_login=True)
assert self.is_on_page(AboPage)
self.AVAILABLE_VIDEOS = self.page.get_available_videos()
@id2url(NolifeTVVideo.id2url)
def get_video(self, url, video=None):
self.location(url)
assert self.is_on_page(VideoPage)
return self.page.get_video(video)
def iter_family(self, type, sub):
self.location('/online/familles-%s/' % type)
assert self.is_on_page(FamilyPage)
return self.page.iter_family(sub)
def iter_category(self, type):
self.location('/online/familles-%s/' % type)
assert self.is_on_page(FamilyPage)
return self.page.iter_category()
def iter_video(self, family):
data = { 'a': 'ge',
'famille': family,
'emissions': 0 }
while True:
self.location('/do.php', urlencode(data))
assert self.is_on_page(VideoListPage)
if self.page.is_list_empty():
break
for vid in self.page.iter_video(self.AVAILABLE_VIDEOS):
yield vid
data['emissions'] = data['emissions'] + 1
def get_latest(self):
return self.iter_video(0)
def search_videos(self, pattern):
data = { 'search': pattern,
'submit': 'Rechercher' }
self.location('/online/', urlencode(data))
assert self.is_on_page(VideoListPage)
for vid in self.page.iter_video(self.AVAILABLE_VIDEOS):
yield vid
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.video import CapVideo, BaseVideo
from weboob.capabilities.collection import CapCollection, CollectionNotFound, Collection
from weboob.tools.value import Value, ValueBackendPassword
from weboob.tools.backend import Module, BackendConfig
from .browser import NolifeTVBrowser
from .video import NolifeTVVideo
import urllib
import time
from hashlib import md5
__all__ = ['NolifeTVModule']
class NolifeTVModule(Module, CapVideo, CapCollection):
NAME = 'nolifetv'
MAINTAINER = u'Romain Bignon'
EMAIL = 'romain@weboob.org'
VERSION = '1.4'
DESCRIPTION = 'NolifeTV French video streaming website'
LICENSE = 'AGPLv3+'
BROWSER = NolifeTVBrowser
CONFIG = BackendConfig(Value('username', label='Username', default=''),
ValueBackendPassword('password', label='Password', default=''),
Value('quality', label='Quality',
choices = { '1':'LQ', '2':'HQ', '3':'TV', '4':'720p', '5':'1080p' },
default = '5' ))
def create_default_browser(self):
username = self.config['username'].get()
if username:
password = self.config['password'].get()
else:
password = None
return self.create_browser(username, password)
def iter_resources(self, objs, split_path):
with self.browser:
if BaseVideo in objs:
collection = self.get_collection(objs, split_path)
if collection.path_level == 0:
yield Collection([u'theme'], u'Par theme')
yield Collection([u'type'], u'Par type')
yield Collection([u'latest'], u'Latest NolifeTV videos')
if collection.path_level == 1:
if split_path[0] == 'latest':
for vid in self.browser.get_latest():
yield vid
else:
for cat in self.browser.iter_category(split_path[0]):
yield cat
if collection.path_level == 2:
for cat in self.browser.iter_family(split_path[0], split_path[1]):
yield cat
if collection.path_level == 3:
for cat in self.browser.iter_video(split_path[2]):
yield cat
def validate_collection(self, objs, collection):
if BaseVideo in objs:
if collection.path_level == 0:
return
if collection.path_level == 1 and collection.split_path[0] in [u'theme', u'type', u'latest']:
return
if collection.path_level > 1:
return
raise CollectionNotFound(collection.split_path)
def get_video(self, _id):
with self.browser:
return self.browser.get_video(_id)
def fill_video(self, video, fields):
with self.browser:
self.browser.get_video(NolifeTVVideo.id2url(video.id), video)
if 'thumbnail' in fields and video.thumbnail:
with self.browser:
video.thumbnail.data = self.browser.readurl(video.thumbnail.url)
if 'url' in fields:
with self.browser:
video.url = self.get_url(video.id, self.config['quality'].get())
return video
def search_videos(self, pattern, sortby=CapVideo.SEARCH_RELEVANCE, nsfw=False):
with self.browser:
return self.browser.search_videos(pattern)
OBJECTS = { NolifeTVVideo: fill_video }
SALT = 'a53be1853770f0ebe0311d6993c7bcbe'
def genkey(self):
# This website is really useful to get info: http://www.showmycode.com/
timestamp = str(int(time.time()))
skey = md5(md5(timestamp).hexdigest() + self.SALT).hexdigest()
return skey, timestamp
def get_url(self, id, quality):
skey, timestamp = self.genkey()
self.browser.readurl('http://online.nolife-tv.com/_nlfplayer/api/api_player.php',
'quality=%s&a=EML&skey=%s&id%%5Fnlshow=%s&timestamp=%s' % (quality, skey, id, timestamp))
skey, timestamp = self.genkey()
data = self.browser.readurl('http://online.nolife-tv.com/_nlfplayer/api/api_player.php',
'quality=%s&a=UEM%%7CSEM%%7CMEM%%7CCH%%7CSWQ&skey=%s&id%%5Fnlshow=%s&timestamp=%s' % (quality, skey, id, timestamp))
values = dict([urllib.splitvalue(s) for s in data.split('&')])
if 'url' not in values:
return None
return unicode(values['url'])
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.collection import Collection
from weboob.capabilities.image import Thumbnail
from weboob.deprecated.browser import Page
import re
from datetime import datetime, timedelta
from .video import NolifeTVVideo
class VideoPage(Page):
def get_video(self, video):
if not video:
video = NolifeTVVideo(self.group_dict['id'])
els = self.document.getroot().xpath('//div[@data-role="content"]')
if els and els[0] is not None:
h3 = els[0].find('h3')
if h3 is not None and h3.text:
video.title = unicode(h3.text)
h4 = els[0].find('h4')
if h4 is not None and h4.text:
video.title = video.title + u' - ' + h4.text
thumb = els[0].find('p/img')
if thumb is not None and thumb.get('src'):
video.thumbnail = Thumbnail(thumb.attrib['src'])
video.thumbnail.url = video.thumbnail.id
ps = els[0].findall('p')
if len(ps) > 4:
if ps[4].text:
video.description = ps[4].text
if ps[0].text and ps[0].text != u'∞':
video.date = datetime.strptime(ps[0].text, '%d/%m/%Y').date()
for text in ps[2].xpath('.//text()'):
m = re.search(r'[^\d]*((\d+):)?(\d+)s?', text)
if m:
if m.group(2):
minutes = int(m.group(2))
else:
minutes = 0
video.duration = timedelta(minutes=minutes,
seconds=int(m.group(3)))
return video
class VideoListPage(Page):
def is_list_empty(self):
return self.document.getroot() is None
def iter_video(self, available_videos):
for el in self.document.getroot().xpath('//li/a'):
strongs = el.findall('p/strong')
if len(strongs) > 3 and strongs[0].text not in ['Autopromo', 'Annonce'] and strongs[1].text in available_videos:
m = re.search(r'emission-(\d+)', el.attrib['href'])
if m and m.group(1):
video = NolifeTVVideo(m.group(1))
h3 = el.find('h3')
if h3 is not None and h3.text:
video.title = unicode(h3.text)
if strongs[3].text:
video.title = video.title + ' - ' + strongs[3].text
yield video
class FamilyPage(Page):
def iter_category(self):
subs = list()
for el in self.document.xpath('//ul/li[@data-role="list-divider"]'):
if el.text not in subs:
yield Collection([el.text], unicode(el.text))
subs.append(el.text)
def iter_family(self, sub):
for el in self.document.xpath('//ul/li[@data-role="list-divider"]'):
if el.text != sub:
continue
while True:
el = el.getnext()
if el is None or el.get('data-role'):
break
h1 = el.find('.//h1')
id = h1.getparent().attrib['href']
m = re.search(r'famille-(\d+)', id)
if m and m.group(1):
yield Collection([m.group(1)], unicode(h1.text))
class AboPage(Page):
def get_available_videos(self):
available = ['[Gratuit]']
for text in self.document.xpath('//div[@data-role="content"]/center/text()'):
if 'Soutien' in text:
available.append('[Archive]')
available.append('[Standard]')
if 'Standard' in text:
available.append('[Standard]')
return available
class LoginPage(Page):
def login(self, username, password):
self.browser.select_form(name='login')
self.browser['username'] = str(username)
self.browser['password'] = str(password)
self.browser.submit()
class HomePage(Page):
def is_logged(self):
return len(self.document.xpath('//a[@href="deconnexion/"]')) == 1
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo
class NolifeTVTest(BackendTest):
MODULE = 'nolifetv'
def test_search(self):
l = list(self.backend.search_videos('nolife'))
self.assertTrue(len(l) > 0)
v = l[0]
self.backend.fillobj(v, ('url',))
self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
def test_latest(self):
l = list(self.backend.iter_resources([BaseVideo], [u'latest']))
assert len(l) > 0
v = l[0]
self.backend.fillobj(v, ('url',))
self.assertTrue(v.url and v.url.startswith('http://'), 'URL for video "%s" not found: %s' % (v.id, v.url))
# -*- coding: utf-8 -*-
# Copyright(C) 2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.capabilities.video import BaseVideo
class NolifeTVVideo(BaseVideo):
def __init__(self, *args, **kwargs):
BaseVideo.__init__(self, *args, **kwargs)
self.ext = u'mp4'
@classmethod
def id2url(cls, _id):
return u'http://mobile.nolife-tv.com/online/emission-%s/' % _id
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