-
Notifications
You must be signed in to change notification settings - Fork 0
/
ran.py
114 lines (92 loc) · 2.78 KB
/
ran.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
import sys
# Scrapish
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import re
# Holy f c-like-struct
from dataclasses import dataclass, asdict, is_dataclass
from threading import Thread
# Out
# from json import dump
import json
import pickle
USER, OUTFS = sys.argv[1], sys.argv[2]
@dataclass
class Ent:
li: str
name: str
shr: int
img: str
opts = Options()
opts.add_argument("--log-level=1")
opts.add_argument("--headless")
driver = webdriver.Chrome(options=opts)
# display.Image("cc")
driver.get("https://mbasic.facebook.com")
cookies = pickle.load(open("cookies.pickle", "rb"))
for cookie in cookies:
driver.add_cookie(cookie)
driver.get(f"https://mbasic.facebook.com/{USER}/friends?mutual=1")
friends = []
class DCJSONEncoder(json.JSONEncoder):
def default(self, o):
if is_dataclass(o):
return asdict(o)
return super().default(o)
def dmpint(stri):
m = "".join(re.findall("([0-9]*)", stri))
return int(m) if len(m) > 0 else 0
PTH = f"{OUTFS}/{USER}"
print(f"> {PTH}.json")
def snapfs():
print(">fs")
with open(f"{PTH}.json", "w") as fio:
json.dump(friends, fio, cls=DCJSONEncoder, separators=(",", ":"))
# with open(f"{PTH}.pickle", "wb") as fio:
# pickle.dump(friends, fio)
def ivsnap():
thread = Thread(target = snapfs, args = ())
thread.start()
while True:
root = driver.find_element(By.ID, "root")
WebDriverWait(driver, 15).until(lambda d: root.is_displayed())
print("..")
li = root.find_elements(
By.CSS_SELECTOR, "table[role=presentation]:has(tbody>tr>td>img)"
)
# print("class", once.get_attribute("class"))
print("+", len(li))
for r in li:
# r=i.find_element(By.XPATH, ".//table/tbody/tr")
rf = dmpint(r.find_element(By.XPATH, ".//td[2]/div[1]").text)
if rf < 1:
continue
friends.append(
Ent(
"".join(
re.findall(
r"facebook\.com/(?:profile\.php\?id=([0-9]*)|([^=&?]*))",
r.find_element(By.XPATH, ".//td[2]/a").get_attribute("href"),
)[0]
),
r.find_element(By.XPATH, ".//td[2]/a").text,
rf, # i.find_element(By.XPATH, "//td/div")
r.find_element(By.XPATH, ".//td[1]/img").get_attribute("src"),
)
)
ivsnap()
try:
# print("Await new page and save")
tpag = driver.find_element(By.ID, "m_more_mutual_friends").find_element(
By.XPATH, ".//a"
)
tpag.click()
continue
except: # noqa: E722
break
print("X-X")
# snapfs()