1
|
|
|
# coding: utf-8 |
2
|
|
|
import arrow |
3
|
|
|
|
4
|
|
|
from slugify import slugify |
5
|
|
|
|
6
|
|
|
# django classes |
7
|
|
|
from django.conf import settings |
8
|
|
|
from django.utils.log import getLogger |
9
|
|
|
|
10
|
|
|
# django_th classes |
11
|
|
|
from django_th.services.services import ServicesMgr |
12
|
|
|
from django_th.tools import to_datetime |
13
|
|
|
|
14
|
|
|
""" |
15
|
|
|
TH_SERVICES = ( |
16
|
|
|
... |
17
|
|
|
'th_pelican.my_pelican.ServicePelican', |
18
|
|
|
... |
19
|
|
|
) |
20
|
|
|
""" |
21
|
|
|
|
22
|
|
|
logger = getLogger('django_th.trigger_happy') |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class ServicePelican(ServicesMgr): |
26
|
|
|
|
27
|
|
|
def __init__(self, token=None, **kwargs): |
28
|
|
|
super(ServicePelican, self).__init__(token, **kwargs) |
29
|
|
|
self.AUTHOR = settings.TH_PELICAN_AUTHOR \ |
30
|
|
|
if settings.TH_PELICAN_AUTHOR else '' |
31
|
|
|
|
32
|
|
|
def _create_content(self, site_title, content, pelican_path, url, **data): |
33
|
|
|
""" |
34
|
|
|
create the file in the 'content' directory of pelican |
35
|
|
|
:param content: the content of the post |
36
|
|
|
:param pelican_path: where the files are created |
37
|
|
|
:param url: url of the datasource |
38
|
|
|
:param data: the data to check to be used and save |
39
|
|
|
:type content: string |
40
|
|
|
:type pelican_path: string |
41
|
|
|
:type url: string |
42
|
|
|
:type data: dict |
43
|
|
|
:return: the status of the save statement |
44
|
|
|
:rtype: boolean |
45
|
|
|
""" |
46
|
|
|
published = to_datetime(data) |
47
|
|
|
|
48
|
|
|
category = data.get('category') if data.get('category') else '' |
49
|
|
|
tags = data.get('tags') if data.get('tags') else '' |
50
|
|
|
|
51
|
|
|
filename = self._set_filename(data.get('title'), pelican_path) |
52
|
|
|
|
53
|
|
|
full_content = self._set_full_content(site_title, data.get('title'), |
54
|
|
|
published, content, url, |
55
|
|
|
category, tags) |
56
|
|
|
|
57
|
|
|
try: |
58
|
|
|
with open(filename, 'w') as f: |
59
|
|
|
f.write(full_content) |
60
|
|
|
status = True |
61
|
|
|
except Exception as e: |
62
|
|
|
logger.critical(e) |
63
|
|
|
status = False |
64
|
|
|
|
65
|
|
|
return status |
66
|
|
|
|
67
|
|
|
@staticmethod |
68
|
|
|
def _set_filename(title, pelican_path): |
69
|
|
|
""" |
70
|
|
|
build the filename |
71
|
|
|
:param title: the title of the post |
72
|
|
|
:param pelican_path: where the files are created |
73
|
|
|
:type title: string |
74
|
|
|
:type pelican_path: string |
75
|
|
|
:return: the filename |
76
|
|
|
:rtype: string |
77
|
|
|
""" |
78
|
|
|
# cleaning the special char |
79
|
|
|
name = title.replace('/', '_').replace('\\', '_').\ |
80
|
|
|
replace(' ', '_').replace(':', '_').replace('&', '').\ |
81
|
|
|
replace('?', '').replace('!', '') |
82
|
|
|
|
83
|
|
|
return "{}/{}.html".format(pelican_path, name) |
84
|
|
|
|
85
|
|
|
def _set_full_content(self, site_title, title, published, |
86
|
|
|
content, url, category='', tags=''): |
87
|
|
|
""" |
88
|
|
|
generate the full content of the file |
89
|
|
|
create the file in the 'content' directory of pelican |
90
|
|
|
:param site_title: title of the website |
91
|
|
|
:param title: the title of the post |
92
|
|
|
:param content: the content of the post |
93
|
|
|
:param published: the date when the data |
94
|
|
|
has been published by the provider |
95
|
|
|
:param url: url of the datasource |
96
|
|
|
:param category: category of this data |
97
|
|
|
:type site_title: string |
98
|
|
|
:type title: string |
99
|
|
|
:type content: string |
100
|
|
|
:type published: string |
101
|
|
|
:type url: string |
102
|
|
|
:param category: string |
103
|
|
|
:return: the the complet content |
104
|
|
|
:rtype: string |
105
|
|
|
""" |
106
|
|
|
|
107
|
|
|
header = self._set_meta(title, published, category, tags) |
108
|
|
|
content = self._set_content(content) |
109
|
|
|
footer = self._set_footer(url, site_title) |
110
|
|
|
|
111
|
|
|
full_content = self._set_html_begin() + self._set_title(title) |
112
|
|
|
full_content += header + content + footer + self._set_html_end() |
113
|
|
|
|
114
|
|
|
return full_content |
115
|
|
|
|
116
|
|
|
def _set_meta(self, title, published, category='', tags=''): |
117
|
|
|
""" |
118
|
|
|
the header |
119
|
|
|
|
120
|
|
|
:param title: the title of the post |
121
|
|
|
:param published: the date when the data |
122
|
|
|
has been published by the provider |
123
|
|
|
:param category: category of this data |
124
|
|
|
:param tags: the tags |
125
|
|
|
:type title: string |
126
|
|
|
:type published: string |
127
|
|
|
:param category: string |
128
|
|
|
:param tags: string |
129
|
|
|
:return: the complet head |
130
|
|
|
:rtype: string |
131
|
|
|
""" |
132
|
|
|
slug_published = slugify(arrow.get(published).format( |
133
|
|
|
'YYYY-MM-DD HH:mm')) |
134
|
|
|
slug_title = slugify(title) |
135
|
|
|
|
136
|
|
|
header = '\n\t\t<meta name="date" content="{}" />\n'.format(published) |
137
|
|
|
if tags: |
138
|
|
|
header += '\t\t<meta name="tags" content="{}" />\n'.format(tags) |
139
|
|
|
if category: |
140
|
|
|
header += '\t\t<meta name="category" content="{}" />\n'.format( |
141
|
|
|
category) |
142
|
|
|
if self.AUTHOR: |
143
|
|
|
header += '\t\t<meta name="authors" content="{}" />\n'.format( |
144
|
|
|
self.AUTHOR) |
145
|
|
|
header += '\t\t<meta name="slug" content="{}"/>\n'.format( |
146
|
|
|
slug_published + '-' + slug_title) |
147
|
|
|
header += '\t</head>' |
148
|
|
|
|
149
|
|
|
return header |
150
|
|
|
|
151
|
|
|
@staticmethod |
152
|
|
|
def _set_title(title): |
153
|
|
|
""" |
154
|
|
|
the title of the Article |
155
|
|
|
:param title: the title of the post |
156
|
|
|
:type title: string |
157
|
|
|
:return: the title |
158
|
|
|
:rtype: string |
159
|
|
|
""" |
160
|
|
|
return "\t<head>\n\t\t<title>{}</title>".format(title) |
161
|
|
|
|
162
|
|
|
@staticmethod |
163
|
|
|
def _set_content(content): |
164
|
|
|
""" |
165
|
|
|
the body of the article |
166
|
|
|
:param content: the content |
167
|
|
|
:param content: string |
168
|
|
|
:return: the complet content |
169
|
|
|
:rtype: string |
170
|
|
|
""" |
171
|
|
|
return "\n\t<body>{}".format(content) |
172
|
|
|
|
173
|
|
|
@staticmethod |
174
|
|
|
def _set_footer(url, name): |
175
|
|
|
""" |
176
|
|
|
Footer of the article |
177
|
|
|
that displays what website provided |
178
|
|
|
the article |
179
|
|
|
:param url: string of the source |
180
|
|
|
:param name: name of the source |
181
|
|
|
:return: the complet footer |
182
|
|
|
:rtype: string |
183
|
|
|
""" |
184
|
|
|
provided = "\t\t<p><em><a href='{}'>Provided by {}</a>" \ |
185
|
|
|
"</em></p>\n\t</body>" |
186
|
|
|
return provided.format(url, name) |
187
|
|
|
|
188
|
|
|
@staticmethod |
189
|
|
|
def _set_html_begin(): |
190
|
|
|
""" |
191
|
|
|
start the html page |
192
|
|
|
""" |
193
|
|
|
return "<html>\n" |
194
|
|
|
|
195
|
|
|
@staticmethod |
196
|
|
|
def _set_html_end(): |
197
|
|
|
""" |
198
|
|
|
close the html page |
199
|
|
|
""" |
200
|
|
|
return "\n</html>" |
201
|
|
|
|
202
|
|
|
def save_data(self, token, trigger_id, **data): |
203
|
|
|
""" |
204
|
|
|
let's save the data |
205
|
|
|
:param token string |
206
|
|
|
:param trigger_id: trigger ID from which to save data |
207
|
|
|
:param data: the data to check to be used and save |
208
|
|
|
:type token string |
209
|
|
|
:type trigger_id: int |
210
|
|
|
:type data: dict |
211
|
|
|
:return: the status of the save statement |
212
|
|
|
:rtype: boolean |
213
|
|
|
""" |
214
|
|
|
from th_pelican.models import Pelican |
215
|
|
|
|
216
|
|
|
kwargs = {} |
217
|
|
|
|
218
|
|
|
title, content = super(ServicePelican, self).save_data( |
219
|
|
|
trigger_id, data, **kwargs) |
220
|
|
|
|
221
|
|
|
trigger = Pelican.objects.get(trigger_id=trigger_id) |
222
|
|
|
|
223
|
|
|
params = {'tags': trigger.tags.lower(), |
224
|
|
|
'category': trigger.category.lower()} |
225
|
|
|
|
226
|
|
|
if 'tags' in data: |
227
|
|
|
del data['tags'] |
228
|
|
|
|
229
|
|
|
params.update(data) |
230
|
|
|
|
231
|
|
|
if self._create_content(trigger.title, content, trigger.path, |
232
|
|
|
trigger.url, **params): |
233
|
|
|
sentence = 'pelican {} created' |
234
|
|
|
status = True |
235
|
|
|
else: |
236
|
|
|
sentence = 'pelican {} not created' |
237
|
|
|
status = False |
238
|
|
|
logger.debug(sentence.format(title)) |
239
|
|
|
|
240
|
|
|
return status |
241
|
|
|
|