aboutsummaryrefslogtreecommitdiff
path: root/glifestream/apis/youtube.py
blob: 509e38590eeaea8374ff7f69a3c1e060c89eab91 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#  gLifestream Copyright (C) 2009, 2010, 2011, 2013, 2015 Wojciech Polak
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 3 of the License, or (at your
#  option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime
from django.utils.translation import ugettext as _
from glifestream.stream import media
from glifestream.stream.models import Entry
from glifestream.utils import httpclient
from glifestream.utils.time import mtime, now


class API:
    name = 'YouTube API v3'
    limit_sec = 3600
    playlist_types = {}

    def __init__(self, service, verbose=0, force_overwrite=False):
        self.service = service
        self.verbose = verbose
        self.force_overwrite = force_overwrite
        if self.verbose:
            print('%s: %s' % (self.name, self.service))

    def get_urls(self):
        if self.service.url.startswith('http://') or \
           self.service.url.startswith('https://'):
            return (self.service.url,)
        else:
            urls = []
            if ':' in self.service.url:
                apikey, playlists = self.service.url.split(':')
                for playlist in playlists.split(','):
                    if '#' in playlist:
                        playlist, kind = playlist.split('#')
                    else:
                        kind = 'video'
                    url = ('https://www.googleapis.com/youtube/v3/'
                           'playlistItems?part=snippet,contentDetails,status&'
                           'playlistId=%s&'
                           'maxResults=25&'
                           'key=%s' % (playlist, apikey))
                    self.playlist_types[url] = kind
                    urls.append(url)
            return urls

    def run(self):
        for url in self.get_urls():
            try:
                self.fetch(url)
            except:
                pass

    def fetch(self, url):
        try:
            r = httpclient.get(url)
            if r.status_code == 200:
                self.json = r.json()
                self.service.last_checked = now()
                self.service.save()
                self.process(url)
            elif self.verbose:
                print('%s (%d) HTTP: %s' % (self.service.api,
                                            self.service.id, r.reason))
        except Exception as e:
            if self.verbose:
                import sys
                import traceback
                print('%s (%d) Exception: %s' % (self.service.api,
                                                 self.service.id, e))
                traceback.print_exc(file=sys.stdout)

    def process(self, url):
        for ent in self.json.get('items', ()):
            snippet = ent.get('snippet', {})
            date = snippet['publishedAt'][:10]

            vid = ent['contentDetails']['videoId']
            if self.playlist_types[url] == 'favorite':
                guid = 'tag:youtube.com,2008:favorite:%s' % ent.get('id')
            else:
                guid = 'tag:youtube.com,2008:video:%s' % vid

            t = datetime.datetime.strptime(snippet['publishedAt'],
                                           '%Y-%m-%dT%H:%M:%S.000Z')

            if self.verbose:
                print("ID: %s" % guid)
            try:
                e = Entry.objects.get(service=self.service, guid=guid)
                if not self.force_overwrite and e.date_updated \
                   and mtime(t.timetuple()) <= e.date_updated:
                    continue
                if e.protected:
                    continue
            except Entry.DoesNotExist:
                e = Entry(service=self.service, guid=guid)

            e.title = snippet['title']
            e.link = 'https://www.youtube.com/watch?v=%s' % vid
            e.date_published = t
            e.date_updated = t
            e.author_name = snippet['channelTitle']

            if vid and 'thumbnails' in snippet:
                tn = None
                if 'medium' in snippet['thumbnails']:
                    tn = snippet['thumbnails']['medium']
                    tn['width'], tn['height'] = 320, 180
                elif 'high' in snippet['thumbnails']:
                    tn = snippet['thumbnails']['high']
                    tn['width'], tn['height'] = 200, 150
                if not tn:
                    tn = snippet['thumbnails']['default']
                    tn['width'], tn['height'] = 200, 150

                if self.service.public:
                    tn['url'] = media.save_image(tn['url'], downscale=True,
                                                 size=(tn['width'], tn['height']))

                e.content = """<table class="vc"><tr><td><div id="youtube-%s" class="play-video"><a href="%s" rel="nofollow"><img src="%s" width="%s" height="%s" alt="YouTube Video" /></a><div class="playbutton"></div></div></td></tr></table>""" % (
                    vid, e.link, tn['url'], tn['width'], tn['height'])
            else:
                e.content = '<a href="%s">%s</a>' % (e.link, e.title)

            try:
                e.save()
            except Exception as exc:
                print(exc)


def filter_title(entry):
    if 'favorite' in entry.guid:
        return _('Favorited %s') % ('<em>' + entry.title + '</em>')
    else:
        return _('Published %s') % ('<em>' + entry.title + '</em>')

Return to:

Send suggestions and report system problems to the System administrator.