|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
from pytumblr import TumblrRestClient |
|
3
|
|
|
# django classes |
|
4
|
|
|
from django.conf import settings |
|
5
|
|
|
from django.core.cache import caches |
|
6
|
|
|
|
|
7
|
|
|
from logging import getLogger |
|
8
|
|
|
|
|
9
|
|
|
# django_th classes |
|
10
|
|
|
from django_th.services.services import ServicesMgr |
|
11
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
handle process with tumblr |
|
14
|
|
|
put the following in th_settings.py |
|
15
|
|
|
|
|
16
|
|
|
TH_TUMBLR = { |
|
17
|
|
|
'consumer_key': 'abcdefghijklmnopqrstuvwxyz', |
|
18
|
|
|
'consumer_secret': 'abcdefghijklmnopqrstuvwxyz', |
|
19
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
""" |
|
22
|
|
|
|
|
23
|
|
|
logger = getLogger('django_th.trigger_happy') |
|
24
|
|
|
cache = caches['django_th'] |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class ServiceTumblr(ServicesMgr): |
|
28
|
|
|
""" |
|
29
|
|
|
Service Tumblr |
|
30
|
|
|
""" |
|
31
|
|
View Code Duplication |
def __init__(self, token=None, **kwargs): |
|
|
|
|
|
|
32
|
|
|
""" |
|
33
|
|
|
|
|
34
|
|
|
:param token: |
|
35
|
|
|
:param kwargs: |
|
36
|
|
|
""" |
|
37
|
|
|
super(ServiceTumblr, self).__init__(token, **kwargs) |
|
38
|
|
|
self.AUTH_URL = 'https://www.tumblr.com/oauth/authorize' |
|
39
|
|
|
self.ACC_TOKEN = 'https://www.tumblr.com/oauth/access_token' |
|
40
|
|
|
self.REQ_TOKEN = 'https://www.tumblr.com/oauth/request_token' |
|
41
|
|
|
self.consumer_key = settings.TH_TUMBLR_KEY['consumer_key'] |
|
42
|
|
|
self.consumer_secret = settings.TH_TUMBLR_KEY['consumer_secret'] |
|
43
|
|
|
self.token = token |
|
44
|
|
|
self.service = 'ServiceTumblr' |
|
45
|
|
|
self.oauth = 'oauth1' |
|
46
|
|
|
if self.token is not None: |
|
47
|
|
|
token_key, token_secret = self.token.split('#TH#') |
|
48
|
|
|
|
|
49
|
|
|
self.tumblr = TumblrRestClient(self.consumer_key, |
|
50
|
|
|
self.consumer_secret, |
|
51
|
|
|
token_key, |
|
52
|
|
|
token_secret) |
|
53
|
|
|
|
|
54
|
|
|
def read_data(self, **kwargs): |
|
55
|
|
|
""" |
|
56
|
|
|
get the data from the service |
|
57
|
|
|
as the pocket service does not have any date |
|
58
|
|
|
in its API linked to the note, |
|
59
|
|
|
add the triggered date to the dict data |
|
60
|
|
|
thus the service will be triggered when data will be found |
|
61
|
|
|
|
|
62
|
|
|
:param kwargs: contain keyword args : trigger_id at least |
|
63
|
|
|
:type kwargs: dict |
|
64
|
|
|
|
|
65
|
|
|
:rtype: list |
|
66
|
|
|
""" |
|
67
|
|
|
trigger_id = kwargs.get('trigger_id') |
|
68
|
|
|
data = list() |
|
69
|
|
|
cache.set('th_tumblr_' + str(trigger_id), data) |
|
70
|
|
|
|
|
71
|
|
|
def save_data(self, trigger_id, **data): |
|
72
|
|
|
""" |
|
73
|
|
|
let's save the data |
|
74
|
|
|
:param trigger_id: trigger ID from which to save data |
|
75
|
|
|
:param data: the data to check to be used and save |
|
76
|
|
|
:type trigger_id: int |
|
77
|
|
|
:type data: dict |
|
78
|
|
|
:return: the status of the save statement |
|
79
|
|
|
:rtype: boolean |
|
80
|
|
|
""" |
|
81
|
|
|
from th_tumblr.models import Tumblr |
|
82
|
|
|
|
|
83
|
|
|
title, content = super(ServiceTumblr, self).save_data(trigger_id, |
|
84
|
|
|
**data) |
|
85
|
|
|
|
|
86
|
|
|
# get the data of this trigger |
|
87
|
|
|
trigger = Tumblr.objects.get(trigger_id=trigger_id) |
|
88
|
|
|
# we suppose we use a tag property for this service |
|
89
|
|
|
status = self.tumblr.create_text(blogname=trigger.blogname, |
|
90
|
|
|
title=title, |
|
91
|
|
|
body=content, |
|
92
|
|
|
state='published', |
|
93
|
|
|
tags=trigger.tag) |
|
94
|
|
|
|
|
95
|
|
|
return status |
|
96
|
|
|
|
|
97
|
|
|
def auth(self, request): |
|
98
|
|
|
""" |
|
99
|
|
|
let's auth the user to the Service |
|
100
|
|
|
:param request: request object |
|
101
|
|
|
:return: callback url |
|
102
|
|
|
:rtype: string that contains the url to redirect after auth |
|
103
|
|
|
""" |
|
104
|
|
|
request_token = super(ServiceTumblr, self).auth(request) |
|
105
|
|
|
callback_url = self.callback_url(request) |
|
106
|
|
|
|
|
107
|
|
|
# URL to redirect user to, to authorize your app |
|
108
|
|
|
auth_url_str = '{auth_url}?oauth_token={token}' |
|
109
|
|
|
auth_url_str += '&oauth_callback={callback_url}' |
|
110
|
|
|
auth_url = auth_url_str.format(auth_url=self.AUTH_URL, |
|
111
|
|
|
token=request_token['oauth_token'], |
|
112
|
|
|
callback_url=callback_url) |
|
113
|
|
|
|
|
114
|
|
|
return auth_url |
|
115
|
|
|
|