Commit [ef0411] Maximize Restore History

[plugin.video.wimp] updated to version 2.0.0

spiff spiff 2012-10-15

changed plugin.video.wimp/resources/language/English/strings.xml
changed plugin.video.wimp/resources/language/German/strings.xml
changed plugin.video.wimp/resources/settings.xml
changed plugin.video.wimp/addon.py
changed plugin.video.wimp/addon.xml
changed plugin.video.wimp/changelog.txt
changed plugin.video.wimp/icon.png
copied plugin.video.wimp/resources/lib/scraper.py -> plugin.video.wimp/resources/lib/api.py
plugin.video.wimp/resources/language/English/strings.xml Diff Switch to side-by-side view
Loading...
plugin.video.wimp/resources/language/German/strings.xml Diff Switch to side-by-side view
Loading...
plugin.video.wimp/resources/settings.xml Diff Switch to side-by-side view
Loading...
plugin.video.wimp/addon.py Diff Switch to side-by-side view
Loading...
plugin.video.wimp/addon.xml Diff Switch to side-by-side view
Loading...
plugin.video.wimp/changelog.txt Diff Switch to side-by-side view
Loading...
plugin.video.wimp/icon.png Diff Switch to side-by-side view
Loading...
plugin.video.wimp/resources/lib/scraper.py to plugin.video.wimp/resources/lib/api.py
--- a/plugin.video.wimp/resources/lib/scraper.py
+++ b/plugin.video.wimp/resources/lib/api.py
@@ -1,132 +1,108 @@
-import urllib2
-import re
-from datetime import datetime
-from base64 import b64decode
-from BeautifulSoup import BeautifulSoup
-
-MAIN_URL = 'http://m.wimp.com/'
-
-MONTHS = {'Jan': 1,
-          'Feb': 2,
-          'Mar': 3,
-          'Apr': 4,
-          'May': 5,
-          'Jun': 6,
-          'Jul': 7,
-          'Aug': 8,
-          'Sep': 9,
-          'Oct': 10,
-          'Nov': 11,
-          'Dec': 12}
-
-
-def get_current_videos():
-    tree = __get_tree(MAIN_URL)
-    return __get_videos(tree)
-
-
-def get_archive_dates():
-    url = MAIN_URL + 'archives'
-    tree = __get_tree(url)
-    archive_dates = []
-    for entry in tree.findAll('a', {'class': 'b'}):
-        title = entry.string.strip()
-        archive_id = entry['href'].replace('/archives/', '').strip()
-        archive_dates.append({'title': title,
-                              'archive_id': archive_id})
-    return archive_dates
-
-
-def get_videos_by_archive_date(date):
-    url = MAIN_URL + 'archives/%s' % date
-    tree = __get_tree(url)
-    return __get_videos(tree)
-
-
-def get_search_result(query):
-    url = MAIN_URL + 'search?query=%s' % query
-    tree = __get_tree(url)
-    return __get_videos(tree)
-
-
-def get_random_video_url(mobile=False):
-    return get_video_url('random', mobile)
-
-
-def get_video_url(video_id, mobile=False):
-    log('get_video_url started with video_id=%s mobile: %s' % (video_id,
-                                                               mobile))
-    url = MAIN_URL + video_id
-
-    def _mobile(url):
-        log('get_video_url mobile mode')
-        tree = __get_tree(url, mobile=True)
-        title = tree.head.title.string.replace('[VIDEO]', '').strip()
-        video_url = tree.find('a', {'id': 'video'})['href']
-        return video_url, title
-
-    def _flv(url):
-        log('get_video_url flv mode')
-        tree = __get_tree(url, mobile=False)
-        title = tree.head.title.string.replace('[VIDEO]', '').strip()
-        r_js = re.compile('lxUTILsign')
-        r_gc = re.compile('var googleCode = \'(.+?)\';')  # funny idea :-)
-        r_vurl = re.compile('"file","(.+?)"')
-        try:
-            js_code = tree.find('script', text=r_js).string.strip()
-            js_params = b64decode(re.search(r_gc, js_code).group(1))
-            video_url = re.search(r_vurl, js_params).group(1)
-        except AttributeError:
-            log('get_video_url flv mode FAILED')
-            video_url = None
-        return video_url, title
-
-    if not mobile:
-        video_url, title = _flv(url) or _mobile(url)
-    else:
-        video_url, title = _mobile(url)
-    return video_url, title
-
-
-def __get_videos(tree):
-    videos = []
-    page_title = tree.head.title.string
-    year = None
-    if page_title:
-        year = filter(lambda s: s.isdigit(), page_title)
-    if not year:
-        year = datetime.now().year
-    for video in tree.findAll('span', {'class': 'video_date'}):
-        link = video.nextSibling.nextSibling
-        title = link.string.strip()
-        video_id = link['href'].replace(MAIN_URL, '').strip('/')
-        date_str = video.string
-        month_str, day_str = date_str.split()
-        month = MONTHS.get(month_str)
-        date = '%02i.%02i.%s' % (int(day_str), month, year)
-        videos.append({'title': title,
-                       'video_id': video_id,
-                       'date': date})
-    return videos
-
-
-def __get_tree(url, mobile=True):
-    if mobile:
-        if '?' in url:
-            url = url + '&nord'
-        else:
-            url = url + '?nord'
-    log('__get_tree opening url: %s' % url)
-    req = urllib2.Request(url)
-    req.add_header('Accept', ('text/html,application/xhtml+xml,'
-                              'application/xml;q=0.9,*/*;q=0.8'))
-    req.add_header('User-Agent', ('Mozilla/5.0 (X11; Linux i686) '
-                                  'AppleWebKit/535.21 (KHTML, like Gecko) '
-                                  'Chrome/19.0.1041.0 Safari/535.21'))
-    html = urllib2.urlopen(req).read()
-    tree = BeautifulSoup(html, convertEntities=BeautifulSoup.HTML_ENTITIES)
-    return tree
-
-
-def log(msg):
-    print('%s scraper: %s' % ('wimp.com', msg))
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+#
+#     Copyright (C) 2012 Tristan Fischer
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program 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 General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+
+from datetime import date
+from urllib import urlencode
+from urllib2 import urlopen, Request, HTTPError, URLError
+import simplejson
+
+
+class NetworkError(Exception):
+    pass
+
+
+class WimpApi():
+
+    API_URL = 'http://m.wimp.com/apiv2/'
+
+    def get_current_videos(self):
+        data = self.call('list')
+        return self.__format_videos(data['videos'])
+
+    def get_archive_dates(self):
+
+        def month_range(start_month, start_year, end_month, end_year):
+            ym_start = 12 * start_year + start_month - 1
+            ym_end = 12 * end_year + end_month
+            for ym in range(ym_start, ym_end):
+                y, m = divmod(ym, 12)
+                yield y, m + 1
+
+        today = date.today()
+        dates = month_range(12, 2008, today.month, today.year)
+        return [{
+            'title': date(year, month, 1).strftime('%B %Y'),
+            'year': year,
+            'month': month,
+        } for year, month in dates]
+
+    def get_archived_videos(self, year, month):
+        params = {
+            'year': '%04i' % year,
+            'month': '%02i' % month,
+        }
+        data = self.call('archive', params)
+        return self.__format_videos(data['videos'])
+
+    def get_serch_result(self, search_string):
+        params = {'query': search_string}
+        data = self.call('search', params)
+        return self.__format_videos(data['videos'])
+
+    def __format_videos(self, videos):
+        return [{
+            'uid': video['uid'],
+            'title': video['title'],
+            'date': self.__format_date(video['date']),
+            'page': video['page'],
+            'thumb': video.get('thumb_506_332') or video.get('thumb_1164_766'),
+            'video_url': video['video_hq_url'],
+        } for video in videos]
+
+    def __format_date(self, date_str):
+        y, m, d = date_str.split('-')
+        return '%02i.%02i.%s' % (int(d), int(m), int(y))
+
+    def call(self, path, params=None):
+        url = WimpApi.API_URL + path
+        if params:
+            url += '?' + urlencode(params)
+        self.log('opening url: %s' % url)
+        req = Request(url)
+        req.add_header('User-Agent', 'XBMC WimpApi')
+        try:
+            response = urlopen(req).read()
+        except HTTPError, error:
+            raise NetworkError(error)
+        except URLError, error:
+            raise NetworkError(error)
+        self.log('got %d bytes' % len(response))
+        data = simplejson.loads(response)
+        return data
+
+    @staticmethod
+    def log(text):
+        print 'WimpApi: %s' % text
+
+
+if __name__ == '__main__':
+    api = WimpApi()
+    assert api.get_current_videos()
+    assert api.get_archived_videos(year=2012, month=9)
+    assert api.get_archive_dates()