aboutsummaryrefslogtreecommitdiff
path: root/worker.py
blob: 69a6fea49adb866365a9c9a976e6e889c5c55c7e (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#  gLifestream Copyright (C) 2009, 2010, 2014 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 os
import sys
import time
import datetime
import getopt

try:
    import workerpool
except ImportError:
    workerpool = None

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'glifestream.settings'

from django.conf import settings
from glifestream.utils.time import unixnow
from glifestream.stream.models import Service, Entry, Favorite
from glifestream.stream import media, pshb

if workerpool:
    class WorkerJob (workerpool.Job):
        def __init__(self, fn):
            self.fn = fn

        def run(self):
            self.fn()


def run():
    verbose = 0
    list = False
    force_check = False
    force_overwrite = False
    list_old = False
    delete_old = False
    only_inactive = False
    thumbs = False
    pshb_cmd = False
    fs = {}

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:a:lvf',
                                   ['id=',
                                    'api=',
                                    'list',
                                    'verbose',
                                    'force-check',
                                    'force-overwrite',
                                    'delete-old=',
                                    'list-old=',
                                    'only-inactive',
                                    'thumbs-list-orphans',
                                    'thumbs-delete-orphans',
                                    'pshb=',
                                    'email2post',
                                    'init-files-dirs'])
        for o, arg in opts:
            if o in ('-a', '--api'):
                fs['api'] = arg
            elif o in ('-i', '--id'):
                fs['id'] = arg
            elif o in ('-l', '--list'):
                list = True
            elif o in ('-v', '--verbose'):
                verbose += 1
            elif o in ('-f', '--force-check'):
                force_check = True
            elif o == '--force-overwrite':
                force_overwrite = True
            elif o == '--list-old':
                list_old = int(arg)
            elif o == '--delete-old':
                delete_old = int(arg)
            elif o == '--only-inactive':
                only_inactive = True
            elif o == '--thumbs-list-orphans':
                thumbs = 'list-orphans'
            elif o == '--thumbs-delete-orphans':
                thumbs = 'delete-orphans'
            elif o == '--pshb':
                pshb_cmd = arg
            elif o == '--email2post':
                sys.exit(email2post())
            elif o == '--init-files-dirs':
                sys.exit(init_files_dirs())
    except getopt.GetoptError:
        print "Usage: %s [OPTION...]" % sys.argv[0]
        print """%s -- gLifestream worker

  -a, --api=NAME               API name of services to update
  -i, --id=ID                  ID of the service to update
  -l, --list                   List service IDs
  -f, --force-check            Force service check for updates
      --force-overwrite        Force overwriting unmodified entries
      --list-old=DAYS          List entries older than DAYS
      --delete-old=DAYS        Delete entries older than DAYS
      --only-inactive          Match only inactive entries (hidden)
      --thumbs-list-orphans    List orphaned thumbnails
      --thumbs-delete-orphans  Delete orphaned thumbnails
      --pshb=ACTION            PubSubHubbub's actions: (un)subscribe, list
      --email2post             Post things using e-mail (from stdin)
  """ % sys.argv[0]
        sys.exit(0)

    if list:
        for service in Service.objects.all().order_by('id'):
            print '%4d "%s"  API=%s' % (service.id, service.name, service.api)
        sys.exit(0)

    if pshb_cmd:
        if pshb_cmd == 'subscribe' and 'id' in fs:
            service = Service.objects.get(id=fs['id'])
            r = pshb.subscribe(service, verbose)
            if r['rc'] == 1:
                print '%s: %s' % (sys.argv[0], r['error'])
            elif r['rc'] == 2:
                print '%s: Hub not found.' % sys.argv[0]
            elif r['rc'] == 202:
                print 'hub=%s: Accepted for verification.' % r['hub']
            elif r['rc'] == 204:
                print 'hub=%s: Subscription verified.' % r['hub']
        elif pshb_cmd == 'unsubscribe' and 'id' in fs:
            r = pshb.unsubscribe(fs['id'], verbose)
            if r['rc'] == 1:
                print '%s: No subscription found.' % sys.argv[0]
            elif r['rc'] == 202:
                print 'hub=%s: Accepted for verification.' % r['hub']
            elif r['rc'] == 204:
                print 'hub=%s: Unsubscribed.' % r['hub']
            else:
                print 'hub=%s: %s.' % (r['hub'], r['rc'])
        elif pshb_cmd == 'renew':
            pshb.renew_subscriptions(force=force_check, verbose=verbose)
        elif pshb_cmd == 'list':
            pshb.list()
        elif pshb_cmd == 'publish':
            pshb.publish(verbose=verbose)
        else:
            print '%s: Unknown "%s" action.' % (sys.argv[0], pshb_cmd)
            sys.exit(1)
        sys.exit(0)

    if thumbs in ('list-orphans', 'delete-orphans'):
        import re
        ths = {}
        for root, dirs, files in os.walk(os.path.join(settings.MEDIA_ROOT,
                                                      'thumbs')):
            for file in files:
                if file[0] != '.':
                    ths[media.get_thumb_info(file)['rel']] = True
        entries = Entry.objects.all()
        for entry in entries:
            hash = media.get_thumb_hash(entry.link_image)
            t = media.get_thumb_info(hash)['rel'] if hash else ''
            if t in ths:
                del ths[t]
            for hash in re.findall('\[GLS-THUMBS\]/([a-f0-9]{40})',
                                   entry.content):
                t = media.get_thumb_info(hash)['rel']
                if t in ths:
                    del ths[t]
        if thumbs == 'delete-orphans':
            if verbose:
                print 'Files to remove: %d' % len(ths)
            for file in ths:
                file = os.path.join(settings.MEDIA_ROOT, file)
                os.remove(file)
        else:
            for file in ths:
                print file
        sys.exit(0)

    if list_old or delete_old:
        days = list_old if list_old else delete_old
        n = time.mktime(unixnow()) - (86400 * days)
        rt = datetime.datetime.fromtimestamp(n).date()
        if 'id' in fs:
            lst = fs['id'].split(',')
            if len(lst) > 1:
                fs['service__id__in'] = lst
            else:
                fs['service__id'] = int(fs['id'])
            del fs['id']
        elif 'api' in fs:
            lst = fs['api'].split(',')
            if len(lst) > 1:
                fs['service__api__in'] = lst
            else:
                fs['service__api'] = fs['api']
            del fs['api']
        fs['service__public'] = False
        fs['protected'] = False
        fs['date_published__lte'] = rt
        fs['date_inserted__lte'] = rt
        if only_inactive:
            fs['active'] = False
        favs = Favorite.objects.all().values('entry')
        if list_old:
            for entry in Entry.objects.filter(**fs).exclude(id__in=favs):
                print '%4d "%s" by %s' % (entry.id,
                                          entry.title,
                                          entry.author_name)
        elif delete_old:
            Entry.objects.filter(**fs).exclude(id__in=favs).delete()
        sys.exit(0)
    else:
        if not force_check or not 'id' in fs:
            fs['active'] = True

    if force_overwrite:
        sel = raw_input(
            "WARNING: This may create thumbnail orphans! Continue Y/N? ").strip()
        if sel != 'Y':
            sys.exit(0)

    try:
        last1 = Entry.objects.filter (service__public=True).\
            order_by('-date_published')[0]
    except IndexError:
        last1 = None

    if workerpool:
        pool = workerpool.WorkerPool(size=10)
    else:
        pool = None

    for service in Service.objects.filter(**fs):
        try:
            mod = __import__('glifestream.apis.%s' % service.api,
                             {}, {}, ['API'])
        except ImportError:
            continue
        mod_api = getattr(mod, 'API')

        if service.last_checked and hasattr(mod_api, 'limit_sec'):
            if not force_check:
                d = datetime.datetime.now() - service.last_checked
                if d.seconds < mod_api.limit_sec:
                    continue

        api = mod_api(service, verbose, force_overwrite)
        if pool:
            pool.put(WorkerJob(api.run))
        else:
            api.run()

    if pool:
        pool.shutdown()
        pool.wait()

    try:
        last2 = Entry.objects.filter (service__public=True).\
            order_by('-date_published')[0]
    except IndexError:
        last2 = None

    if last2 and last1 != last2:
        pshb.publish(verbose=verbose)


