1
|
|
|
# coding: utf-8 |
2
|
|
|
from instapush import Instapush, App |
3
|
|
|
|
4
|
|
|
# django classes |
5
|
|
|
from django.utils.log import getLogger |
6
|
|
|
|
7
|
|
|
# django_th classes |
8
|
|
|
from django_th.services.services import ServicesMgr |
9
|
|
|
from th_instapush.models import Instapush as InstapushModel |
10
|
|
|
|
11
|
|
|
""" |
12
|
|
|
handle process with instapush |
13
|
|
|
put the following in settings.py |
14
|
|
|
|
15
|
|
|
TH_SERVICES = ( |
16
|
|
|
... |
17
|
|
|
'th_instapush.my_instapush.ServiceInstapush', |
18
|
|
|
... |
19
|
|
|
) |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
logger = getLogger('django_th.trigger_happy') |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class ServiceInstapush(ServicesMgr): |
26
|
|
|
|
27
|
|
|
def __init__(self, token=None, **kwargs): |
28
|
|
|
super(ServiceInstapush, self).__init__(token, **kwargs) |
29
|
|
|
self.token = token |
30
|
|
|
|
31
|
|
|
def read_data(self, **kwargs): |
32
|
|
|
""" |
33
|
|
|
get the data from the service |
34
|
|
|
as the pocket service does not have any date |
35
|
|
|
in its API linked to the note, |
36
|
|
|
add the triggered date to the dict data |
37
|
|
|
thus the service will be triggered when data will be found |
38
|
|
|
|
39
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
40
|
|
|
:type kwargs: dict |
41
|
|
|
|
42
|
|
|
:rtype: list |
43
|
|
|
""" |
44
|
|
|
pass |
45
|
|
|
|
46
|
|
|
def save_data(self, trigger_id, **data): |
47
|
|
|
""" |
48
|
|
|
let's save the data |
49
|
|
|
|
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
|
|
|
kwargs = {} |
58
|
|
|
title, content = super(ServiceInstapush, self).save_data(trigger_id, |
59
|
|
|
data, |
60
|
|
|
**kwargs) |
61
|
|
|
instance = InstapushModel.objects.get(trigger_id=trigger_id) |
62
|
|
|
Instapush(user_token=self.token) |
63
|
|
|
app = App(appid=instance.app_id, secret=instance.app_secret) |
64
|
|
|
trackers = {instance.tracker_name: content} |
65
|
|
|
status = app.notify(event_name=instance.event_name, |
66
|
|
|
trackers=trackers) |
67
|
|
|
return status |
68
|
|
|
|