summaryrefslogtreecommitdiff
path: root/modules/anidb.py
blob: 83e48ea471a4044e13ec94040ffc226c2a952aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
info = {
	'author': 'Jon Bergli Heier',
	'title': 'anidb',
	'description': 'Fetches anime info from anidb.net.',
}

import urllib2, gzip, os, time
from pyPgSQL import PgSQL
from lxml import etree
import pyanidb
import ConfigParser
import marshal

anidb_cfg = {}
cp = ConfigParser.ConfigParser()
cp.read(os.path.expanduser('~/.pyanidb.conf'))
for option in ('username', 'password'):
	anidb_cfg[option] = cp.get('pyanidb', option)

class Module:
	def __init__(self, bot):
		self.irc = bot
		self.db = PgSQL.connect(database = 'fot')
		if self.irc:
			self.irc.register_keyword('!anidb', self)

	def get_aid(self, msg, short = False):
		if msg.isdigit():
			return [(int(msg), 'N/A')]
		search = '%'.join(msg.split())
		if not search:
			return
		if not short:
			search = '%%%s%%' % search
		cur = self.db.cursor()
		if short:
			cur.execute('select * from anidb where aid in (select distinct aid from anidb where title ~~* %s and type in (2, 3)) and type = 1', (search,))
		else:
			cur.execute('select * from anidb where aid in (select distinct aid from anidb where title ~~* %s) and type = 1', (search,))
		r = cur.fetchall()
		cur.close()
		return [(r[0][0], r[0][3])] if len(r) == 1 else [(x[0], x[3]) for x in r]

	def get_info(self, aid):
		cachepath = os.path.expanduser('~/.fotanidbcache/a%d.dat' % aid)
		if os.access(cachepath, os.F_OK | os.R_OK) and time.time() - os.stat(cachepath).st_mtime < 60*60*24*7:
			cache = open(cachepath, 'r').read()
			data = marshal.loads(cache)
		else:
			amask = '33a0c0f0000080'
			try:
				anidb = pyanidb.AniDB(anidb_cfg['username'], anidb_cfg['password'])
				anidb.auth()
				data = anidb.get_anime(aid = aid, amask = amask)
				anidb.logout()
				open(cachepath, 'w').write(marshal.dumps(data))
			except:
				return 'Failed to get data from anidb'
		year, type, catlist, catweight, romaji, english, epcount, normalep, rating, ratingcount, temprating, tempcount, specialep = data
		if '-' in year:
			syear, eyear = [int(x) for x in year.split('-')]
			if syear == eyear:
				year = syear
		catlist = catlist.split(',')
		catweight = catweight.split(',')
		try:
			catn = max(catweight.index(str(int(catweight[0])-1)), 10)
		except:
			catn = 10
		rating = float(rating) / 100
		temprating = float(temprating) / 100

		epcount = int(epcount)
		normalep = int(normalep)
		specialep = int(specialep)

		catl = ', '.join(catlist[:catn] + (['+%d more' % (len(catlist)-catn)] if len(catlist) > catn else []))
		s = '%s%s is: %s, year %s, %d%s%s eps%s, cats: %s | rating: %.2f (%s), temp: %.2f (%s) | http://anidb.net/a%d' % (
				romaji,
				' aka. %s' % english if english else '',
				type,
				year,
				normalep,
				'/%d' % epcount if epcount > 0 and normalep != epcount else '',
				'+%d' % specialep if specialep > 0 else '',
				' (ongoing)' if epcount == 0 or normalep < epcount else '',
				catl or '(None)',
				rating,
				ratingcount,
				temprating,
				tempcount,
				aid)
		return s.encode('utf8')

	def get_anime(self, msg):
		short = msg.startswith('-s')
		if short:
			msg = ' '.join(msg.split()[1:])
			if not len(msg):
				return 'Usage: !anidb [-s] search|aid'
		aid = self.get_aid(msg, short)
		if len(aid) == 1:
			return self.get_info(aid[0][0])
		else:
			if len(aid) == 0:
				return 'anidb: No results'
			else:
				return 'anidb: %s%s' % ('%d/%d results: ' % (5, len(aid)) if len(aid) > 5 else '', ', '.join(['%s (%d)' % (x[1], x[0]) for x in aid[:5]]))

	def keyword(self, nick, channel, kw, msg):
		target = channel if not channel == self.irc.nickname else nick.split('!')[0]
		args = msg.split()
		if len(args) == 0:
			self.irc.msg(target, 'Usage: !anidb [-s] search|aid')
			return
		info = self.get_anime(' '.join(args))
		if info:
			self.irc.msg(target, info)

if __name__ == '__main__':
	import sys
	m = Module(None)
	print m.get_anime(' '.join(sys.argv[1:]))