python-ox/ox/web/movieposterdb.py

46 lines
1.3 KiB
Python
Raw Normal View History

2010-07-07 23:25:57 +00:00
# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-09-30 19:27:26 +00:00
from __future__ import print_function
2010-07-07 23:25:57 +00:00
import re
from ox.cache import read_url
from ox import find_re
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def get_data(id):
2010-07-07 23:25:57 +00:00
'''
2012-08-15 15:15:40 +00:00
>>> get_data('0060304')['posters'][0]
2010-07-07 23:25:57 +00:00
u'http://www.movieposterdb.com/posters/06_03/1967/0060304/l_99688_0060304_639fdd1e.jpg'
2012-08-15 15:15:40 +00:00
>>> get_data('0123456')['posters']
2010-07-07 23:25:57 +00:00
[]
'''
data = {
2012-08-15 15:15:40 +00:00
"url": get_url(id)
2010-07-07 23:25:57 +00:00
}
2012-08-15 15:15:40 +00:00
data["posters"] = get_posters(data["url"])
2010-07-07 23:25:57 +00:00
return data
2012-08-15 15:15:40 +00:00
def get_id(url):
2010-07-07 23:25:57 +00:00
return url.split("/")[-2]
2012-08-15 15:15:40 +00:00
def get_posters(url, group=True, timeout=-1):
2010-07-07 23:25:57 +00:00
posters = []
html = read_url(url, timeout=timeout, unicode=True)
2010-07-07 23:25:57 +00:00
if url in html:
if group:
results = re.compile('<a href="(http://www.movieposterdb.com/group/.+?)\??">', re.DOTALL).findall(html)
for result in results:
2012-08-15 15:15:40 +00:00
posters += get_posters(result, False)
2010-07-07 23:25:57 +00:00
results = re.compile('<a href="(http://www.movieposterdb.com/poster/.+?)">', re.DOTALL).findall(html)
for result in results:
html = read_url(result, timeout=timeout, unicode=True)
posters.append(find_re(html, '"(http://www.movieposterdb.com/posters/.+?\.jpg)"'))
2010-07-07 23:25:57 +00:00
return posters
2012-08-15 15:15:40 +00:00
def get_url(id):
2010-07-07 23:25:57 +00:00
return "http://www.movieposterdb.com/movie/%s/" % id
if __name__ == '__main__':
2014-09-30 19:27:26 +00:00
print(get_data('0060304'))
print(get_data('0133093'))