youtube.py and xml.etree.ElementTree -> lxml.etree
This commit is contained in:
parent
6467e67402
commit
1000f6ede3
|
@ -4,19 +4,19 @@ retrieves most recent tweets
|
|||
"""
|
||||
|
||||
import urllib
|
||||
from xml.etree import ElementTree
|
||||
from lxml import etree
|
||||
|
||||
import hook
|
||||
|
||||
@hook.command
|
||||
def twitter(bot, input):
|
||||
'''.twitter <user> - gets most recent tweet from <user>'''
|
||||
".twitter <user> - gets most recent tweet from <user>"
|
||||
if not input.inp.strip():
|
||||
return twitter.__doc__
|
||||
|
||||
url = "http://twitter.com/statuses/user_timeline/%s.xml?count=1" \
|
||||
% urllib.quote(input.inp)
|
||||
tweet = ElementTree.parse(urllib.urlopen(url))
|
||||
tweet = etree.parse(url)
|
||||
|
||||
if tweet.find('error') is not None:
|
||||
return "can't find that username"
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"weather, thanks to google"
|
||||
|
||||
import urllib
|
||||
from xml.etree import ElementTree
|
||||
from lxml import etree
|
||||
|
||||
import hook
|
||||
|
||||
|
@ -15,7 +15,7 @@ def weather(bot, input):
|
|||
|
||||
data = urllib.urlencode({'weather':input.inp.encode('utf-8')})
|
||||
url = 'http://www.google.com/ig/api?' + data
|
||||
w = ElementTree.parse(urllib.urlopen(url)).find('weather')
|
||||
w = etree.parse(url).find('weather')
|
||||
|
||||
if w.find('problem_cause') is not None:
|
||||
return "Couldn't fetch weather data for '%s', try using a zip or " \
|
||||
|
|
|
@ -1,17 +1,45 @@
|
|||
import urllib
|
||||
from xml.etree import ElementTree
|
||||
import re
|
||||
from lxml import etree
|
||||
import locale
|
||||
|
||||
import hook
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "")
|
||||
|
||||
def ytdata(id):
|
||||
url = 'http://gdata.youtube.com/feeds/api/videos/' + idt
|
||||
print url
|
||||
data = urllib.urlopen(url).read()
|
||||
if len(data) < 50: # it's some error message; ignore
|
||||
print data
|
||||
return
|
||||
global x
|
||||
def
|
||||
x = ElementTree.XML(data)
|
||||
url = 'http://gdata.youtube.com/feeds/api/videos/' + id
|
||||
x = etree.parse(url)
|
||||
|
||||
# I can't figure out how to deal with schemas/namespaces properly :(
|
||||
yt = '{http://gdata.youtube.com/schemas/2007}'
|
||||
media = '{http://search.yahoo.com/mrss/}'
|
||||
|
||||
rating = x.find('{http://schemas.google.com/g/2005}rating')
|
||||
data = dict(rating.items())
|
||||
data['title'] = x.find('{http://www.w3.org/2005/Atom}title').text
|
||||
data['views'] = locale.format('%d', int(x.find(yt + 'statistics').get(
|
||||
'viewCount')), 1)
|
||||
length = int(x.find(media + 'group/' + yt + 'duration').get('seconds'))
|
||||
data['length'] = ''
|
||||
if length / 3600: # > 1 hour
|
||||
data['length'] += str(length/3600) + 'h '
|
||||
if length / 60: # > 1 minute
|
||||
data['length'] += str(length/60 % 60) + 'm '
|
||||
data['length'] += "%ds" % (length % 60)
|
||||
|
||||
return data
|
||||
|
||||
youtube_re = re.compile(r'.*youtube.*v=([-_a-z0-9]+)', flags=re.IGNORECASE)
|
||||
|
||||
@hook.command(hook=r'(.*)', prefix=False)
|
||||
def youtube(inp):
|
||||
m = youtube_re.match(inp)
|
||||
if m:
|
||||
data = ytdata(m.group(1))
|
||||
return '\x02%(title)s\x02 - rated \x02%(average)s/%(max)s\x02 ' \
|
||||
'(%(numRaters)s) - views \x02%(views)s\x02 - length \x02' \
|
||||
'%(length)s\x02' % data
|
||||
|
||||
if __name__ == '__main__':
|
||||
ytdata('the0KZLEacs')
|
||||
print ytdata('the0KZLEacs')
|
||||
print ytdata('Ct8nZ6eTTiY')
|
||||
|
|
Loading…
Reference in New Issue