-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
144 lines (110 loc) · 3.54 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
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
import hashlib
import random
import resource
import string
import sys
from datetime import datetime
from bs4 import BeautifulSoup
from internetarchive import get_item
def print_stats(i, skipped=None):
print("{number} total entries".format(number=i))
if skipped:
print("{number} skipped".format(number=skipped))
print_ram()
def print_ram():
print("used {mb}MB".format(mb=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss // 1024))
def html2text(body):
soup = BeautifulSoup(body, "lxml")
for code in soup.find_all("code"):
code.decompose()
return str(soup.get_text())
def get_files():
ia = get_item("stackexchange")
return {x["name"]: x for x in ia.files}
def file_hash(filename):
"""from https://stackoverflow.com/a/44873382/4398037"""
h = hashlib.sha1()
with open(filename, "rb", buffering=0) as f:
for b in iter(lambda: f.read(128 * 1024), b""):
h.update(b)
return h.hexdigest()
def get_settings(count):
if len(sys.argv) != count + 1:
if count == 1:
return "sites/workplace"
elif count == 2:
return "sites/workplace", "Title"
print("Please specify {x} parameters".format(x=count))
if count == 1:
return sys.argv[1]
elif count == 2:
return sys.argv[1], sys.argv[2]
def get_random_string(length):
return "".join(random.choices(string.ascii_uppercase + string.digits, k=length))
def prettydate(d):
diff = datetime.now() - d
s = diff.seconds
if diff.days > 7 or diff.days < 0:
return d.strftime("%d %b %y")
elif diff.days == 1:
return "1 day ago"
elif diff.days > 1:
return "{} days ago".format(diff.days)
elif s <= 1:
return "just now"
elif s < 60:
return "{} seconds ago".format(s)
elif s < 120:
return "1 minute ago"
elif s < 3600:
return "{} minutes ago".format(int(s / 60))
elif s < 7200:
return "1 hour ago"
else:
return "{} hours ago".format(int(s / 3600))
def create_pagination(num_pages, page, padding=2):
pages = ["1"]
i = 2
while i <= num_pages:
if i < (page - padding - 1):
pages.append("d")
i = page - padding
elif (i > (page + padding)) and (num_pages > (page + padding + 2)):
pages.append("d")
i = num_pages
pages.append(str(i))
i += 1
return pages
def rand():
return random.randint(-2 ** 31, 2 ** 31 - 1)
rating_sql = """
((upvotes + 1.9208) / (upvotes + downvotes) -
1.96 * SQRT((upvotes * downvotes) / (upvotes + downvotes) + 0.9604) /
(upvotes + downvotes)) / (1 + 3.8416 / (upvotes + downvotes))
AS ci_lower_bound
"""
def get_fallback_site():
return {
"id": None,
"name": "Stack Exchange",
"url": "stackexchange.com/",
"icon_url": "https://cdn.sstatic.net/Sites/stackexchange/img/apple-touch-icon.png",
"fallback": True,
"background_color": False,
"foreground_color": False,
"primary_color": False
}
def hex_to_rgb(hex):
""" https://stackoverflow.com/a/29643643 """
return tuple(int(hex[i:i + 2], 16) for i in (0, 2, 4))
def is_light_color(hex):
""" https://stackoverflow.com/a/596241 """
r, g, b = hex_to_rgb(hex[1:])
brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b
return brightness > 245
def save_question_count(count):
with open("count.txt", "w") as f:
f.write(str(count))
def load_question_count():
with open("count.txt", "r") as f:
return int(f.readline())