~josephjamesmills/+junk/scope-youtube

« back to all changes in this revision

Viewing changes to debian/scope-youtube/usr/share/unity-scope-youtube/youtubeapi.py

  • Committer: Joseph Mills
  • Date: 2012-05-23 05:36:43 UTC
  • Revision ID: josephjamesmills@gmail.com-20120523053643-mtbblru1s14hv1wh
Fixed up dbus path and added right directorys to push to in intinal make.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
# -*- coding: iso-8859-15 -*-
3
 
#
4
 
__author__='atareao'
5
 
__date__ ='$26-dic-2011'
6
 
#
7
 
# Youtube API
8
 
#
9
 
# Copyright (C) 2011 Lorenzo Carbonell
10
 
# lorenzo.carbonell.cerezo@gmail.com
11
 
#
12
 
# This program is free software: you can redistribute it and/or modify
13
 
# it under the terms of the GNU General Public License as published by
14
 
# the Free Software Foundation, either version 3 of the License, or
15
 
# (at your option) any later version.
16
 
#
17
 
# This program is distributed in the hope that it will be useful,
18
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
# GNU General Public License for more details.
21
 
#
22
 
# You should have received a copy of the GNU General Public License
23
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 
#
25
 
#
26
 
#
27
 
 
28
 
import gdata.youtube
29
 
import gdata.youtube.service
30
 
 
31
 
RECORDS_PER_PAGE = 10
32
 
 
33
 
class YouTubeService:
34
 
        
35
 
        def __init__(self):
36
 
                self.client = gdata.youtube.service.YouTubeService()
37
 
                
38
 
        def search(self,search = None, page = 1, categories = [], orderby = 'viewCount', time = 'all_time', rating = 0):
39
 
                query = gdata.youtube.service.YouTubeVideoQuery()
40
 
                if search != None:      
41
 
                        query.vq = search # the term(s) that you are searching for
42
 
                        query.orderby = orderby    # how to display the results
43
 
                        query.time = time 
44
 
                        #if rating > 0:
45
 
                        query.max_results = RECORDS_PER_PAGE        # number of results to retrieve
46
 
                        query.start_index = 1 + (page - 1) * RECORDS_PER_PAGE
47
 
                        query.racy = 'include'
48
 
                        if len(categories) > 0:
49
 
                                for category in categories:
50
 
                                        query.categories.append(category)
51
 
                        try:
52
 
                                feed = self.client.YouTubeQuery(query)
53
 
                        except Exception,e:
54
 
                                feed = self.client.GetMostViewedVideoFeed()
55
 
                else:
56
 
                        feed = self.client.GetMostViewedVideoFeed()
57
 
                results = []
58
 
                contador = 0
59
 
                for entry in feed.entry:                        
60
 
                        result = {}
61
 
                        if entry.title and entry.title.text:
62
 
                                result['title'] = entry.title.text
63
 
                        else:
64
 
                                result['title'] = ''
65
 
                        if entry.media and entry.media.thumbnail and len(entry.media.thumbnail)>0 and entry.media.thumbnail[0]:
66
 
                                result['icon'] = entry.media.thumbnail[0].url
67
 
                        else:
68
 
                                result['icon'] = None
69
 
                        if entry.link and len(entry.link)>0 and entry.link[0].href:
70
 
                                result['href'] = entry.link[0].href
71
 
                        else:
72
 
                                result['href'] = None
73
 
                        if entry.summary and entry.summary.text:
74
 
                                result['summary'] = entry.summary.text
75
 
                        else:
76
 
                                result['summary'] = ''
77
 
                        if entry.rating and entry.rating.average and entry.rating.average:                              
78
 
                                result['rating'] = int(float(entry.rating.average))
79
 
                                result['summary'] = 'Rating: %s'% result['rating']
80
 
                        else:
81
 
                                result['rating'] = 0
82
 
                        if result['rating']>=rating:
83
 
                                results.append(result)
84
 
                                contador += 1
85
 
                                if contador >= RECORDS_PER_PAGE:
86
 
                                        return results
87
 
                return results
88
 
                
89
 
 
90
 
if __name__ == '__main__':
91
 
        yt = YouTubeService()
92
 
        categories = []
93
 
        categories.append('Animals')
94
 
        results = yt.search(search = 'Prueba', categories = categories, rating = 4)
95
 
        for result in results:
96
 
                print '########################################################'
97
 
                print result['title']
98
 
                print result['icon']
99
 
                print result['href']
100
 
                print result['summary']
101
 
                print result['rating']
102
 
        '''
103
 
        client = gdata.youtube.service.YouTubeService()
104
 
        #gdata.alt.appengine.run_on_appengine(client)
105
 
        query = gdata.youtube.service.YouTubeVideoQuery()
106
 
        
107
 
        #query.vq = 'nasa' # the term(s) that you are searching for
108
 
        query.orderby = 'viewCount'    # how to display the results
109
 
        query.max_results = '25'        # number of results to retrieve
110
 
        #query.category = 'Tech'#'Science &amp; Technology'
111
 
        #query.categories.append('Travel')
112
 
        query.casa = ''
113
 
 
114
 
        feed = client.YouTubeQuery(query)
115
 
        for entry in feed.entry:
116
 
                print '#######################################################'
117
 
                print entry.link[0].href
118
 
                print entry.title.text
119
 
                print entry.published.text
120
 
 
121
 
                if entry.rating:
122
 
                        print entry.rating.average
123
 
                        print entry.rating.num_raters
124
 
                print entry.media.category[0]
125
 
                print entry.media.thumbnail[0].url
126
 
                if entry.recorded:
127
 
                        print entry.recorded.text
128
 
                if entry.rights:
129
 
                        print entry.rights.text
130
 
                if entry.source:
131
 
                        print entry.source.text
132
 
                if entry.statistics:
133
 
                        print entry.statistics.text
134
 
                if entry.summary:
135
 
                        print entry.summary.text
136
 
                if entry.text:
137
 
                        print entry.text.text
138
 
                if entry.title:
139
 
                        print entry.title.text
140
 
                if entry.updated:
141
 
                        print entry.updated.text
142
 
        '''
143
 
        exit(0)