From e4e679843346dc45ff8b571870136ee0e70cdfe2 Mon Sep 17 00:00:00 2001
From: j <0x006A@0x2620.org>
Date: Fri, 3 Sep 2010 23:19:19 +0200
Subject: [PATCH] make sure tests work again, fix to32
---
README | 2 +-
ox/format.py | 70 +++++++++++++++++++++++++++++++++++++-----
ox/torrent/__init__.py | 8 +++--
ox/web/criterion.py | 9 +++---
ox/web/dailymotion.py | 42 ++++++++++++-------------
ox/web/imdb.py | 16 +++++++---
ox/web/impawards.py | 6 ++--
setup.py | 2 +-
8 files changed, 109 insertions(+), 46 deletions(-)
diff --git a/README b/README
index 19f317f..f3936d0 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-python-ox some tools to build tools
+python-ox - the web in a dict
Depends:
python2.5
diff --git a/ox/format.py b/ox/format.py
index 4e439ad..8814852 100644
--- a/ox/format.py
+++ b/ox/format.py
@@ -2,21 +2,75 @@
# vi:si:et:sw=4:sts=4:ts=4
import math
import re
+import string
def to32(q):
"""
Converts an integer to base 32
We exclude 4 of the 26 letters: I L O U.
http://www.crockford.com/wrmg/base32.html
+ >>> to32(0)
+ '0'
+
+ >>> to32(10)
+ 'A'
+ >>> to32(11)
+ 'B'
+ >>> to32(12)
+ 'C'
+ >>> to32(13)
+ 'D'
+ >>> to32(14)
+ 'E'
+ >>> to32(15)
+ 'F'
+ >>> to32(16)
+ 'G'
+ >>> to32(17)
+ 'H'
+ >>> to32(18)
+ 'J'
+ >>> to32(19)
+ 'K'
+ >>> to32(20)
+ 'M'
+ >>> to32(21)
+ 'N'
+ >>> to32(22)
+ 'P'
+ >>> to32(23)
+ 'Q'
+ >>> to32(24)
+ 'R'
+ >>> to32(25)
+ 'S'
+ >>> to32(26)
+ 'T'
+ >>> to32(27)
+ 'V'
+ >>> to32(28)
+ 'W'
+ >>> to32(29)
+ 'X'
+ >>> to32(30)
+ 'Y'
+ >>> to32(31)
+ 'Z'
+ >>> to32(32)
+ '10'
+ >>> to32(33)
+ '11'
+ >>> to32(34)
+ '12'
>>> to32(35)
'13'
>>> to32(119292)
- '3MgV'
+ '3MfW'
>>> to32(939387374)
- 'wZwTgD'
- >>> to32(0)
- '0'
+ 'vZvTfE'
+ >>> from32(to32(939387374))
+ 939387374
>>> to32(-393)
Traceback (most recent call last):
...
@@ -24,7 +78,7 @@ def to32(q):
"""
if q < 0: raise ValueError, "must supply a positive integer"
- letters = "0123456789ACBEDGFHKJMNQPSRTWVYXZ"
+ letters = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
converted = []
upper = True
while q != 0:
@@ -44,9 +98,9 @@ def from32(q):
We exclude 4 of the 26 letters: I L O U.
http://www.crockford.com/wrmg/base32.html
- >>> form32(to32(35))
+ >>> from32(to32(35))
35
- >>> form32(to32(119292))
+ >>> from32(to32(119292))
119292
>>> from32(to32(0))
0
@@ -88,7 +142,7 @@ def from32(q):
'Y': 30,
'Z': 31,
}
- base32 = "0123456789ACBEDGFHKJMNQPSRTWVYXZ"
+ base32 = ('0123456789' +string.ascii_uppercase)[:32]
q = q.replace('-','')
q = ''.join([base32[_32map[i.upper()]] for i in q])
return int(q, 32)
diff --git a/ox/torrent/__init__.py b/ox/torrent/__init__.py
index 2ca89d1..dd3c424 100644
--- a/ox/torrent/__init__.py
+++ b/ox/torrent/__init__.py
@@ -3,7 +3,7 @@
# GPL 2007-2009
from threading import Event
-import hashlib
+from hashlib import sha1
import os
from bencode import bencode, bdecode
@@ -21,7 +21,7 @@ def getInfoHash(torrentFile):
metainfo_file = open(torrentFile, 'rb')
metainfo = bdecode(metainfo_file.read())
info = metainfo['info']
- return hashlib.sha1(bencode(info)).hexdigest()
+ return sha1(bencode(info)).hexdigest()
def getTorrentInfoFromFile(torrentFile):
f = open(torrentFile, 'rb')
@@ -32,6 +32,8 @@ def getTorrentInfoFromFile(torrentFile):
return tinfo
def getTorrentInfo(data):
+ from bencode import bencode
+
"Returns Torrent Info from torrent file"
tinfo = {}
metainfo = bdecode(data)
@@ -52,7 +54,7 @@ def getTorrentInfo(data):
if key != 'info':
tinfo[key] = metainfo[key]
tinfo['size'] = file_length
- tinfo['hash'] = hashlib.sha1(bencode(info)).hexdigest()
+ tinfo['hash'] = sha1(bencode(info)).hexdigest()
tinfo['announce'] = metainfo['announce']
return tinfo
diff --git a/ox/web/criterion.py b/ox/web/criterion.py
index 4f48930..e3f85ea 100644
--- a/ox/web/criterion.py
+++ b/ox/web/criterion.py
@@ -18,13 +18,13 @@ def getUrl(id):
def getData(id):
'''
>>> getData('1333')['imdbId']
- '0060304'
+ u'0060304'
>>> getData('236')['posters'][0]
- 'http://criterion_production.s3.amazonaws.com/release_images/1586/ThirdManReplace.jpg'
+ u'http://criterion_production.s3.amazonaws.com/release_images/1586/ThirdManReplace.jpg'
>>> getData('786')['posters'][0]
- 'http://criterion_production.s3.amazonaws.com/product_images/185/343_box_348x490.jpg'
+ u'http://criterion_production.s3.amazonaws.com/product_images/185/343_box_348x490.jpg'
'''
data = {
"url": getUrl(id)
@@ -50,7 +50,7 @@ def getData(id):
data["posters"] = [result]
else:
html_ = readUrlUnicode(result)
- result = findRe(html_, "(.*?)" % id)
+ result = findRe(html_, '(.*?)' % id)
result = findRe(result, "src=\"(.*?)\"")
data["posters"] = [result.replace("_w100", "")]
result = findRe(html, ">> getVideoUrl('http://www.dailymotion.com/relevance/search/priere%2Bpour%2Brefuznik/video/x3opar_priere-pour-refuznik-1-jeanluc-goda_shortfilms').split('?key')[0]
- 'http://www.dailymotion.com/get/16/320x240/flv/6191379.flv'
-
- >>> getVideoUrl('http://www.dailymotion.com/relevance/search/priere%2Bpour%2Brefuznik/video/x3ou94_priere-pour-refuznik-2-jeanluc-goda_shortfilms').split('?key')[0]
- 'http://www.dailymotion.com/get/15/320x240/flv/6197800.flv'
- '''
- data = readUrl(url)
- video = re.compile('''video", "(.*?)"''').findall(data)
- for v in video:
- v = unquote(v).split('@@')[0]
- return "http://www.dailymotion.com" + v
- return ''
-
+# vi:si:et:sw=4:sts=4:ts=4
+import re
+from urllib import unquote
+from ox.cache import readUrl
+
+
+def getVideoUrl(url):
+ '''
+ >>> getVideoUrl('http://www.dailymotion.com/relevance/search/priere%2Bpour%2Brefuznik/video/x3opar_priere-pour-refuznik-1-jeanluc-goda_shortfilms').split('?auth')[0]
+ 'http://www.dailymotion.com/cdn/FLV-320x240/video/x3opar_priere-pour-refuznik-1-jean-luc-god_shortfilms.flv'
+
+ >>> getVideoUrl('http://www.dailymotion.com/relevance/search/priere%2Bpour%2Brefuznik/video/x3ou94_priere-pour-refuznik-2-jeanluc-goda_shortfilms').split('?auth')[0]
+ 'http://www.dailymotion.com/cdn/FLV-320x240/video/x3ou94_priere-pour-refuznik-2-jean-luc-god_shortfilms.flv'
+ '''
+ data = readUrl(url)
+ video = re.compile('''video", "(.*?)"''').findall(data)
+ for v in video:
+ v = unquote(v).split('@@')[0]
+ return v
+ return ''
+
diff --git a/ox/web/imdb.py b/ox/web/imdb.py
index 67f3a4c..127de82 100644
--- a/ox/web/imdb.py
+++ b/ox/web/imdb.py
@@ -249,14 +249,20 @@ class ImdbCombined(Imdb):
def getMovieId(title, director='', year=''):
'''
>>> getMovieId('The Matrix')
- '0133093'
+ u'0133093'
+
+ >>> getMovieId('2 or 3 Things I Know About Her', 'Jean-Luc Godard')
+ u'0060304'
+
+ >>> getMovieId('2 or 3 Things I Know About Her', 'Jean-Luc Godard', '1967')
+ u'0060304'
'''
- if year:
- title = "%s (%s)" % (title, year)
if director:
- query = 'site:imdb.com %s "%s"' % (director, title)
+ query = 'site:imdb.com %s "%s" ' % (director, title)
else:
- query = 'site:imdb.com "%s"' % title
+ query = 'site:imdb.com "%s" ' % title
+ if year:
+ query += year
for (name, url, desc) in google.find(query, 5, timeout=-1):
if url.startswith('http://www.imdb.com/title/tt'):
return url[28:35]
diff --git a/ox/web/impawards.py b/ox/web/impawards.py
index ef15cc5..08fe984 100644
--- a/ox/web/impawards.py
+++ b/ox/web/impawards.py
@@ -14,7 +14,7 @@ def getData(id):
u'0102926'
>>> getData('1991/silence_of_the_lambs')['posters'][0]
- u'http://www.impawards.com/1991/posters/silence_of_the_lambs_ver1_xlg.jpg'
+ u'http://www.impawards.com/1991/posters/silence_of_the_lambs_ver1.jpg'
>>> getData('1991/silence_of_the_lambs')['url']
u'http://www.impawards.com/1991/silence_of_the_lambs_ver1.html'
@@ -77,10 +77,10 @@ def getIdsByPage(page):
return set(ids)
def getUrl(id):
- url = "http://www.impawards.com/%s.html" % id
+ url = u"http://www.impawards.com/%s.html" % id
html = readUrlUnicode(url)
if findRe(html, "No Movie Posters on This Page"):
- url = "http://www.impawards.com/%s_ver1.html" % id
+ url = u"http://www.impawards.com/%s_ver1.html" % id
return url
if __name__ == '__main__':
diff --git a/setup.py b/setup.py
index a2c4454..cd81262 100644
--- a/setup.py
+++ b/setup.py
@@ -13,7 +13,7 @@ def get_bzr_version():
setup(
name="ox",
version="2.0.%s" % get_bzr_version() ,
- description="python-ox some tools to build tools",
+ description="python-ox - the web in a dict",
author="0x",
author_email="code@0x2620.org",
url="http://code.0x2620.org/python-ox",