From 51092431631c43b69b4ed24d2851eaef8b7b0789 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 21 Aug 2009 15:04:28 +0200 Subject: [PATCH] use copy of default headers before changing it, user ~/.ox/auth.json for cookies and other user credentials --- README | 9 +++++++++ oxweb/auth.py | 22 ++++++++++++++++++++++ oxweb/karagarga.py | 12 +++++++++--- oxweb/thepiratebay.py | 3 +-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 oxweb/auth.py diff --git a/README b/README index 35bacde..bad1382 100644 --- a/README +++ b/README @@ -7,5 +7,14 @@ Depends: python-feedparser (http://www.feedparser.org/) (there seam to be some issues if not using the one from ubuntu/debian) +Install: + python setup.py install + + some modules require user accont information or cookies to work, + those are saved in ~/.ox/auth.json, most basic form looks like this: + { + "key": "value" + } + Test: nosetests --with-doctest oxweb diff --git a/oxweb/auth.py b/oxweb/auth.py new file mode 100644 index 0000000..0f360df --- /dev/null +++ b/oxweb/auth.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +# GPL 2009 +import os +import simplejson + +import oxlib + + +def get(key): + user_auth = os.environ.get('oxAUTH', os.path.expanduser('~/.ox/auth.json')) + auth = {} + if os.path.exists(user_auth): + f = open(user_auth, "r") + data = f.read() + f.close() + auth = simplejson.loads(data) + if key in auth: + return auth[key] + print "please add key %s to json file '%s'" % (key, user_auth) + return "" + diff --git a/oxweb/karagarga.py b/oxweb/karagarga.py index 39dcf35..44f7b3b 100644 --- a/oxweb/karagarga.py +++ b/oxweb/karagarga.py @@ -1,11 +1,17 @@ import re -from oxlib.cache import getUrlUnicode, DEFAULT_HEADERS +from oxlib import cache from oxlib.html import stripTags from oxlib.text import findRe +import auth -headers = DEFAULT_HEADERS -headers["Cookie"] = "uid=9829; pass=cd08329f960450b32218bd73a39f90f1" + +def _getUrl(url, data=None, headers=cache.DEFAULT_HEADERS.copy(), timeout=cache.cache_timeout, valid=None): + headers["Cookie"] = auth.get("karagarga.cookie") + return cache.getUrl(url, data, headers, timeout) + +def getUrlUnicode(url, timeout=cache.cache_timeout): + return cache.getUrlUnicode(url, _getUrl=_getUrl, timeout=timeout) def getData(id): data = { diff --git a/oxweb/thepiratebay.py b/oxweb/thepiratebay.py index 644e60c..f9e7a64 100644 --- a/oxweb/thepiratebay.py +++ b/oxweb/thepiratebay.py @@ -18,8 +18,7 @@ cache_timeout = 24*60*60 # cache search only for 24 hours season_episode = re.compile("S..E..", re.IGNORECASE) -def _getUrl(url, data=None, headers=cache.DEFAULT_HEADERS, timeout=cache.cache_timeout, valid=None): - headers = cache.DEFAULT_HEADERS +def _getUrl(url, data=None, headers=cache.DEFAULT_HEADERS.copy(), timeout=cache.cache_timeout, valid=None): headers['Cookie'] = 'language=en_EN' return cache.getUrl(url, data, headers, timeout)