From 2c141fdbcff691d6110680825289cb5697dc1700 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 13 Oct 2011 19:28:18 +0200 Subject: [PATCH] add api, refine parse_movie_path --- ox/__init__.py | 1 + ox/api.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ ox/movie.py | 24 +++++++++---- 3 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 ox/api.py diff --git a/ox/__init__.py b/ox/__init__.py index 581bc54..3fe735f 100644 --- a/ox/__init__.py +++ b/ox/__init__.py @@ -8,6 +8,7 @@ import js import jsonc import net +from api import * from file import * from form import * from format import * diff --git a/ox/api.py b/ox/api.py new file mode 100644 index 0000000..92c147c --- /dev/null +++ b/ox/api.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +# GPL 2011 + +import cookielib +import urllib2 + +from .utils import json +from .form import MultiPartForm + +__all__ = ['API'] + + +class API(object): + __version__ = 0.0 + __name__ = 'ox' + DEBUG = False + debuglevel = 0 + + def __init__(self, url, cj=None): + if cj: + self._cj = cj + else: + self._cj = cookielib.CookieJar() + self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj), + urllib2.HTTPHandler(debuglevel=self.debuglevel)) + self._opener.addheaders = [ + ('User-Agent', '%s/%s' % (self.__name__, self.__version__)) + ] + + self.url = url + r = self._request('api', {'docs': True}) + self._properties = r['data']['actions'] + self._actions = r['data']['actions'].keys() + for a in r['data']['actions']: + self._add_action(a) + + def _add_method(self, method, name): + if name is None: + name = method.func_name + setattr(self.__class__, name, method) + + def _add_action(self, action): + def method(self, *args, **kw): + if not kw: + if args: + kw = args[0] + else: + kw = None + return self._request(action, kw) + method.__doc__ = self._properties[action]['doc'] + method.func_name = str(action) + self._add_method(method, action) + + def _json_request(self, url, form): + result = {} + try: + request = urllib2.Request(str(url)) + body = str(form) + request.add_header('Content-type', form.get_content_type()) + request.add_header('Content-length', len(body)) + request.add_data(body) + result = self._opener.open(request).read().strip() + return json.loads(result) + except urllib2.HTTPError, e: + if self.DEBUG: + import webbrowser + if e.code >= 500: + with open('/tmp/error.html', 'w') as f: + f.write(e.read()) + webbrowser.open_new_tab('/tmp/error.html') + + result = e.read() + try: + result = json.loads(result) + except: + result = {'status':{}} + result['status']['code'] = e.code + result['status']['text'] = str(e) + return result + except: + if self.DEBUG: + import webbrowser + import traceback + traceback.print_exc() + if result: + with open('/tmp/error.html', 'w') as f: + f.write(str(result)) + webbrowser.open_new_tab('/tmp/error.html') + raise + + def _request(self, action, data=None): + form = MultiPartForm() + form.add_field('action', action) + if data: + form.add_field('data', json.dumps(data)) + return self._json_request(self.url, form) diff --git a/ox/movie.py b/ox/movie.py index 54e53e4..c97498f 100644 --- a/ox/movie.py +++ b/ox/movie.py @@ -27,13 +27,19 @@ def parse_movie_path(path): "L/Labarthe, André S_/Cinéastes de notre temps (1964-)/Cinéastes de notre temps.Episode.Jean Renoir le patron, première partie_ La Recherche du relatif.avi" "S/Scott, Ridley/Blade Runner (1982)/Blade Runner.Directors's Cut.avi" + or + + T/Title (Year)/Title.avi """ episodeTitle = episodeYear = None episodeDirector = [] parts = path.split('/') #title/year - title = parts[2] + if len(parts) == 4: + title = parts[2] + else: + title = parts[1] year = findRe(title, '(\(\d{4}\))') if not year: year = findRe(title, '(\(\d{4}-\d*\))') @@ -42,13 +48,17 @@ def parse_movie_path(path): year = year[1:-1] if '-' in year: year = findRe(year, '\d{4}') + #director - director = parts[1] - if director.endswith('_'): - director = "%s." % director[:-1] - director = director.split('; ') - director = [normalizeName(d).strip() for d in director] - director = filter(lambda d: d not in ('Unknown Director', 'Various Directors'), director) + if len(parts) == 4: + director = parts[1] + if director.endswith('_'): + director = "%s." % director[:-1] + director = director.split('; ') + director = [normalizeName(d).strip() for d in director] + director = filter(lambda d: d not in ('Unknown Director', 'Various Directors'), director) + else: + director = [] #extension/language fileparts = parts[-1].split('.')