def email2post():
    from glifestream.apis import mail
    api = mail.API()
    return api.share(sys.stdin)


def init_files_dirs():
    """Create initial directories and files."""

    upload = os.path.join(settings.MEDIA_ROOT, 'upload')
    _create_dir(upload)

    thumbs = os.path.join(settings.MEDIA_ROOT, 'thumbs')
    _create_dir(thumbs)

    for i in range(0, 10):
        _create_dir(os.path.join(thumbs, str(i)))
    for i in 'abcdef':
        _create_dir(os.path.join(thumbs, i))

    print """
Make sure that 'static/thumbs/*' and 'static/upload' directories exist
and all have write permissions by your webserver.
"""

    template_dir = settings.TEMPLATE_DIRS[0]
    template_files = (
        'user-about.html',
        'user-copyright.html',
        'user-scripts.js',
    )
    try:
        for i in template_files:
            file = os.path.join(template_dir, i)
            if not os.path.isfile(file):
                print "Creating empty file '%s'" % file
                open(file, 'w').close()
    except Error, e:
        print e
        return 1

    return 0


def _create_dir(d, verbose=True):
    if not os.path.isdir(d):
        if verbose:
            print "Creating directory '%s'" % d
        os.mkdir(d)

if __name__ == '__main__':
    run()

Return to:

Send suggestions and report system problems to the System administrator.