mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 21:19:31 +01:00
games: implement fml (#122)
This commit is contained in:
parent
9cae7682a8
commit
e498a0cace
@ -6,6 +6,9 @@ import sys
|
|||||||
import os
|
import os
|
||||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
import random
|
import random
|
||||||
|
import urllib.request
|
||||||
|
import urllib.error
|
||||||
|
from xml.etree import ElementTree
|
||||||
|
|
||||||
import utils
|
import utils
|
||||||
from log import log
|
from log import log
|
||||||
@ -76,6 +79,53 @@ gameclient.add_cmd(eightball)
|
|||||||
gameclient.add_cmd(eightball, '8ball')
|
gameclient.add_cmd(eightball, '8ball')
|
||||||
gameclient.add_cmd(eightball, '8b')
|
gameclient.add_cmd(eightball, '8b')
|
||||||
|
|
||||||
|
def fml(irc, source, args):
|
||||||
|
"""[<id>]
|
||||||
|
|
||||||
|
Displays an entry from fmylife.com. If <id>
|
||||||
|
is not given, fetch a random entry from the API."""
|
||||||
|
try:
|
||||||
|
query = args[0]
|
||||||
|
except IndexError:
|
||||||
|
# Get a random FML from the API.
|
||||||
|
query = 'random'
|
||||||
|
|
||||||
|
# TODO: configurable language?
|
||||||
|
url = ('http://api.betacie.com/view/%s/nocomment'
|
||||||
|
'?key=4be9c43fc03fe&language=en' % query)
|
||||||
|
try:
|
||||||
|
data = urllib.request.urlopen(url).read()
|
||||||
|
except urllib.error as e:
|
||||||
|
reply(irc, 'Error: %s' % e)
|
||||||
|
return
|
||||||
|
|
||||||
|
tree = ElementTree.fromstring(data.decode('utf-8'))
|
||||||
|
tree = tree.find('items/item')
|
||||||
|
|
||||||
|
try:
|
||||||
|
category = tree.find('category').text
|
||||||
|
text = tree.find('text').text
|
||||||
|
fmlid = tree.attrib['id']
|
||||||
|
url = tree.find('short_url').text
|
||||||
|
except AttributeError as e:
|
||||||
|
log.debug("games.FML: Error fetching FML %s from URL %s: %s",
|
||||||
|
query, url, e)
|
||||||
|
reply(irc, "Error: That FML does not exist or there was an error "
|
||||||
|
"fetching data from the API.")
|
||||||
|
return
|
||||||
|
|
||||||
|
if not fmlid:
|
||||||
|
reply(irc, "Error: That FML does not exist.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# TODO: customizable formatting
|
||||||
|
votes = "\x02[Agreed: %s / Deserved: %s]\x02" % \
|
||||||
|
(tree.find('agree').text, tree.find('deserved').text)
|
||||||
|
s = '\x02#%s [%s]\x02: %s - %s \x02<\x0311%s\x03>\x02' % \
|
||||||
|
(fmlid, category, text, votes, url)
|
||||||
|
reply(irc, s)
|
||||||
|
gameclient.add_cmd(fml)
|
||||||
|
|
||||||
# loading
|
# loading
|
||||||
def main(irc=None):
|
def main(irc=None):
|
||||||
"""Main function, called during plugin loading at start."""
|
"""Main function, called during plugin loading at start."""
|
||||||
|
Loading…
Reference in New Issue
Block a user