phenny-1/modules/imdb.py

51 lines
1.3 KiB
Python
Raw Normal View History

2012-05-22 22:47:36 -04:00
#!/usr/bin/env python
"""
imdb.py - Phenny Web Search Module
Copyright 2012, Randy Nance, randynance.info
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
2017-02-11 01:21:04 -05:00
import re
2012-05-22 22:47:36 -04:00
import web
2017-02-11 01:21:04 -05:00
r_imdb_find = re.compile(r'href="/title/(.*?)/')
r_imdb_details = re.compile(r'<title>(.*?) \((.*?)\) .*?name="description" content="(.*?)"')
def imdb_search(query):
2013-10-05 19:20:37 -04:00
query = query.replace('!', '')
query = web.quote(query)
2017-02-11 01:21:04 -05:00
uri = 'http://imdb.com/find?q=%s' % query
bytes = web.get(uri)
m = r_imdb_find.search(bytes)
if not m: return m
ID = web.decode(m.group(1))
uri = 'http://imdb.com/title/%s' % ID
2013-10-05 19:20:37 -04:00
bytes = web.get(uri)
2017-02-11 01:21:04 -05:00
bytes = bytes.replace('\n', '')
info = r_imdb_details.search(bytes)
info = {'Title': info.group(1), 'Year': info.group(2), 'Plot': info.group(3), 'imdbID': ID}
return info
2013-10-05 19:20:37 -04:00
2012-05-22 22:47:36 -04:00
def imdb(phenny, input):
2017-02-11 01:21:04 -05:00
""".imdb <movie> - Find a link to a movie on IMDb."""
2013-10-05 19:20:37 -04:00
query = input.group(2)
if not query:
return phenny.say('.imdb what?')
m = imdb_search(query)
2017-02-11 01:21:04 -05:00
if m:
2013-10-05 19:20:37 -04:00
phenny.say('{0} ({1}): {2} http://imdb.com/title/{3}'.format(
m['Title'],
m['Year'],
m['Plot'],
m['imdbID']))
2017-02-11 01:21:04 -05:00
else:
phenny.reply("No results found for '%s'." % query)
2012-05-22 22:47:36 -04:00
imdb.commands = ['imdb']
2013-11-28 22:23:32 -05:00
imdb.example = '.imdb Promethius'