-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
53 lines (46 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
import json
import pickle
import fbchat
from pathlib import Path
import importlib
import random
import time
# load in json file, if it exists
# ideally use this for loading in config files and cookie files
def load_json(filename):
try:
with open(filename, "r") as f:
return json.load(f)
except FileNotFoundError:
return
# dumps a dictionary to a file specified by filename
def save_json(filename, cookies):
try:
with open(filename, "w") as f:
json.dump(cookies, f)
return True
except:
return False
def get_login_credentials(filename):
my_file = Path(filename)
if my_file.is_file():
mod = importlib.import_module(filename[:len(filename)-3])
user_agent = mod.USER_AGENT
username = mod.FB_EMAIL
password = mod.FB_PASS
return {'user_agent': user_agent,
'username':username,
'password':password}
else:
return
def load_session(cookies):
if not cookies:
return
try:
return fbchat.Session.from_cookies(cookies)
except fbchat.FacebookError:
return # Failed loading from cookies
#delays a random amount between float1 and float2
def rand_delay(float1, float2):
delay3 = random.uniform(float1, float2)
time.sleep(delay3)