This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
56 lines (40 loc) · 1.34 KB
/
utils.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
from urllib.parse import quote
from html.parser import HTMLParser
if not sys.version_info < (3,):
unicode = str
basestring = str
def u(u_string):
"""
Convert a string to unicode working on both python 2 and 3.
:param u_string: a string to convert to unicode.
.. versionadded:: 0.1.5
"""
if isinstance(u_string, unicode):
return u_string
return u_string.decode('utf-8')
def s(s_string):
"""
Convert a byte stream to string working on both python 2 and 3.
:param s_string: a byte stream to convert to string.
.. versionadded:: 0.1.5
"""
if isinstance(s_string, bytes):
return s_string
return s_string.encode('utf-8')
def html_unescape(_string):
return HTMLParser().unescape(_string)
def escape(_string):
return quote(u(_string).encode(), safe='~')
def filter_json_index_by_year(json_index_content):
json_index_filtered = {}
current_year = int(datetime.now().strftime('%Y'))
for pid, data in json_index_content.items():
post_date = datetime.strptime(data['date'][:-6], '%Y-%m-%dT%H:%M:%S')
post_year = int(post_date.strftime('%Y'))
if post_year >= (current_year - 2):
json_index_filtered[pid] = data
return json_index_filtered