Completed
Pull Request — master (#216)
by
unknown
40s
created

ServiceReddit   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 41
rs 10
c 1
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A save_data() 0 22 1
A read_data() 0 16 1
1
# coding: utf-8
2
# add here the call of any native lib of python like datetime etc.
3
4
# add the python API here if needed
5
from praw import PRAW
6
# django classes
7
from django.core.cache import caches
8
9
from logging import getLogger
10
11
# django_th classes
12
from django_th.services.services import ServicesMgr
13
14
15
"""
16
    TH_SERVICES = (
17
        ...
18
        'th_reddit.my_reddit.ServiceReddit',
19
        ...
20
    )
21
"""
22
23
logger = getLogger('django_th.trigger_happy')
24
25
cache = caches['th_reddit']
26
27
28
class ServiceReddit(ServicesMgr):
29
30
    def read_data(self, **kwargs):
31
        """
32
            get the data from the service
33
            as the pocket service does not have any date
34
            in its API linked to the note,
35
            add the triggered date to the dict data
36
            thus the service will be triggered when data will be found
37
38
            :param kwargs: contain keyword args : trigger_id at least
39
            :type kwargs: dict
40
41
            :rtype: list
42
        """
43
        trigger_id = kwargs.get('trigger_id')
44
        data = list()
45
        cache.set('th_reddit_' + str(trigger_id), data)
46
47
    def save_data(self, trigger_id, **data):
48
        """
49
            let's save the data
50
            :param trigger_id: trigger ID from which to save data
51
            :param data: the data to check to be used and save
52
            :type trigger_id: int
53
            :type data:  dict
54
            :return: the status of the save statement
55
            :rtype: boolean
56
        """
57
        from th_reddit.models import Reddit
58
59
        status = False
60
61
        title, content = super(ServiceReddit, self).save_data(trigger_id, **data)
62
63
        # get the data of this trigger
64
        trigger = Reddit.objects.get(trigger_id=trigger_id)
65
        # we suppose we use a tag property for this service
66
        status = self.reddit.add(title=title, content=content, tags= trigger.tags)
67
68
        return status
69
70