-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
categories.py
323 lines (283 loc) · 11.5 KB
/
categories.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
__filename__ = "categories.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.6.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "RSS Feeds"
import os
import datetime
from utils import data_dir
from utils import date_utcnow
from utils import date_epoch
from utils import replace_strings
MAX_TAG_LENGTH = 42
INVALID_HASHTAG_CHARS = (',', ' ', '<', ';', '\\', '"', '&', '#')
def get_hashtag_category(base_dir: str, hashtag: str) -> str:
"""Returns the category for the hashtag
"""
category_filename = base_dir + '/tags/' + hashtag + '.category'
if not os.path.isfile(category_filename):
category_filename = base_dir + '/tags/' + hashtag.title() + '.category'
if not os.path.isfile(category_filename):
category_filename = \
base_dir + '/tags/' + hashtag.upper() + '.category'
if not os.path.isfile(category_filename):
return ''
category_str = None
try:
with open(category_filename, 'r', encoding='utf-8') as fp_category:
category_str = fp_category.read()
except OSError:
print('EX: unable to read category ' + category_filename)
except UnicodeEncodeError as ex:
print('EX: unable to read category unicode ' + category_filename +
' ' + str(ex))
if category_str:
return category_str
return ''
def load_city_hashtags(base_dir: str, translate: {}) -> None:
"""create hashtag categories for cities
"""
category_str = 'places'
if translate.get(category_str):
category_str = translate[category_str]
replacements = {
' & ': ' and ',
'/': ''
}
replacements2 = {
'-': '',
' ': ''
}
for _, _, files in os.walk(base_dir + '/data/cities'):
for cities_file in files:
if not cities_file.endswith('.txt'):
continue
cities_filename = base_dir + '/data/cities/' + cities_file
if not os.path.isfile(cities_filename):
continue
cities: list[str] = []
try:
with open(cities_filename, 'r', encoding='utf-8') as fp_cities:
cities = fp_cities.read().split('\n')
except OSError:
print('EX: unable to load cities file ' + cities_filename)
if not cities:
continue
for hashtag in cities:
hashtag = hashtag.lower().strip()
hashtag = replace_strings(hashtag, replacements)
hashtag2 = replace_strings(hashtag, replacements2)
city_filename = base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category ' +
city_filename)
if '-' in hashtag:
section = hashtag.split('-')
new_hashtag = ''
for text in section:
new_hashtag += text.lower().title()
hashtag2 = new_hashtag
city_filename = \
base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category2 ' +
city_filename)
if ' ' in hashtag:
section = hashtag.split(' ')
new_hashtag = ''
for text in section:
new_hashtag += text.lower().title()
hashtag2 = new_hashtag
city_filename = \
base_dir + '/tags/' + hashtag2 + '.category'
if not os.path.isfile(city_filename):
try:
with open(city_filename, 'w+',
encoding='utf-8') as fp_city:
fp_city.write(category_str)
except OSError:
print('EX: unable to write city category3 ' +
city_filename)
def get_hashtag_categories(base_dir: str,
recent: bool, category: str) -> None:
"""Returns a dictionary containing hashtag categories
"""
hashtag_categories = {}
if recent:
curr_time = date_utcnow()
days_since_epoch = (curr_time - date_epoch()).days
recently = days_since_epoch - 1
for _, _, files in os.walk(base_dir + '/tags'):
for catfile in files:
if not catfile.endswith('.category'):
continue
category_filename = os.path.join(base_dir + '/tags', catfile)
if not os.path.isfile(category_filename):
continue
hashtag = catfile.split('.')[0]
if len(hashtag) > MAX_TAG_LENGTH:
continue
category_str = None
try:
with open(category_filename, 'r',
encoding='utf-8') as fp_category:
category_str = fp_category.read()
except OSError:
print('EX: get_hashtag_categories ' + category_filename)
except UnicodeEncodeError as ex:
print('EX: get_hashtag_categories unicode ' +
category_filename + ' ' + str(ex))
if not category_str:
continue
if category:
# only return a dictionary for a specific category
if category_str != category:
continue
if recent:
tags_filename = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(tags_filename):
continue
mod_time_since_epoc = \
os.path.getmtime(tags_filename)
last_modified_date = \
datetime.datetime.fromtimestamp(mod_time_since_epoc,
datetime.timezone.utc)
file_days_since_epoch = \
(last_modified_date - date_epoch()).days
if file_days_since_epoch < recently:
continue
if not hashtag_categories.get(category_str):
hashtag_categories[category_str] = [hashtag]
else:
if hashtag not in hashtag_categories[category_str]:
hashtag_categories[category_str].append(hashtag)
break
return hashtag_categories
def update_hashtag_categories(base_dir: str) -> None:
"""Regenerates the list of hashtag categories
"""
category_list_filename = data_dir(base_dir) + '/categoryList.txt'
hashtag_categories = get_hashtag_categories(base_dir, False, None)
if not hashtag_categories:
if os.path.isfile(category_list_filename):
try:
os.remove(category_list_filename)
except OSError:
print('EX: update_hashtag_categories ' +
'unable to delete cached category list ' +
category_list_filename)
return
category_list: list[str] = []
for category_str, _ in hashtag_categories.items():
category_list.append(category_str)
category_list.sort()
category_list_str = ''
for category_str in category_list:
category_list_str += category_str + '\n'
# save a list of available categories for quick lookup
try:
with open(category_list_filename, 'w+',
encoding='utf-8') as fp_category:
fp_category.write(category_list_str)
except OSError:
print('EX: unable to write category ' + category_list_filename)
def _valid_hashtag_category(category: str) -> bool:
"""Returns true if the category name is valid
"""
if not category:
return False
for char in INVALID_HASHTAG_CHARS:
if char in category:
return False
# too long
if len(category) > 40:
return False
return True
def set_hashtag_category(base_dir: str, hashtag: str, category: str,
update: bool, force: bool) -> bool:
"""Sets the category for the hashtag
"""
if not _valid_hashtag_category(category):
return False
if not force:
hashtag_filename = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_filename):
hashtag = hashtag.title()
hashtag_filename = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_filename):
hashtag = hashtag.upper()
hashtag_filename = base_dir + '/tags/' + hashtag + '.txt'
if not os.path.isfile(hashtag_filename):
return False
if not os.path.isdir(base_dir + '/tags'):
os.mkdir(base_dir + '/tags')
category_filename = base_dir + '/tags/' + hashtag + '.category'
if force:
# don't overwrite any existing categories
if os.path.isfile(category_filename):
return False
category_written = False
try:
with open(category_filename, 'w+', encoding='utf-8') as fp_category:
fp_category.write(category)
category_written = True
except OSError as ex:
print('EX: unable to write category ' + category_filename +
' ' + str(ex))
except UnicodeEncodeError as ex:
print('EX: unable to write category unicode ' + category_filename +
' ' + str(ex))
if category_written:
if update:
update_hashtag_categories(base_dir)
return True
return False
def guess_hashtag_category(tag_name: str, hashtag_categories: {},
min_tag_length: int) -> str:
"""Tries to guess a category for the given hashtag.
This works by trying to find the longest similar hashtag
"""
if len(tag_name) < min_tag_length:
return ''
category_matched = ''
tag_matched_len = 0
finished = False
for category_str, hashtag_list in hashtag_categories.items():
if finished:
break
for hashtag in hashtag_list:
if hashtag == tag_name:
# exact match
category_matched = category_str
finished = True
break
if len(hashtag) < min_tag_length:
# avoid matching very small strings which often
# lead to spurious categories
continue
if hashtag not in tag_name:
if tag_name not in hashtag:
continue
if not category_matched:
tag_matched_len = len(hashtag)
category_matched = category_str
else:
# match the longest tag
if len(hashtag) > tag_matched_len:
category_matched = category_str
if not category_matched:
return ''
return category_matched