-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.py
80 lines (67 loc) · 2.14 KB
/
helpers.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
from typing import List
from models import Follower
import tweepy, time
RANK_BY = {
"recent": {"column": None},
"followers_count": {"column": Follower.followers_count},
"following_count": {"column": Follower.friends_count},
"listed_count": {"column": Follower.listed_count},
"favourites_count": {"column": Follower.favourites_count},
"statuses_count": {"column": Follower.statuses_count},
}
def divide_into_chunks(l: List, n: int):
"""
Split a list into chunks of n size
params:
l(list) - list to split up
n(int) - size of sublists
"""
for i in range(0, len(l), n):
yield l[i : i + n]
def print_progress_bar(
iteration: int,
total: int,
prefix: str = "",
suffix: str = "",
decimals: int = 1,
length: int = 50,
fill: str = "█",
print_end: str = "\r",
):
"""
Call in a loop to create terminal progress bar. Print new line on complete
params:
iteration(int) - current iteration
total(int) - total iterations
prefix(str) - prefix string
suffix(str) - suffix string
decimals(int) - positive number of decimals in percent complete
length(int) - character length of bar
fill(str) - bar fill character
print_end(str) - end character (e.g. "\r", "\r\n")
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + "-" * (length - filled_length)
print("\r%s |%s| %s%% %s" % (prefix, bar, percent, suffix), end=print_end)
if iteration == total:
print()
def rate_limit_handler(cursor: tweepy.Cursor):
"""
Handler for tweepy Cursors and automatically stops
execution when rate limit is reached
params:
cursor(tweepy.Cursor) - cursor to handle
"""
while True:
try:
yield cursor.next()
except tweepy.RateLimitError:
print("Oh no!! We hit the rate limit. Resuming in 15 mins.")
time.sleep(15 * 60)
def bye():
"""
Stop execution of script.
"""
print("Ok bye.")
exit()