~bnrubin/supytube/main

« back to all changes in this revision

Viewing changes to plugin.py

  • Committer: Benjamin Rubin
  • Date: 2007-09-09 17:51:54 UTC
  • Revision ID: bnrubin@gmail.com-20070909175154-fouiz6g50z4xl0vc
- Updated code for new gdata based API (http://code.google.com/apis/youtube/overview.html)
- Removed Developer Id config key due to API change
+ Added duration parsing

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
import supybot.conf as conf
39
39
import supybot.log as log
40
40
from urlparse import urlparse
 
41
from datetime import timedelta
41
42
import urllib
42
43
 
43
44
import xml.sax
46
47
    def __init__(self):
47
48
        self.inTitle = 0
48
49
        self.inAuthor = 0
49
 
        self.inRating = 0
50
 
        self.inRatingCount = 0
51
 
        self.inViews = 0
 
50
        self.inName = 0
52
51
        self.title = ''
53
52
        self.author = ''
54
53
        self.rating = ''
55
54
        self.rating_count = ''
56
55
        self.views = ''
 
56
        self.duration = ''
57
57
       
58
58
        
59
59
    def startElement(self, name, attributes):
61
61
            self.title = ''
62
62
            self.inTitle = 1
63
63
        if(name == 'author'):
64
 
            self.author = ""
 
64
            self.author = ''
65
65
            self.inAuthor = 1
66
 
        if(name == 'rating_avg'):
67
 
            self.rating = ''
68
 
            self.inRating = 1
69
 
        if(name == 'rating_count'):
70
 
            self.rating_count = ''
 
66
        if(name == 'name'):
 
67
            self.inName = 1
 
68
        if(name == 'gd:rating'):
 
69
            self.rating_count = attributes.getValue('numRaters')
 
70
          
 
71
            # Compute percentage rating based on the average rating and the maximum rating 
 
72
            self.rating = ( float(attributes.getValue('average')) / float(attributes.getValue('max')) ) * 100
71
73
            self.inRatingCount = 1
72
 
        if(name == 'view_count'):
73
 
            self.views = ''
 
74
        if(name == 'yt:statistics'):
 
75
            self.views = attributes.getValue('viewCount')
74
76
            self.inViews = 1
 
77
        if(name == 'yt:duration'):
 
78
            self.duration = timedelta(seconds = float(attributes.getValue('seconds')))
75
79
            
76
80
    def characters(self,data):
77
81
        if(self.inTitle):
78
82
            self.title += data
79
 
        if(self.inAuthor):
 
83
        if(self.inAuthor and self.inName):
80
84
            self.author += data
81
 
        if(self.inRating):
82
 
            self.rating += data
83
 
        if(self.inRatingCount):
84
 
            self.rating_count += data
85
 
        if(self.inViews):
86
 
            self.views += data
 
85
 
87
86
    def endElement(self,name):
88
87
        if(name == 'title'):
89
88
            self.inTitle=0
90
89
        if(name == 'author'):
91
90
            self.inAuthor = 0
92
 
        if(name == 'rating_avg'):
93
 
            self.inRating = 0
94
 
        if(name == 'rating_count'):
95
 
            self.inRatingCount = 0
96
 
            self.rating = (float(self.rating)/5)*100
97
 
        if(name == 'view_count'):
98
 
            self.inViews = 0          
 
91
        if(name == 'name'):
 
92
            self.inName = 0       
99
93
 
100
94
class Supytube(callbacks.Plugin):
101
95
    """Add the help for "@plugin help Supytube" here
103
97
    threaded = True
104
98
 
105
99
    def doPrivmsg(self, irc, msg):
106
 
        if(self.registryValue('enable',msg.args[0])):
107
 
            # If this is a youtube link, commence lookup
108
 
            if(msg.args[1].find("youtube") != -1):
109
 
                for word in msg.args[1].split(' '):
110
 
                    if(word.find("youtube") != -1):
111
 
                        videoid = urlparse(word)[4].split('=')[1].split('&')[0]
112
 
                        devid = self.registryValue('devid')
113
 
                        #log.critical(videoid)
114
 
                        #log.critical(devid)
115
 
                        f = urllib.urlopen('http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=%s&video_id=%s' % (devid,videoid))
116
 
                        parser = xml.sax.make_parser()
117
 
                        handler = YoutubeHandler()
118
 
                        parser.setContentHandler(handler)
119
 
                        parser.parse(f)
120
 
                        #log.critical(handler.title)
121
 
                        irc.queueMsg(ircmsgs.privmsg(msg.args[0],'Title: %s, Views: %s, Rating: %s%%' % (ircutils.bold(handler.title), ircutils.bold(handler.views),ircutils.bold(handler.rating))))
 
100
            if(self.registryValue('enable',msg.args[0])):
 
101
                # If this is a youtube link, commence lookup
 
102
                if(msg.args[1].find("youtube") != -1):
 
103
                    for word in msg.args[1].split(' '):
 
104
                        if(word.find("youtube") != -1):
 
105
                            videoid = urlparse(word)[4].split('=')[1].split('&')[0]
 
106
 
 
107
                            f = urllib.urlopen('http://gdata.youtube.com/feeds/videos/%s' % (videoid))
 
108
                            parser = xml.sax.make_parser()
 
109
                            handler = YoutubeHandler()
 
110
                            parser.setContentHandler(handler)
 
111
                            parser.parse(f)
 
112
                            
 
113
                            #log.critical('Title: %s' % handler.title)
 
114
                            #log.critical('Author: %s' % handler.author)
 
115
                            #log.critical('Rating: %s' % handler.rating)
 
116
                            #log.critical('Views: %s' % handler.views)
 
117
                            #log.critical('Rating Count: %s' % handler.rating_count)
 
118
                            #log.critical('Duration: %s' % handler.duration)
 
119
                            
 
120
                            irc.queueMsg(ircmsgs.privmsg(msg.args[0],'Title: %s, Views: %s, Rating: %s%%' % (ircutils.bold(handler.title), ircutils.bold(handler.views),ircutils.bold(handler.rating))))
122
121
                
123
122
                
124
123
            
125
124
            
126
125
 
127
126
Class = Supytube
128
 
 
129
 
 
130
 
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: