python-ox/ox/web/movieposterdb.py

46 lines
1.3 KiB
Python
Raw Permalink 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:
2024-09-11 21:52:01 +00:00
results = re.compile(r'<a href="(http://www.movieposterdb.com/group/.+?)\??">', re.DOTALL).findall(html)
2010-07-07 23:25:57 +00:00
for result in results:
2012-08-15 15:15:40 +00:00
posters += get_posters(result, False)
2024-09-11 21:52:01 +00:00
results = re.compile(r'<a href="(http://www.movieposterdb.com/poster/.+?)">', re.DOTALL).findall(html)
2010-07-07 23:25:57 +00:00
for result in results:
html = read_url(result, timeout=timeout, unicode=True)
2024-09-11 21:52:01 +00:00
posters.append(find_re(html, r'"(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'))