Completed
Branch master (5db784)
by Fox
01:52 queued 20s
created

ServiceRss.process_data()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 1
c 5
b 0
f 1
dl 0
loc 9
rs 9.6666
1
# coding: utf-8
2
import datetime
3
import time
4
import arrow
5
6
# django classes
7
from django.conf import settings
8
from django.utils.log import getLogger
9
from django.core.cache import caches
10
11
# django_th classes
12
from django_th.services.services import ServicesMgr
13
# th_rss classes
14
from th_rss.lib.feedsservice import Feeds
15
16
logger = getLogger('django_th.trigger_happy')
17
18
cache = caches['th_rss']
19
20
21
class ServiceRss(ServicesMgr):
22
23
    def __init__(self, token=None):
24
        super(ServiceRss, self).__init__(token)
25
26
    def read_data(self, **kwargs):
27
        """
28
            get the data from the service
29
30
            :param kwargs: contain keyword args : trigger_id and model name
31
            :type kwargs: dict
32
            :rtype: dict
33
        """
34
        date_triggered = kwargs['date_triggered']
35
        trigger_id = kwargs['trigger_id']
36
        kwargs['model_name'] = 'Rss'
37
38
        # get the URL from the trigger id
39
        rss = super(ServiceRss, self).read_data(**kwargs)
40
41
        logger.debug("RSS Feeds from %s : url %s", rss.name, rss.url)
42
43
        now = arrow.utcnow().to(settings.TIME_ZONE)
44
        published = ''
45
        my_feeds = []
46
47
        # retrieve the data
48
        feeds = Feeds(**{'url_to_parse': rss.url}).datas()
49
50
        for entry in feeds.entries:
51
52
            if hasattr(entry, 'published_parsed'):
53
                published = datetime.datetime.utcfromtimestamp(
54
                    time.mktime(entry.published_parsed))
55
            elif hasattr(entry, 'created_parsed'):
56
                published = datetime.datetime.utcfromtimestamp(
57
                    time.mktime(entry.created_parsed))
58
            elif hasattr(entry, 'updated_parsed'):
59
                published = datetime.datetime.utcfromtimestamp(
60
                    time.mktime(entry.updated_parsed))
61
62
            if published == '':
63
                published = now
64
            else:
65
                published = arrow.get(str(published)).to(settings.TIME_ZONE)
66
67
            date_triggered = arrow.get(
68
                str(date_triggered)).to(settings.TIME_ZONE)
69
70
            if date_triggered is not None and\
71
               published is not None and\
72
               now >= published >= date_triggered:
73
                my_feeds.append(entry)
74
75
        cache.set('th_rss_' + str(trigger_id), my_feeds)
76
        cache.set('th_rss_uuid_{}'.format(rss.uuid), my_feeds)
77
        # return the data
78
        return my_feeds
79