facebook.py
109.4 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020-02-05 16:58
# @Author : Lemon
# @File : facebook.py
# @Software: PyCharm
import base64
import datetime
import html
import json
import logging
import os
import random
import re
import threading
import time
import uuid
from enum import Enum
from urllib import parse
from uuid import uuid1
import demjson
import furl
import retrying
from bs4 import BeautifulSoup
from fbchat import (Client, FBchatException, FBchatUserError, Message, Sticker,
ThreadType, _exception, _graphql, _util, log)
from lib import common, download, google_map, graph
from lib._mqtt import MqttClient
from lib.common import College, WorkPlace
from lib.graph import GraphAPIError
from lib.state import PCState
from lib.ttl_cache import lru_cache
from utils import _attachment, parse_html
from utils.cache import cache
class PostParam(Enum):
EVERYONE = 300645083384735
NETWORKS_FRIENDS = 123780391104836
FRIENDS_OF_FRIENDS = 275425949243301
FRIENDS = 291667064279714
FRIENDS_MINUS_ACQUAINTANCES = 284920934947802
ONLY_ME = 286958161406148
FB_ONLY = 411331705596297
EVENT_PUBLIC = 1493271774218406
EVENT_INVITE_ONLY = 599950423437029
class RequestSource():
const = {
1: 'requests_page_pymk',
2: 'profile_button',
3: 'profile_friends',
4: 'group_member_list',
5: 'hovercard',
6: 'browse',
}
@classmethod
def get(cls, channel):
if isinstance(channel, str):
try:
channel = int(channel)
except:
pass
return cls.const.get(channel, channel)
class FacebookClient(Client):
def __init__(self, user_obj, logout_call=None, max_tries=3, logging_level=logging.ERROR):
self.email = user_obj.email
self.user_obj = user_obj
self.extend = self.graph = None
self.proxy = common.frombase64(user_obj.proxy) if hasattr(user_obj, 'proxy') else None
self.call_logout_function = logout_call
self.confirm_friends = set()
log.setLevel(logging_level)
print('登录参数', user_obj.email, user_obj.password, user_obj.user_agent, max_tries, user_obj.format_cookie(),
self.proxy)
super().__init__(user_obj.email, user_obj.password, user_obj.user_agent, max_tries, user_obj.format_cookie(),
logging_level=logging_level)
if self.user_obj.token:
self._state.access_token = self.user_obj.token
elif self._state.is_logged_in():
self._state.access_token = self._state.getToken()
self._state._session.set_logout_callback(self.call_logout_function)
self.graph = graph.GraphAPI(access_token=self._state.access_token, session=self._state._session, timeout=10)
def __repr__(self):
return "client:%s" % self.email
def setSession(self, session_cookies, user_agent=None):
try:
self._state = PCState.from_cookies(session_cookies, user_agent=user_agent, proxy=self.proxy)
self._uid = self._state.user_id
except Exception as e:
log.exception("Failed loading session")
return False
return True
def login(self, email, password, max_tries=5, user_agent=None):
self.onLoggingIn(email=email)
if max_tries < 1:
raise FBchatUserError("Cannot login: max_tries should be at least one")
if not (email and password):
raise FBchatUserError("Email and password not set")
for i in range(1, max_tries + 1):
try:
self._state = PCState.login2(self.user_obj, self.proxy)
self._uid = self._state.user_id
except Exception as err:
if '密码错误' in str(err) or '账号被封锁' in str(err):
raise err
if i >= max_tries:
raise err
log.exception("Attempt #{} failed, retrying".format(i))
time.sleep(1)
else:
self.onLoggedIn(email=email)
break
def get_token(self):
return self._state.access_token
def get_cookie(self):
cookies = self._state.get_cookies()
ck = ["=".join(item) for item in cookies.items()]
return '; '.join(ck)
def _payload_get(self, url, data):
j = self._state._get(url, data)
try:
return j["payload"]
except (KeyError, TypeError):
raise _exception.FBchatException("Missing payload: {}".format(j))
def bannerInfo(self, fbid):
res = self._payload_post('/ajax/messenger/context_banner/?profile_id=%s' % fbid, {'__user': self.uid})
return res
def addFriend(self, fbid, channel=1, **kwargs):
how_found = RequestSource.get(channel)
data = {
"to_friend": fbid,
"action": "add_friend",
"how_found": how_found,
"logging_location": 'friends_center',
"floc": 'pymk'
}
data.update(**kwargs)
if how_found == 'profile_friends':
data['ref_param'] = 'friends_tab'
try:
# session = self._state.session_factory(self.user_obj.user_agent)
# # res = session.get(url, cookies=self._state.get_cookies())
# if how_found == 'profile_button':
# res = self._payload_post("/ajax/add_friend/action.php", data)
# else:
# proxies = {
# "http": "http://8.210.170.163:24000",
# "https": "https://8.210.170.163:24000",
# }
#
# url = "https://www.facebook.com/ajax/add_friend/action?" + self._to_url_params(data)
# res = session.post(url, cookies=self._state.get_cookies(), data=self._state.get_params(),
# proxies=proxies)
# content = _util.check_request(res)
# j = _util.to_json(content)
# # We can't yet, since errors raised in here need to be caught below
# _util.handle_payload_error(j)
# res = j["payload"]
# 下面是原来的加粉写法
if how_found == 'profile_button':
res = self._payload_post("/ajax/add_friend/action.php", data)
else:
res = self._payload_post("/ajax/add_friend/action?" + self._to_url_params(data), {})
except _exception.FBchatNotLoggedIn:
raise
except _exception.FBchatFacebookError as err:
res = {'success': False, 'error_code': err.fb_error_code, 'error_msg': err.fb_error_message}
return res
def previewName(self, primary_first, primary_middle, primary_last):
data = {
"primary_last_name": primary_last,
"primary_middle_name": primary_middle or '',
"primary_first_name": primary_first,
}
res = self._post(
"/settings/name_change_preview/", data)
return res
def saveName(self, save_password, primary_first, primary_last, primary_middle=None, display_format='complete'):
data = {
'display_format': display_format,
'save_password': save_password,
'primary_first_name': primary_first,
'primary_middle_name': primary_middle or '',
'primary_last_name': primary_last
}
# 'alternate_name': 'lin wei quan', 'show_alternate': '1'
res = self._post("/ajax/settings/account/name.php", data)
status = res['jsmods']['require'][0][1]
if status == 'addError':
html = res['jsmods']['require'][0][3][0]['__html']
return {'success': False, 'error': parse_html.get_div_text(html)}
elif status == 'setPreviewForCurrent':
html = res['jsmods']['require'][0][3][0]['__html']
name = parse_html.get_div_text(html)
try:
self._state.name = self.fetchUserInfo(self._uid)[str(self._uid)].name
except:
pass
return {'success': True, 'msg': name}
elif status == 'closeDialog' and res['jsmods']['require'][0][0] == 'NameChangeSettingsPreview':
return {'success': False, 'error': "无法设置。"}
raise BaseException('未知错误:' + json.dumps(res['jsmods']['require']))
def uploudAvatar(self, binary):
files = {
'photo': binary
}
res = self._payload_post('/profile/picture/upload/?profile_id=%s&photo_source=9' % self.uid, {}, files=files)
try:
return res['fbid']
except:
return None
def setAvatar(self, photo_id):
'''
设置头像
'''
data = {'x': '0', 'y': '0', 'width': '1', 'height': '1',
'photo_id': photo_id, 'profile_id': self.uid,
'source': 'timeline'} # 'source': 'unknown', 'method': 'upload'
res = self._payload_post('/profile/picture/crop_profile_pic/?' + self._to_url_params(data), {})
if 'src' in res:
self._state.imageurl = res['src']
threading.Thread(target=self._hidden_latest, args=()).start()
return res
def setBio(self, content):
'''
设置简介
'''
data = {'bio': content, 'bio_expiration_time': '-1', 'intro_card_session_id': ''}
res = self._post('/profile/intro/bio/save/', data)
html = parse_html.get_domops_3(res)
if 'profile_intro_card_bio' in html:
return {"success": True}
return res
def addWorkPlace(self, workplace: WorkPlace, value='EVERYONE'):
'''添加工作经历'''
value = PostParam.__dict__.get(value).value
data = {
# 'hub_id': '142351455848493',
'type': '2002',
'ref': 'about_tab',
'action_type': 'add', # add | edit
'experience_id': '0',
'privacy[741155019280009]': value
}
data.update(workplace.to_dict())
res = self._post('/profile/edit/work/save/', data)
try:
return {
'success': True,
'setting': res['jsmods']['require'][1][3][0]['props']
}
except:
return {
'success': False,
'error': res['jsmods']['require']
}
def addCollege(self, college: College, value='EVERYONE'):
value = PostParam.__dict__.get(value).value
data = {
'experience_type': '2004',
'degree_id': '0',
'degree_text': '',
'ref': 'about_tab',
'action_type': 'add',
'experience_id': '0',
'privacy[1588594478034907]': value}
data.update(college.to_dict())
res = self._post('/profile/edit/edu/save/', data)
try:
return {
'success': True,
'setting': res['jsmods']['require'][1][3][0]['props']
}
except:
return {
'success': False,
'error': res['jsmods']['require']
}
def addHighSchool(self, college: College, privacy='EVERYONE'):
value = PostParam.__dict__.get(privacy).value
data = {
'experience_type': '2003',
'ref': 'about_tab',
'action_type': 'add',
'experience_id': '0',
'privacy[693162510761869]': value
}
data.update(college.to_dict())
data['school_type'] = 'hs'
res = self._post('/profile/edit/edu/save/', data)
try:
return {
'success': True,
'setting': res['jsmods']['require'][1][3][0]['props']
}
except:
return {
'success': False,
'error': res['jsmods']['require']
}
def setCurrentCity(self, city_id, city_name):
'''
设置当前城市
'''
data = {'current_city_text': city_name, 'current_city': city_id,
'privacy[8787650733]': PostParam.EVERYONE.value}
res = self._post('/profile/async/edit/infotab/save/current_city/', data)
if city_id in res['domops'][0][3]['__html']:
return {"success": True}
return {"success": False}
def setHometown(self, city_id, city_name):
'''
设置家乡地址
'''
data = {'hometown_text': city_name, 'hometown': city_id,
'nctr[_mod]': 'pagelet_hometown',
'privacy[8787655733]': PostParam.EVERYONE.value}
res = self._post("/profile/async/edit/infotab/save/hometown/", data)
if city_id in res['domops'][0][3]['__html']:
return {"success": True}
return {"success": False}
def setGender(self, gender: str):
'''
设置性别 boy or girl
'''
sex = 2 if gender.lower() == 'boy' else 1
data = {'sex': sex, 'sex_preferred_pronouns': '1',
'privacy[237760973066217]': PostParam.ONLY_ME.value}
res = self._post('/profile/edit/infotab/save/gender/', data)
html = parse_html.get_domops_3(res)
if 'gender' in html:
self._state.gender = gender.lower()
return {"success": True}
else:
raise BaseException("未知错误:", html)
def setSexualOrientation(self, sex: int):
'''
设置性取向 1女性 2男性 3双性
'''
data = {
'privacy[8787590733]': PostParam.EVERYONE.value
}
if sex == 1 or sex == 3:
data['meeting_sex1'] = 'on'
if sex == 2 or sex == 3:
data['meeting_sex2'] = 'on'
res = self._post('/profile/edit/infotab/save/interested_in/', data)
html = parse_html.get_domops_3(res)
if 'interested_in' in html:
return {"success": True}
else:
raise BaseException("未知错误:", html)
def sendSticker(self, sticker_id, thread_id, thread_type=ThreadType.USER):
return self.send(
Message(sticker=Sticker(uid=sticker_id),
emoji_size=None, reply_to_id=None, text=None),
thread_id=thread_id,
thread_type=thread_type
)
def _parse_link(self, link):
data = {"image_height": 144, "image_width": 144, 'uri': link}
return self._payload_post('/message_share_attachment/fromURI/', data)
def sendPictureLink(self, link, thread_id, thread_type=ThreadType.USER):
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
data.update({
'action_type': 'ma-type:user-generated-message',
'body': '', 'ephemeral_ttl_mode': '0', 'has_attachment': '1',
'timestamp': int(time.time() * 1000)
})
if thread_type == ThreadType.USER:
data["specific_to_list[0]"] = "fbid:{}".format(thread_id)
data["specific_to_list[1]"] = "fbid:{}".format(self.uid)
info = _attachment.parse(self._parse_link(link))
data.update(info)
return self._doSendRequest(data)
def friendRequest(self):
'''收到的好友请求'''
url = None
empty = False
ids, names = [], []
while True:
if not (ids or names):
if empty:
return
ids, names, url = self._friendRequest(url)
if not url:
empty = True
sub = (ids.pop(0), names.pop(0))
yield sub
def _friendRequest(self, url=None, require_gender=True):
if url:
j = self._post(url, params={})
else:
data = {'log_impressions': 'true', 'reloadcontent': 'false'}
j = self._post('/ajax/requests/loader/', data)
result, next_url = parse_html.friend_unit(j)
if require_gender and result:
ids = [x['fbid'] for x in result]
users = self.fetchUserInfo(*ids)
for r in result:
gender = users.get(r['fbid']).gender
r['gender'] = 'boy' if 'male_singular' == gender else \
('girl' if 'female_singular' == gender else 'unknown')
return result, next_url
def deleteRequest(self, fbid):
data = {'confirm': fbid,
'type': 'friend_connect',
'request_id': fbid,
'list_item_id': '%s_1_req' % fbid,
'status_div_id': '%s_1_req_status' % fbid,
'inline': '1',
'ref': 'jewel',
'ego_log': '',
'actions[reject]': '1',
# 'nctr[_mod]': 'pagelet_bluebar'
}
res = self._post('/ajax/reqs.php', data)
detail = res['domops'][0]
if detail[0] == 'setContent' and detail[2]:
return {"success": True}
raise BaseException("未知错误:" + json.dumps(detail))
def pymkRequest(self) -> tuple:
'''
可能认识的人
:return (ID,Name)
'''
total_ids = []
res = []
while True:
if not res:
res = self._pymk_request(total_ids)
ids = [x['fbid'] for x in res]
total_ids.extend(ids)
if not ids:
return
sub = res.pop(0)
yield sub
def _pymk_request(self, total_ids: list):
data = {
'how_found': 'requests_page_pymk',
'page': 'friends_center',
'instance_name': 'friend-browser',
'big_pics': 1,
'social_context': 1,
'network_context': 1,
'show_more': True
}
for index, id in enumerate(total_ids):
data['friend_browser_id[%d]' % index] = id
j = self._post("/ajax/growth/friend_browser/checkbox.php", data)
result, _ = parse_html.friend_unit(j)
return result
def pymkFriendsNew(self, ext_data=None):
variables = {"count": 20, "location": "FRIENDS_CENTER", "scale": 2}
if ext_data:
variables.update({'cursor': ext_data})
res = self.graphql_api('FriendingCometPYMKPanelPaginationQuery', '3179562935416115', variables)
data = res['data']['viewer']
items = []
for edge in data['people_you_may_know']['edges']:
node = edge['node']
items.append({
'fbid': node['id'],
'name': node['name'],
'src': node['profile_picture']['uri'],
})
result = {
'items': items,
'has_next_page': data['people_you_may_know']['page_info']['has_next_page'],
'ext_data': data['people_you_may_know']['page_info']['end_cursor']
}
return result
def pymkRemoveNew(self, fbid):
variables = {"input": {"people_you_may_know_id": fbid,
"people_you_may_know_location": "FRIENDS_CENTER",
"actor_id": self.uid,
"client_mutation_id": self.get_client_mutation_id()}}
self.graphql_api('FriendingCometPYMKBlacklistSuggestionMutation', '2882558265094181', variables)
return {"success": True}
def _pymk_request_out(self, total_ids: list):
data = {
'jazoest': self._state.jazoest,
'fb_dtsg': self._state._fb_dtsg,
'how_found': 'people_you_may_know',
'page': 'pymk_jewel',
'instance_name': 'jewel',
'show_more': 'true',
'dpr': '1'
}
for index, id in enumerate(total_ids):
data['friend_browser_id[%d]' % index] = id
j = self._post('/ajax/growth/friend_browser/checkbox/', data)
result, _ = parse_html.friend_unit(j)
return result
def blockUser(self, fbid: str):
data = {
'jazoest': self._state.jazoest,
'fb_dtsg': self._state._fb_dtsg,
'uid': fbid,
'update_plite': None,
'privacy_source': 'privacy_settings_page',
'actionCheck': 'blockChecked',
}
res = self._post('https://www.facebook.com/privacy/block_user/', data)
if 'lid' in res:
return {"success": True}
return {"success": False}
def unblockUser(self, fbid: str):
data = {
'jazoest': self._state.jazoest,
'fb_dtsg': self._state._fb_dtsg,
'uid': fbid,
'privacy_source': 'privacy_settings_page',
}
res = self._post('https://www.facebook.com/privacy/unblock_user/', data)
if 'lid' in res:
return {"success": True, 'msg': '请注意,你必须在 48 小时后才能再次拉黑'}
return {"success": False}
def pymkRemove(self, fbid):
'''
data = {'uid': fbid,
'src_page': 'pymk_jewel',
'how_found': 'people_you_may_know',
'nctr[_mod]': 'pagelet_bluebar'}
res = self._post('/friends/pymk/xout/', data)
if res['payload'] == None:
return {"success": True}
raise BaseException("未知错误:" + json.dumps(res))
'''
return self.pymkRemoveNew(fbid)
def _to_url_params(self, data: dict):
return '&'.join(["{}={}".format(k, v) for k, v in data.items()])
def followPage(self, page_id, status=True):
'''关注热门主页'''
data = {'page_id': page_id, 'status': status, 'location': '141', 'qoc_dialog_button': 'false'}
res = self._post('/page/follow_mutator/?' + self._to_url_params(data), {})
return res
def likePage(self, page_id, status=True):
'''点赞热门主页'''
data = {'fbpage_id': page_id,
'add': status, 'reload': 'false',
'fan_origin': 'page_profile',
'actor_id': self._uid,
}
res = self._post('/ajax/pages/fan_status.php?av=' + self._uid, data)
return res
def outgoingRequest(self):
'''发出的加好友请求'''
url = None
empty = False
res = []
while True:
if not res:
if empty:
return
res, url = self._outgoingRequest(url)
if not url:
empty = True
if not res:
empty = True
else:
sub = res.pop(0)
yield sub
def _outgoingRequest(self, url=None, require_gender=True):
if url:
j = self._get(url, {})
else:
data = {'page': 1, 'page_size': '10',
'pager_id': 'outgoing_reqs_pager_5e3a685eb1075' + os.urandom(5).hex()}
j = self._get("/friends/requests/outgoing/more/", data)
result, next_url = parse_html.friend_unit(j)
if require_gender and result:
ids = [x['fbid'] for x in result]
users = self.fetchUserInfo(*ids)
for r in result:
gender = users.get(r['fbid']).gender
r['gender'] = 'boy' if 'male_singular' == gender else \
('girl' if 'female_singular' == gender else 'unknown')
return result, next_url
def cancelRequest(self, fbid):
'''
取消好友申请
:return
'''
data = {
'friend': fbid,
'cancel_ref': 'outgoing_requests',
}
res = self._post('/ajax/friends/requests/cancel.php', data)
detail = res['jsmods']['require'][0]
if 'FriendRequest/cancel' in json.dumps(detail[3]):
return {'success': True}
raise BaseException("未知错误:" + json.dumps(detail))
def searchForCity(self, city):
query = {
"query_params": {"query": city, "viewer_coordinates": {}, "provider": "here_thrift",
"search_type": "city_typeahead", "integration_strategy": "string_match",
"result_ordering": "interleave", "caller": "profile_about",
"page_category": ["city", "subcity"], "geocode_fallback": False}, "max_results": 10,
"photo_width": 50, "photo_height": 50
}
data = {
"query_id": 1529221910536355,
"variables": json.dumps(query)
}
f = furl.furl("https://www.facebook.com/webgraphql/query/").add(data)
res = self._payload_post(f.url.replace("+", ""), {})
return res
def searchForCompany(self, name):
'''搜索公司'''
data = {'value': name, 'existing_ids': '', 'category': '2200', 'context': 'hub_work', 'filter[0]': 'page',
'section': '2002', 'services_mask': '1', 'viewer': self.uid}
url = furl.furl("https://www.facebook.com/ajax/typeahead/search.php").add(data).url
res = self._payload_get(url, {})
return res
def searchForCollege(self, name, context="大学"):
if context == "大学":
data = {'value': name, 'existing_ids': '', 'category': '2602', 'context': 'hub_college',
'filter[0]': 'page',
'section': '2004', 'services_mask': '1', 'viewer': self.uid}
elif context == '高中':
data = {
'value': name, 'existing_ids': '', 'category': '2601', 'context': 'hub_high_school',
'filter[0]': 'page',
'section': '2003', 'services_mask': '1', 'viewer': self.uid,
'page_categories[0]': '2601',
'page_categories[1]': '110152272401235',
'page_categories[2]': '186998168001766',
'page_categories[3]': '199405806739848',
}
url = furl.furl("https://www.facebook.com/ajax/typeahead/search.php").add(data).url
res = self._payload_get(url, {})
return res
def searchForPosition(self, name):
'''搜索职位'''
data = {'value': name, 'existing_ids': '', 'category': '2611', 'context': 'hub_work_position',
'filter[0]': 'page', 'section': '2002', 'services_mask': '1',
'viewer': self.uid}
url = furl.furl("https://www.facebook.com/ajax/typeahead/search.php").add(data).url
res = self._payload_get(url, {})
return res
def searchForConcentration(self, name):
'''搜索专业学科'''
data = {'value': name, 'existing_ids': '', 'category': '2609', 'context': 'hub_school_concentration',
'filter[0]': 'page', 'page_categories[0]': '2609', 'page_categories[1]': '2608', 'section': '2004',
'services_mask': '1', 'viewer': self.uid}
url = furl.furl("https://www.facebook.com/ajax/typeahead/search.php").add(data).url
res = self._payload_get(url, {})
return res
def getOverview(self):
data = {
"dom_section_id": "u_0_2e",
"profile_id": self.uid,
"section": "overview",
"viewer_id": self.uid,
"lst": "%s:%s:%d" % (self.uid, self.uid, int(time.time()))
}
f = furl.furl("https://www.facebook.com/profile/async/infopage/nav/").add(data)
res = self._post(f.url, {})
return parse_html.get_overview_text(res)
@lru_cache.memoize()
def query_country(self, city_id):
location = self._getLatlng(city_id)
if location:
country = google_map.get_country(location)
return city_id, '测试', country
raise BaseException("未获取到经纬度")
def getUserLive(self, fbid):
data = {
"dom_section_id": "u_0_%d" % random.randint(10, 40),
"profile_id": fbid,
"section": "places",
"viewer_id": self.uid,
"lst": "%s:%s:%d" % (self.uid, fbid, int(time.time()))
}
f = furl.furl("https://www.facebook.com/profile/async/infopage/nav/").add(data)
res = self._post(f.url, {})
city_text, city_id = parse_html.get_current_city(res)
if city_id:
return self.query_country(city_id)
else:
return ("未设置当前地区")
def _getLatlng(self, city_id):
res = self.graphql_api('CityPageHeaderRootQuery', '2749700208407754', {"page": city_id})
try:
return res['data']['page']['location']
except:
return None
def uploadCover(self, binary):
'''上传首页封面'''
files = {
'pic': binary
}
data = {'target_id': self.uid, 'profile_id': self.uid, 'source': '10'}
res = self._post('https://upload.facebook.com/ajax/timeline/cover/upload/', data, files=files)
result = res['jsmods']['require'][0]
status = result[1]
detail = result[3]
if isinstance(detail[0], int):
return str(detail[0])
raise BaseException(status + ' ' + ':'.join(detail[3]))
def setCover(self, photo_id):
'''设置封面图片'''
data = {
'photo_id': photo_id, 'profile_id': self.uid, 'photo_offset_y': '',
'photo_offset_x': '', 'save': '1',
'nctr[_mod]': 'pagelet_timeline_main_column'
}
res = self._post("/ajax/timeline/cover_photo_select.php?av=%s" % self.uid, data)
html = parse_html.get_domops_3(res)
m = re.search(r'src="(.*?)"', html)
if m:
threading.Thread(target=self._hidden_latest, args=()).start()
return {'src': m.group(1).replace('&', '&')}
raise BaseException("设置封面未知错误:" + json.dumps(res['jsmods']['require'][0]))
def info(self):
if not getattr(self._state, 'gender', None):
def _check_lang(client):
if client._state.language != 'zh-Hans':
# client.setLanguage('zh_CN')
print('需要更新语言:', client._state.language, '->', 'zh_CN')
client._state.language = 'zh-Hans'
def _info_old(client):
user = client.fetchUserInfo(client._uid)[str(client._uid)]
gender = user.gender
gender_ = 'boy' if 'male_singular' == gender else ('girl' if 'female_singular' == gender else 'unknown')
return gender_, user.name
def _info(client):
res = client.graph.get_object('me', fields='gender,name,picture{url}')
gender = 'boy' if 'male' == res.get('gender') else 'girl'
name = res.get('name')
imageurl = res.get('picture', {}).get('data', {}).get('url')
id = res.get('id')
return gender, name, imageurl, id
_check_lang(self)
self._state.gender, self._state.name, self._state.imageurl, self._state.id = _info(self)
data = {
'fbid': self._state.id,
'name': self._state.name,
'page_url': self._state.page_url,
'imageurl': self._state.imageurl,
'gender': self._state.gender,
'cookie': self.get_cookie(),
'user_agent': self.get_user_agent(),
'proxy': self.proxy
}
return data
def followProfile(self, fbid, status=True):
'''关注个人主页'''
data = {
'profile_id': fbid,
'location': 1,
}
if status:
url_ = '/ajax/follow/follow_profile.php'
else:
url_ = '/ajax/follow/unfollow_profile.php'
res = self._post(url_, data)
if 'onload' not in res:
raise BaseException("未知错误:" + res)
onloads = json.dumps(res['onload'])
if 'Arbiter.inform' in onloads:
return {"success": True}
raise BaseException("未知错误:" + onloads)
def get_user_agent(self):
return self._state._session.headers.get('User-Agent', random.choice(_util.USER_AGENTS))
def searchForUserByFilter(self, name, city_id=None, ext_data=None):
if not ext_data:
from urllib import parse
tob64 = lambda x: str(base64.b64encode(x.encode('utf-8')), 'utf-8')
if city_id:
filter = json.dumps({'city': json.dumps({"name": "users_location", "args": str(city_id)})})
b64 = tob64(filter)
r = self._state._session.get(
f'https://www.facebook.com/search/people/?q={parse.quote(name)}&epa=FILTERS&filters={b64}')
else:
r = self._state._session.get(
f'https://www.facebook.com/search/people/?q={parse.quote(name)}')
fbid = parse_html.get_all_raw_id(r.text)
data = parse_html.get_ext_data(r)
return {'fbid_list': fbid, 'ext_data': data}
else:
data = {'data': demjson.encode(ext_data), 'fb_dtsg_ag': self._state.fb_dtsg_ag}
res = self._state._get('https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet', data)
fbid = parse_html.get_all_raw_id(res['payload'])
ext_data.pop('page_number', None)
ext_data.update(parse_html.get_ext_data(res))
return {
'fbid_list': fbid,
'ext_data': ext_data,
}
def searchForGroupByPublic(self, name, ext_data):
if not ext_data:
from urllib import parse
filter = 'eyJncm91cHNfc2hvd19vbmx5Ijoie1wibmFtZVwiOlwicHVibGljX2dyb3Vwc1wiLFwiYXJnc1wiOlwiXCJ9In0%3D'
# filter = 'eyJwdWJsaWNfZ3JvdXBzIjoie1wibmFtZVwiOlwicHVibGljX2dyb3Vwc1wiLFwiYXJnc1wiOlwiXCJ9In0%3D'
# https://www.facebook.com/search/groups?q=%E5%91%A8%E6%9D%B0%E4%BC%A6&filters=eyJwdWJsaWNfZ3JvdXBzIjoie1wibmFtZVwiOlwicHVibGljX2dyb3Vwc1wiLFwiYXJnc1wiOlwiXCJ9In0%3D
r = self._state._session.get(
f'https://www.facebook.com/search/groups/?q={parse.quote(name)}&epa=FILTERS&filters={filter}')
fbid = parse_html.get_all_group(r.text)
data = parse_html.get_ext_data(r)
return {'fbid_list': fbid, 'ext_data': data}
else:
data = {'data': demjson.encode(ext_data), 'fb_dtsg_ag': self._state.fb_dtsg_ag}
res = self._state._get('https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet', data)
fbid = parse_html.get_all_group(res['payload'])
ext_data.pop('page_number', None)
next_data = parse_html.get_ext_data(res)
if next_data:
ext_data.update(next_data)
return {
'fbid_list': fbid,
'ext_data': ext_data,
}
def SearchRenderable(self, name, cursor='', filters=[]):
"""新搜索群组功能"""
if not cursor:
cursor = None
variables = {
"allow_streaming": False,
"args": {
"callsite": "COMET_GLOBAL_SEARCH",
"config": {"bootstrap_config": None, "exact_match": False, "high_confidence_config": None,
"watch_config": None},
"context": {"bsid": "b3e300c-1a0b-4643-91a7-d34a582fbb6b", "tsid": "0.4453413139497706"},
"experience": {"fbid": None, "grammar_bqf": None, "role": None, "type": "GROUPS_TAB"},
"filters": filters,
"text": name}, "cursor": cursor, "feedbackSource": 23,
"fetch_filters": True, "scale": 2, "stream_initial_count": 0}
try:
res = self.graphql_api('CometSearchResultsInitialResultsQuery', '4370093036395458', variables)
if 'errors' in res:
return {'success': False, 'errors': res['errors']}
else:
return {'success': True, 'data': res['data']}
except Exception as e:
return {'success': False, 'errors': str(e)}
def GraphSearchQueryFilter(self, city):
"""搜索分组过滤地区"""
variables = {"count": 8,
"filterID": "Z3NxZjp7IjAiOiJicm93c2VfaW5zdGFudF9maWx0ZXIiLCIxIjoia2V5d29yZHNfZ3JvdXBzKFx1MDAyNUU1XHUwMDI1OTFcdTAwMjVBOFx1MDAyNUU2XHUwMDI1OURcdTAwMjVCMFx1MDAyNUU0XHUwMDI1QkNcdTAwMjVBNikiLCIzIjoiNDRiYzM4MTYtOWQwNS00NWQyLTljMDAtN2M0MzMyMGQwMzM1IiwiY3VzdG9tX3ZhbHVlIjoiQnJvd3NlR3JvdXBzQ2l0eUluc3RhbnRGaWx0ZXJDdXN0b21WYWx1ZSJ9",
"profile_picture_size": None, "query": city}
try:
res = self.graphql_api('useCometSearchFilterTypeaheadCompositeNetworkDataSourceQuery', '2065678270223319',
variables)
if 'errors' in res:
return {'success': False, 'errors': res['errors']}
else:
return {'success': True, 'data': res['data']}
except Exception as e:
return {'success': False, 'errors': str(e)}
pass
def searchForPostByPublic(self, keyword, ext_data=None):
if not ext_data:
f = furl.furl('https://www.facebook.com/search/posts/')
f.add({
'q': keyword,
'epa': 'FILTERS',
'filters':
'eyJycF9hdXRob3IiOiJ7XCJuYW1lXCI6XCJtZXJnZWRfcHVibGljX3Bvc3RzXCIsXCJhcmdzXCI6XCJcIn0ifQ==',
'ref': 'side_filter',
'fb_dtsg_ag': self._state.fb_dtsg_ag,
})
r = self._state._session.get(f.url)
story_list = parse_html.get_all_unit_id(r.text)
ext_data = parse_html.get_ext_data(r)
return {'story_list': story_list, 'ext_data': ext_data}
else:
data = {'data': demjson.encode(ext_data), 'fb_dtsg_ag': self._state.fb_dtsg_ag}
res = self._state._get('https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet', data)
story_list = parse_html.get_all_unit_id(res['payload'])
ext_data.pop('page_number', None)
ext = parse_html.get_ext_data(res)
if ext:
ext_data.update(ext)
else:
ext_data = None
return {
'story_list': story_list,
'ext_data': ext_data,
}
def friendsList(self, ext_data=None, fbid=None, simple=False):
if ext_data:
ext_data = common.frombase64(ext_data)
if 'collection_token' in ext_data:
data = {
'fb_dtsg_ag': self._state.fb_dtsg_ag,
'data': demjson.encode(ext_data),
}
res = self._get('/ajax/pagelet/generic.php/AllFriendsAppCollectionPagelet', data)
cursor = parse_html.get_pagelet_info(res)
ext_data.update({'cursor': cursor})
res, _ = parse_html.get_friend_div(res.get('payload'))
res['items']['ext_data'] = ext_data
res['items']['count'] = ext_data['count']
res['items']['has_next_page'] = not cursor is None
else:
variables = {"count": 20,
"cursor": ext_data['cursor'],
"search": None, "scale": 2,
"privacySelectorRenderLocation": "COMET_PROFILE_COLLECTIONS",
"id": ext_data['id']}
res = self.graphql_api('ProfileCometAppCollectionListRendererPaginationQuery', '2773917206008873',
variables)
res = res['data']['node']
else:
if fbid:
# url = f'https://www.facebook.com/{fbid}/friends'
url = f'https://www.facebook.com/profile.php?id={fbid}&sk=friends'
else:
url = f'https://www.facebook.com/profile.php?id={self._uid}&sk=friends'
# url = self._state.page_url.replace('profile.php?id=', '') + '/friends'
res = self._state._session.get(url)
if res.status_code == 200 and 'friends' in res.url:
res, msg = parse_html.get_friend_div(res.text)
if not res:
respone = {
'items': [],
'count': 0,
'ext_data': {},
'has_next_page': False,
'error_msg': msg
}
return respone
else:
raise BaseException("网络访问异常,url=", res.url)
edges = res['items']['edges']
count = res['items']['count']
friends = []
for edge in edges:
node = edge['node']
data = {
'image': node['image']['uri'],
'name': node['title']['text'],
'can_friend': node.get('can_friend', False),
'fbid': node['node']['id'],
'url': node['node']['url']
}
friends.append(data)
if 'page_info' in res['items']:
respone = {
'items': friends,
'count': count,
'ext_data': {
'id': res['id'],
'cursor': res['items']['page_info']['end_cursor']
},
'has_next_page': res['items']['page_info']['has_next_page']
}
elif 'ext_data' in res['items']:
respone = {
'items': friends,
'count': count,
'ext_data': res['items']['ext_data'],
'has_next_page': res['items']['has_next_page'],
}
else:
respone = {
'items': [],
'count': 0,
'ext_data': {},
'has_next_page': False
}
respone['ext_data'] = common.tobase64(respone['ext_data'])
if simple and respone['items']:
fbids = [item.get('fbid') for item in respone['items']]
respone['items'] = fbids
return respone
def changePwd(self, old, new):
data = {
'password_old': old,
'password_new': new,
'password_confirm': new,
'caller': 'WWW Password Settings',
'session_identifier': str(uuid1()),
}
res = self._payload_post('/password/changer/', data)
# {field: "update_success", message: ""}
# {'field': 'old_password_error', 'message': '密码输入错误。'}
if res['field'] == 'update_success':
return {'success': True, 'error': res['message']}
else:
return {'success': False, 'error': res}
def setFriendListPrivate(self, value):
value = PostParam.__dict__.get(value).value
data = {'privacy_fbid': '8787365733', 'post_param': value, 'render_location_enum': 'settings',
'is_saved_on_select': 'true', 'should_return_tooltip': 'false',
'prefix_tooltip_with_app_privacy': 'false', 'ent_id': '0', 'user_id': self._uid}
self._post('https://www.facebook.com/privacy/selector/update?' + self._to_url_params(data), {})
data = {
'post_param': value,
'value': value,
'saved_custom_opt_in': True,
}
res = self._post('/ajax/settings/granular_privacy/friendlist.php', data)
if 'fbSettingsListItem' in res['domops'][0][1]:
return {"success": True}
return {"success": False}
def setMomentComment(self, value):
value = PostParam.__dict__.get(value).value
data = {'privacy_fbid': '10153940308610734', 'post_param': value, 'render_location_enum': 'settings',
'is_saved_on_select': 'true', 'should_return_tooltip': 'false',
'prefix_tooltip_with_app_privacy': 'false', 'ent_id': '0', 'user_id': self._uid}
self._post('https://www.facebook.com/privacy/selector/update?' + self._to_url_params(data), {})
data = {
'post_param': value,
'value': value,
'saved_custom_opt_in': True,
}
# self._post('/goodwill/promotion_trigger_info/record/', {})
res = self._post('/ajax/settings/timeline/posting.php', data)
if 'fbSettingsListItem' in res['domops'][0][1]:
return {"success": True}
return {"success": False}
def setMomentShare(self, value):
# data = {'privacy_fbid': '10153940308610734', 'post_param': value, 'render_location_enum': 'settings',
# 'is_saved_on_select': 'true', 'should_return_tooltip': 'false',
# 'prefix_tooltip_with_app_privacy': 'false', 'ent_id': '0', 'user_id': self._uid}
#
# self._post('https://www.facebook.com/privacy/selector/update?' + self._to_url_params(data), {})
data = {
'storyresharesetting': value
}
# self._post('/goodwill/promotion_trigger_info/record/', {})
res = self._post('/ajax/settings/timeline/story_reshare.php', data)
if 'fbSettingsListItem' in res['domops'][0][1]:
return {"success": True}
return {"success": False}
def setCanFriendReq(self, value):
value = PostParam.__dict__.get(value).value
data = {'privacy_fbid': '8787540733', 'post_param': value, 'render_location_enum': 'settings',
'is_saved_on_select': True, 'should_return_tooltip': 'false',
'prefix_tooltip_with_app_privacy': False, 'ent_id': '0', 'user_id': self.uid}
self._post('/privacy/selector/update/?' + self._to_url_params(data), {})
data = {'post_param': value, 'saved_custom_opt_in': 'true', 'value': value}
res = self._post('/ajax/settings/granular_privacy/can_friend_req.php', data)
if 'fbSettingsListItem' in res['domops'][0][1]:
return {"success": True}
return {"success": False}
def setBirthday(self, year, month, day):
data = {
'privacy[8787510733]': PostParam.EVERYONE.value,
'privacy[8787805733]': PostParam.EVERYONE.value,
'birthday_month': month, 'birthday_day': day, 'birthday_year': year,
'birthday_confirmation': '1',
'bd_surface': 'www_profile', 'edit_birthday_allowed': '0',
'__submit__': '1', 'nctr[_mod]': 'pagelet_basic'
}
res = self._post('/profile/edit/infotab/save/birthday/', data)
html = parse_html.get_domops_3(res)
if 'birthday' in html:
return {"success": True}
else:
raise BaseException("未知错误:", html)
def setLanguage(self, language):
data = {
'new_language': language,
'new_fallback_language': '',
}
res = self._post('/ajax/settings/language/account.php', data)
return {"success": True}
def inboxMessenger(self, ext_data=0):
if ext_data:
info = self.fetchThreadList(before=int(ext_data))
else:
info = self.fetchThreadList()
next_data = info[-1].last_message_timestamp if info else 0
friends = []
for foo in info:
if ThreadType.GROUP == foo.type:
friends.append({
'type': foo.type.name,
'fbid': foo.uid,
'name': foo.name,
'image': foo.photo,
'url': None,
'gender': None,
'message_count': foo.message_count,
'members': list(foo.participants),
})
elif ThreadType.USER == foo.type:
friends.append({
'type': foo.type.name,
'fbid': foo.uid,
'name': foo.name,
'image': foo.photo,
'url': foo.url,
'gender': 'boy' if foo.gender == 'male_singular' else 'girl',
'message_count': foo.message_count,
})
response = {
'items': friends,
'ext_data': next_data
}
return response
def inboxLastMessage(self, before=None):
params = {
"limit": 20,
"tags": ['INBOX'],
"before": before,
"includeDeliveryReceipts": False,
"includeSeqID": False,
}
(j,) = self.graphql_requests(_graphql.from_doc_id("1349387578499440", params))
last_message_array = []
for node in j['viewer']['message_threads']['nodes']:
message_nodes = node['last_message']['nodes']
fbid = node['thread_key']['other_user_id']
if message_nodes:
message = message_nodes[0]
if message['message_sender']['messaging_actor']['id'] == self.uid:
last_message = '我:' + str(message['snippet'])
else:
last_message = '他:' + str(message['snippet'])
last_message_timestamp = message['timestamp_precise']
else:
last_message = None
last_message_timestamp = 0
last_message_array.append((fbid, last_message, last_message_timestamp))
before = last_message_array[-1][2] if last_message_array else 0
return {'data': last_message_array, 'count': len(last_message_array), 'before': before}
def lastMessage(self, thread_id, limit=1):
params = {"id": thread_id, "message_limit": limit, "load_messages": False,
"load_read_receipts": False, "load_delivery_receipts": False,
"is_work_teamwork_not_putting_muted_in_unreads": False}
(j,) = self.graphql_requests(_graphql.from_doc_id("2647524395352386", params))
message_thread = j.get('message_thread')
if message_thread:
message_nodes = message_thread['last_message']['nodes']
if message_nodes:
message = message_nodes[0]
last_message = json.dumps({
'sender': message['message_sender']['messaging_actor']['id'],
'message': str(message['snippet'])
}, ensure_ascii=False)
last_message_timestamp = message['timestamp_precise']
else:
last_message = None
last_message_timestamp = 0
return {'message': last_message, 'timestamp': last_message_timestamp}
def startListening(self):
if not self._mqtt:
self._mqtt = MqttClient(self)
proxy = getattr(self._state._session, '_args', {}).get('sock', {})
print(proxy)
if proxy and self._mqtt._proxy_is_valid(proxy):
self._mqtt.configure_s5proxy(proxy)
def stopListening(self):
if not self._mqtt:
return
try:
self._mqtt.stop()
except:
pass
self._mqtt = None
def listen(self, markAlive=None):
self.startListening()
self._mqtt.listen(self.onListening)
self.stopListening()
def securityDevice(self):
res = self.graphql_api('SecuritySettingsSessionGroupRefetchQuery', '2549907381730805', {"session_count": 20})
return res['data']['security_settings']['sessions']
def sendRemoteImageSalt(self, image_url, message=None, thread_id=None, thread_type=None):
file_urls = _util.require_list(image_url)
files = _util.get_files_from_urls(file_urls)
after_file = []
for file_tuple in files:
fl = list(file_tuple)
fl[1] = fl[1] + os.urandom(32)
after_file.append(tuple(fl))
files = self._upload(after_file)
return self._sendFiles(
files=files, message=message, thread_id=thread_id, thread_type=thread_type or ThreadType.USER
)
def deleteFriend(self, fbid):
data = {
'uid': fbid,
'unref': 'bd_friends_tab',
'floc': 'friends_tab'
}
res = self._post('/ajax/profile/removefriendconfirm.php', data)
if 'jsmods' in res and 'RemoveFriendDialog' in json.dumps(res):
return {"success": True}
else:
return {"success": False}
def friendMoment(self, fbid=None):
f = furl.furl('https://m.facebook.com/profile/timeline/stream/').add({
'cursor': '',
'start_time': '-9223372036854775808',
'profile_id': fbid or self.uid,
'replace_id': 'u_0_1s',
})
payload = self._payload_post(f.url, {'m_sess': ''})
items = parse_html.get_moment_info(payload)
return items
def momentAction(self, store_id, action="DELETE"):
data = {
'feedobjects_identifiers': store_id,
'feed_context': '{"use_m_feed":true,"m_entstream_source":"timeline","story_node_id":"u_0_23","show_attachments":true,"is_attached_story":false}',
'story_node_id': 'u_0_23',
'chevron_button_id': 'u_0_2l',
'outer_story_token': '',
'show_report_ad_only': False,
}
try:
payload = self._payload_post('https://m.facebook.com/story_chevron_menu/?is_menu_registered=false', data)
code = payload['actions'][1]['code']
if action.upper() == 'DELETE':
c3 = re.findall('"removeURI":"(.*?direct_action_execute.*?)"', code)[0]
elif action.upper() == 'HIDE':
c3 = re.findall('"uri":"(.*?direct_action_execute.*?)"', code)[0]
else:
raise BaseException("action 错误,只允许DELETE / HIDE")
c4 = c3.replace('\\\\\\', '').replace('\\\\u0025', '%')
path = html.unescape(c4) + '&confirmed=1'
res = self._payload_post('https://m.facebook.com' + path, {'m_sess': ''})
assert res
if action.upper() == 'HIDE':
msg = parse_html.get_div_text(res['actions'][0]['html'])
else:
msg = '操作成功'
except Exception as err:
msg = f'操作成功'
return msg
def sendMoment(self, text, type="TEXT", privacy='EVERYONE', location=None, attachments=None, with_tags_ids=None):
privacy = PostParam.__dict__.get(privacy).value
requestid = self._rankerRequestID()
uuid = lambda: str(uuid1())
uuid_1 = uuid()
uuid_2 = uuid()
uuid_3 = uuid()
if not with_tags_ids and not isinstance(with_tags_ids, list):
with_tags_ids = []
data = {
"client_mutation_id": uuid_1,
"actor_id": self.uid,
"input": {
"actor_id": self.uid,
"client_mutation_id": uuid_2,
"source": "WWW",
"audience": {
"web_privacyx": str(privacy)
},
"message": {
"text": text,
"ranges": [
]
},
"logging": {
"composer_session_id": uuid_3,
"ref": "timeline"
},
"with_tags_ids": with_tags_ids,
"multilingual_translations": [
],
"camera_post_context": {
"deduplication_id": uuid_3,
"source": "composer"
},
'composer_source_surface': "timeline",
'composer_entry_point': "timeline",
'composer_entry_time': 8,
'composer_session_events_log': {'composition_duration': 83, 'number_of_keystrokes': 6},
"branded_content_data": {
},
"direct_share_status": "NOT_SHARED",
"sponsor_relationship": "WITH",
"web_graphml_migration_params": {
"target_type": "feed",
"xhpc_composerid": "rc.u_fetchstream_10_3",
"xhpc_context": "profile",
"xhpc_publish_type": "FEED_INSERT",
'xhpc_timeline': True,
'waterfall_id': uuid_3
},
"extensible_sprouts_ranker_request": {
"RequestID": requestid
},
"external_movie_data": {
},
"place_attachment_setting": "HIDE_ATTACHMENT"
}
}
if type == 'LOCATION':
assert location, '缺少位置参数,location'
data["input"]['place_attachment_setting'] = "SHOW_ATTACHMENT"
data["input"]['explicit_place_id'] = str(location)
if type in ['PHOTO', 'VIDEO']:
new_attachments = []
for attachment in attachments:
if isinstance(attachment, str):
if type == 'PHOTO':
new_attachments.append({"photo": {"id": str(attachment), "tags": []}})
elif type == 'VIDEO':
new_attachments.append({"video": {"id": str(attachment), "notify_when_processed": True}})
else:
new_attachments.append(attachment)
attachments = new_attachments
assert attachments, '缺少参数,attachments'
data['input']["attachments"] = attachments
if location:
data["input"]['explicit_place_id'] = str(location)
payload = self._payload_post('/webgraphql/mutation/?doc_id=1740513229408093', {'variables': json.dumps(data)})
print(payload)
try:
story_id_b64 = payload['data']['story_create']['story']['id']
except:
story_id_b64 = payload['data']['story_create']['story_id']
story_id = str(base64.b64decode(story_id_b64), 'utf-8')
return {"story_id": story_id}
@lru_cache.memoize(ttl=60)
def _rankerRequestID(self):
data = {
'composer_id': 'rc.u_0_17',
'target_id': self.uid,
'composer_type': 'feedx_sprouts',
'friend_list_id': ''
}
res = self._post('/react_composer/feedx_sprouts/bootload/?' + self._to_url_params(data), {})
requestid = res['jsmods']['require'][0][3][1]['config']['loggingConfig']['rankerRequestID']
return requestid
def upload_attachments(self, image):
url = 'https://upload.facebook.com/ajax/react_composer/attachments/photo/upload?av=' + self.uid
data = {
'qn': str(uuid1()),
'target_id': self.uid,
'profile_id': self.uid,
'source': '8',
'waterfallxapp': 'web_react_composer',
'upload_id': 1024,
}
farr = download.image(image)
res = self._payload_post(url, data, files={'farr': farr})
return {'src': res['imageSrc'], 'photo_id': res['photoID']}
def _hidden_latest(self):
time.sleep(0.5)
item = self.friendMoment()[0]
latest_id = item['story_fbid']
title = item['title']
assert latest_id
result = self.momentAction(latest_id, action='HIDE')
print('隐藏最新一条:%s %s' % (title, result))
@lru_cache.memoize()
def url_get_uid(self, url):
if url:
id = re.findall(r'id=(\d+)', url)
if id:
return id[0]
else:
try:
m = re.compile(r'facebook.com/([0-9a-zA-Z._-]*)')
r = m.search(url)
if r:
name = r.group(1)
res = self.getIdFromUsername(name)
return res['uid']
except:
res = self._state._session.get(url)
id = re.findall(r'profile/(\d+)', res.text)
if id:
return id[0]
raise BaseException('无法获取id')
@retrying.retry(stop_max_attempt_number=2)
def _confirm_notifs(self):
data = {
'log_impressions': True,
'reloadcontent': False
}
res = self._post('/ajax/requests/loader/', data)
all_friend_main = parse_html.get_confirm_notifs(self, res)
confirm_friends = []
for url, name in all_friend_main:
id = self.url_get_uid(url)
if id not in self.confirm_friends:
self.confirm_friends.add(id)
confirm_friends.append({
'id': id,
'name': name
})
else:
break
return confirm_friends
def close_push(self, type, channel, toggle="OFF"):
# comments 评论
# tags 标记
# reminders 提醒
# other_interactions 更多与你有关的动态
# updates_from_friends 好友的更新
# friend 加好友请求
# pymk 可能认识
# birthday 生日
# group 小组
# video 视频
# events 活动
# pages_you_follow 你关注的主页
# marketplace 市场
# community_wellness_and_safety 筹款活动危机事件
# other 其他通知
params = {"notif_category": type, "notif_medium": channel, "toggle_value": toggle} # OFF/ON
action = base64.b64encode(f'NotificationMediumToggleServerActionToken;{json.dumps(params)}'.encode())
action = action.decode()
# mutation_id = "{}:{}".format(int(time.time() * 1000), random.randint(0, 4294967295) + 1)
mutation_id = self.get_client_mutation_id()
variables = {"input": {
"client_mutation_id": mutation_id, "actor_id": self.uid,
"action_type": action,
}}
res = self.graphql_api('FBNotificationOptionActionMutation', '1118102544967719', variables)
if res.get('data', {}).get('notif_option_action', {}):
return {'success': True}
return {'success': False}
def closeAllPush(self):
def close(self):
all_type = ['comments', 'tags', 'reminders', 'other_interactions', 'updates_from_friends',
'friend', 'pymk', 'birthday', 'group', 'video', 'events', 'pages_you_follow',
'marketplace', 'community_wellness_and_safety', 'other']
all_channel = ['email', 'sms', 'push']
for type in all_type:
for channel in all_channel:
self.close_push(type, channel)
threading.Thread(target=close, args=[self]).start()
return {'success': True}
def checkCanFriend(self, fbid):
salt = 'tjhg76ElADNCMBztPJmHqcjfaR6qqVVEdZfOyjlSYIVRgiTHgVvSpgSZ43HMkGLbv'
rand = 'ARBHk4SotjuZI-7' + ''.join([random.choice(salt) for _ in range(len(salt))])
from urllib.parse import quote
data = {
'endpoint': f'/ajax/hovercard/user.php?id={fbid}&extragetparams=%7B%22__tn__'
f'%22%3A%22%2Cdm-R-R%22%2C%22eid%22%3A%22{quote(rand)}%22%7D',
'fb_dtsg_ag': self._state.fb_dtsg_ag,
'extragetparams': json.dumps({"__tn__": ",dm-R-R", "eid": rand})
}
res = self._get(f'/ajax/hovercard/user.php?id={fbid}', data)
return parse_html.hovercard_get_addfriend(res)
def groupMembers(self, group_id, next_url=None):
if not next_url:
res = self._state._session.get(f'https://www.facebook.com/groups/{group_id}/members/')
fbids, next_url, member_num = parse_html.get_members_ids(res.text)
else:
res = self._get(next_url, {})
text = parse_html.get_domops_3(res)
fbids, next_url, member_num = parse_html.get_members_ids(text)
return {
'fbids': fbids,
'next_url': next_url,
'member_num': member_num,
}
def groupQuestion(self, group_id):
res = self.graphql_api('GroupMembershipQuestionnaireDialogContainerQuery', '2242896309090679',
{"groupID": group_id})
try:
info = res['data']['group']
info['visibility_sentence'] = info.get('visibility_sentence', {}).get('text', '')
info['group_member_profiles'] = info.get('group_member_profiles', {}).get('count', 0)
info.pop('profile_picture', None)
info.pop('group_default_actor', None)
info.pop('group_rules', None)
info.pop('can_viewer_see_rules', None)
membership_questions = info['membership_questions']
for question in membership_questions:
type_ = question['membership_question_type']
if type_ == 'PARAGRAPH':
question['membership_question_type'] = '问答题'
question.pop('membership_question_options', None)
elif type_ == 'MULTIPLE_CHOICE':
question['membership_question_type'] = '多选题'
options = question['membership_question_options']
new_options = []
for edge in options['edges']:
new_options.append(edge['node'])
question['membership_question_options'] = new_options
return {'success': True, 'info': info}
except Exception as e:
return {'success': False, 'error': repr(e)}
def friendConnect(self, fbid):
data = {"to_friend": fbid, "action": "confirm"}
j = self._payload_post("/ajax/add_friend/action.php?dpr=1", data)
return j
def FriendingCometFriendRequestConfirmMutation(self, friend_requester_id, source):
"""同意好友请求"""
#TODO:source 多种未抓,文档
var = {"input": {"cancelled_friend_requestee_id": friend_requester_id, "source": source, "actor_id": self.uid,
"client_mutation_id": self.get_client_mutation_id()}, "scale": 2}
res = self.graphql_api('FriendingCometFriendRequestCancelMutation', '3226051994092510', var)
try:
return {'success': True, 'data': res['data']}
except:
return {'success': False, "data": []}
def FriendingCometFriendRequestDeleteMutation(self, friend_requester_id, source):
"""移除好友请求"""
# FRIENDS_CENTER 朋友推荐列表处
# pymk_timeline_chain 好友的好友位置取消
# NETEGO 个人主页
var = {
"input": {"people_you_may_know_id": friend_requester_id, "people_you_may_know_location": source,
"actor_id": self.uid, "client_mutation_id": self.get_client_mutation_id()}}
res = self.graphql_api('FriendingCometFriendRequestDeleteMutation', '2882558265094181', var)
try:
return {'success': True, 'data': res['data']}
except:
return {'success': False, "data": []}
def FriendingCometFriendRequestCancelMutation(self, friend_requester_id, source):
"""取消好友请求"""
# pymk 朋友推荐列表处
# manage_outgoing_requests 查看已发出请求列表
# profile 主页位置取消
# pymk_timeline_chain 好友的好友位置取消
# pymk_feed 自己主页推荐好友处取消
# groups_member_list 公开小组成员
# search 搜索好友取消
var = {"input": {"cancelled_friend_requestee_id": friend_requester_id, "source": source,
"actor_id": self.uid, "client_mutation_id": self.get_client_mutation_id()},
"scale": 2}
res = self.graphql_api('FriendingCometFriendRequestCancelMutation', '3226051994092510', var)
try:
return {'success': True, 'data': res['data']}
except:
return {'success': False, "data": []}
def FriendingCometFriendRequestSendMutation(self, friend_requester_id, source):
"""添加好友请求"""
# people_you_may_know 可能认识的人
# profile_button 主页添加按钮
# pymk_timeline_chain 好友的好友
# groups_member_list 公开小组成员
# search 搜索添加
# netego_pymk 自己主页推荐好友添加
source_list = {
'people_you_may_know': 'friends_center',
'netego_pymk': 'netego',
'pymk_timeline_chain': 'pymk_timeline_chain'
}
var = {
"input": {"friend_requestee_ids": [friend_requester_id],
"refs": [None], "source": source, "actor_id": self.uid,
"client_mutation_id": self.get_client_mutation_id()}, "scale": 2}
if source in source_list.keys():
var['input']['people_you_may_know_location'] = source_list[source]
res = self.graphql_api('FriendingCometFriendRequestSendMutation', '4391497287589685', var)
try:
return {'success': True, 'data': res['data']}
except:
return {'success': False, "data": []}
def primaryLocation(self):
res = self._state._session.get('https://www.facebook.com/primary_location/info/')
return parse_html.get_location_info(res.text)
def searchForCityNew(self, city, peopleview=False):
data = {
'value': city,
'filter[0]': 'page',
'page_categories[0]': '2404',
'context': 'hub_current_city',
'viewer': self._uid,
}
if peopleview:
data['fb_dtsg_ag'] = self._state.fb_dtsg_ag
data['category'] = '2404'
data['context'] = 'hubs'
data['services_mask'] = 1
data['existing_ids'] = '' # 110970792260960,112677128908746,103097699730654
data['bsp'] = True
data['request_id'] = str(uuid.uuid1())
url = "https://www.facebook.com/ajax/typeahead/search.php"
res = self._payload_get(url, data)
if peopleview:
for item in res.get('entries'):
try:
item['name'] = item['name'].encode('unicode_escape').decode().replace('\\u', '%u')
except:
pass
try:
item['text'] = item['text'].encode('unicode_escape').decode().replace('\\u', '%u')
except:
pass
return res
def placeGetPage(self, placeid, cursor='', topic="美食"):
enum = {
'美食': '273819889375819',
'酒吧': '110290705711626',
'观光': '209889829023118',
'酒店': '164243073639257',
'购物': '200600219953504',
'夜生活': '191478144212980',
'咖啡': '197871390225897',
'博物馆': '197817313562497',
'户外': '635235176664335',
}
intersect = enum[topic]
if cursor:
data = {
'query': f'{placeid}/places-in/{intersect}/places/intersect',
'original_query': f'{placeid}/places-in/{intersect}/places/intersect',
'applied_rp_filters': [],
'applied_instant_filters': [],
'cursor': cursor
}
res = self._payload_post('/browse/async/places/', data)
result = res['results']
cursor = res['pagingOptions']['cursor']
else:
url = f'https://www.facebook.com/places/in/{placeid}/{intersect}/'
res = self._state._session.get(url)
result, cursor = parse_html.parse_places_group(res.text)
data = {
'result': result,
'cursor': cursor
}
return {'success': True if cursor else False, 'data': data}
def feedbackActions(self, photo_id, action='like', flag=True):
feedback = 'feedback:%s' % photo_id
try:
variables = {"input": {"client_mutation_id": self.get_client_mutation_id(), "actor_id": self.uid,
"feedback_id": base64.b64encode(feedback.encode()).decode(),
"feedback_reaction": 1,
"feedback_source": "PHOTOS_SNOWLIFT",
"feedback_referrer": "/profile.php",
"tracking": ['{"tn":"[email protected]*F"}'],
"session_id": str(uuid1())},
"useDefaultActor": True}
res = self.graphql_api('UFI2FeedbackReactMutation', '3412141278815236', variables)
if 'errors' in res:
return {'success': False, 'errors': res['errors']}
else:
return {'success': True, 'data': res['data']}
except Exception as e:
return {'success': False, 'errors': str(e)}
def likeProfile(self, fbid, flag=True):
type = 'profile'
action = 'like'
photo_id = self.getAlbumsCover(fbid, type)
if photo_id:
try:
self.graph.put_like(fbid + '_' + photo_id)
return {'success': True, 'photo_id': photo_id}
except GraphAPIError as err:
return {'success': False, 'photo_id': photo_id, 'errors': str(err)}
# return self.feedbackActions(photo_id, action, flag)
else:
return {'success': False, 'errors': "未获取到头像的photo_id"}
def likeCover(self, fbid, flag=True):
type = 'cover'
action = 'like'
photo_id = self.getAlbumsCover(fbid, type)
if photo_id:
return self.feedbackActions(photo_id, action, flag)
else:
return {'success': False, 'errors': "未获取到封面的photo_id"}
def joinGroup(self, group_id, flag=True):
if flag:
data = {
'group_id': group_id,
'client_custom_questions': 1
}
res = self._post('/groups/membership/r2j/', data)
try:
url = res['jsmods']['require'][0][3][0]
if '/groups/' in url:
return {'success': True}
except:
pass
return {'success': False}
else:
data = {
'ref': 'group_jump_header',
'group_id': group_id,
}
self._post('/ajax/groups/confirm_cancel_join_dialog/', data)
return {'success': True}
def answersQuestions(self, group_id, answers: list, accept_rules=True):
if isinstance(answers, str) and '[' in answers:
answers = json.loads(answers)
var = {"input": {"client_mutation_id": self.get_client_mutation_id(), "actor_id": self.uid,
"group_id": group_id, "answers": answers,
}}
if accept_rules:
var.update({'rules_agreement_status': 'accept_rules'})
res = self.graphql_api('GroupMembershipQuestionsAnswersSaveMutation', '2147338612011876', var)
try:
return {'success': True, 'data': res['data']}
except:
return {'success': False, "data": []}
def cloneMoment(self, story_fbid: str, is_multi_photo=False):
if story_fbid.startswith('S:'):
m = re.compile(r'S:_I(\d+):(\d+)[:]?')
g = m.search(story_fbid)
uid, sid = g.group(1), g.group(2)
story_fbid = '%s_%s' % (uid, sid)
elif '_' in story_fbid:
story_fbid = story_fbid
moment_obj = self.graph.get_object(story_fbid, fields='type,object_id,message,place,link')
print(moment_obj)
if moment_obj['type'] == 'photo':
if is_multi_photo:
uid, sid = story_fbid.split('_')
res = self._state._session.get(f'https://www.facebook.com/{uid}/posts/{sid}')
object_ids = parse_html.get_photo_attachments_list(res.text)
else:
object_id = moment_obj['object_id']
object_ids = [object_id]
attachments = []
for object_id in object_ids:
photo_obj = self.graph.get_object(object_id, fields='source,name')
source = photo_obj['source']
name = photo_obj.get('name', "")
if 'video' not in source:
result = self.graph.put_object('me', 'photos', published=False,
caption=name,
url=source)
attachments.append({"photo": {"id": str(result['id']), "tags": []}})
# TODO: 视频未解决
# result = self.graph.put_video_object('me', 'videos',
# title=name,
# file_url=source)
# attachments.append({"video": {"id": str(result['id']), "notify_when_processed": True}})
return self.sendMoment(moment_obj.get('message', ''), "PHOTO",
attachments=attachments,
location=moment_obj.get('place', {}).get('id', None))
elif moment_obj['type'] == 'status':
if 'id' in moment_obj.get('place', {}):
return self.sendMoment(moment_obj.get('message', ''), "LOCATION", location=moment_obj['place']['id'])
else:
return self.sendMoment(moment_obj.get('message', "无标题"), "TEXT")
elif moment_obj['type'] == 'link':
return self.shareLink(message=moment_obj.get('message', ''), link=moment_obj['link'])
def shareMoment(self, story_fbid: str, message=None):
if story_fbid.startswith('S:'):
m = re.compile(r'S:_I(\d+):(\d+)[:]?')
g = m.search(story_fbid)
uid, sid = g.group(1), g.group(2)
story_fbid = '%s_%s' % (uid, sid)
elif '_' in story_fbid:
story_fbid = story_fbid
uid, sid = story_fbid.split('_')
link = f'https://www.facebook.com/{uid}/posts/{sid}'
return self.shareLink(link, message)
def shareLink(self, link: str, message=None):
data = {
'link': link,
}
if message:
data['message'] = message
return self.graph.request('me/feed', method='POST', post_args=data)
@retrying.retry(stop_max_attempt_number=3)
def _fetch_sequence_id(self):
params = {
"limit": 1,
"tags": ["INBOX"],
"before": None,
"includeDeliveryReceipts": False,
"includeSeqID": True,
}
# Same request as in `Client.fetchThreadList`
(j,) = self._state._graphql_requests(_graphql.from_doc_id("1349387578499440", params))
print(common.get_thread_name(), "Fetching MQTT sequence ID")
try:
sync_sequence_id = int(j["viewer"]["message_threads"]["sync_sequence_id"])
print(common.get_thread_name(), "sequence ID:", sync_sequence_id)
return sync_sequence_id
except:
print(common.get_thread_name() + "!!!", "None sequence ID")
# self.call_logout_function()
raise
def listenStatus(self):
return {'status': self._mqtt.get_status()}
def fetchThreadMessages(self, thread_id=None, limit=20, before=None):
thread_id, thread_type = self._getThread(thread_id, None)
params = {
"id": thread_id,
"message_limit": limit,
"load_messages": True,
"load_read_receipts": True,
"before": before,
}
(j,) = self.graphql_requests(_graphql.from_doc_id("1860982147341344", params))
if j.get("message_thread") is None:
raise FBchatException("Could not fetch thread {}: {}".format(thread_id, j))
messages = []
for message in j["message_thread"]["messages"]["nodes"]:
msg = Message._from_graphql(message)
msg.source = message
messages.append(msg)
messages.reverse()
read_receipts = j["message_thread"]["read_receipts"]["nodes"]
for message in messages:
for receipt in read_receipts:
if int(receipt["watermark"]) >= int(message.timestamp):
message.read_by.append(receipt["actor"]["id"])
return messages
def setRelationsShip(self, status, partner_info: dict = None, privacy='EVERYONE'):
status_dict = {
'单身': '1',
'分手': '8',
'和TA在一起了': '2',
'已订婚': '5',
'已婚': '4',
'有同性伴侣': '10',
'有同居伴侣': '11',
'交往中,但保留交友空间': '3',
'比较复杂': '6',
'离婚': '9',
'丧偶': '7',
'---': '0',
}
if status not in status_dict:
raise Exception("无效状态,可选类型" + '|'.join(list(status_dict.keys())))
value = PostParam.__dict__.get(privacy).value
if not partner_info:
partner_info = {}
info = {
'status': status_dict[status],
'partner': partner_info.get('id', ''),
'anniversary_month': partner_info.get('anniversary_month', '-1'),
'anniversary_day': partner_info.get('anniversary_day', '-1'),
'anniversary_year': partner_info.get('anniversary_year', '-1'),
}
data = {'nctr[_mod]': 'pagelet_relationships',
'privacy[8787550733]': PostParam.EVERYONE.value}
data.update(info)
url = '/profile/edit/infotab/save/relationship/'
try:
res = self._post(url, data)
return {"success": True}
except:
return {"success": False}
def setAboutMe(self, textarea, privacy='EVERYONE'):
value = PostParam.__dict__.get(privacy).value
data = {
'textarea': textarea,
'privacy[8787635733]': PostParam.EVERYONE.value
}
url = '/profile/async/edit/infotab/save/about_me/'
try:
self._post(url, data)
return {"success": True}
except:
return {"success": False}
def addFamily(self, relation, family_id, privacy='EVERYONE'):
value = PostParam.__dict__.get(privacy).value
data = {
'family_text[0]': 'my',
'family_id[0]': family_id,
'privacy_fbid[0]': 0,
'relation[0]': relation,
'family_oldid[0]': '0',
'birth_month[0]': -1,
'birth_day[0]': -1,
'birth_year[0]': -1,
'privacy[0]': PostParam.EVERYONE.value
}
url = '/profile/async/edit/infotab/save/family/'
try:
res = self._post(url, data)
return {"success": True}
except:
return {"success": False}
def addWebsite(self, website_list: list, privacy='EVERYONE'):
value = PostParam.__dict__.get(privacy).value
if isinstance(website_list, str):
website_list = [website_list]
data = {
'privacy[8787375733]': PostParam.EVERYONE.value
}
for index, website in enumerate(website_list):
data[f'website_list[{index}]'] = website
url = '/profile/async/edit/infotab/save/website/'
try:
self._post(url, data)
return {"success": True}
except:
return {"success": False}
def addSocialContact(self, social_contacts: list, privacy='EVERYONE'):
value = PostParam.__dict__.get(privacy).value
data = {'privacy[8787335733]': value}
if isinstance(social_contacts, dict):
social_contacts = [social_contacts]
for index, social_contact in enumerate(social_contacts):
data[f'sn[{index}]'] = social_contact['sn']
data[f'sn_serv[{index}]'] = social_contact['sn_serv']
print(data)
url = '/profile/edit/infotab/save/screenname/'
try:
self._post(url, data)
return {"success": True}
except:
return {"success": False}
def approvalRelationShip(self, url):
r = self._state._session.get(url)
url, params = parse_html.get_approval_form(r.text)
res = self._post(url, params)
html = parse_html.get_domops_3(res)
return {'success': True, 'msg': parse_html.get_div_text(html)}
def momentBatch(self, story_actions: list):
for x in story_actions:
x['story_location'] = 'TIMELINE'
data = {"input": {
"client_mutation_id": self.get_client_mutation_id(),
"actor_id": self.uid,
"story_actions": story_actions
}}
res = self.graphql_api('BatchStoryCurationMutation', '1940054299401712', data)
return res
def momentList(self, count=15, startTime=None, endTime=None, cursor=None):
data = {
"count": count, "cursor": cursor, "id": self.uid, "taggedInOnly": False, "postedBy": {},
"startTime": startTime, "endTime": endTime
}
res = self.graphql_api('ProfileOverviewContainerQuery', '3218725658179182', data)
return res['data']['user']['timeline_feed_units']
def TimelineAppCollection(self, cursor=None):
"""
你的照片
"""
# 如果cursor为None,则请求 https://www.facebook.com/ana.sofia.583671/photos_all 获取到
if not cursor:
cursor = None
variables = {"count": 8,
"cursor": cursor,
"scale": 2, "id": "YXBwX2NvbGxlY3Rpb246MTAwMDAzMTQ5NTE2NDk5OjIzMDUyNzI3MzI6NQ=="}
try:
res = self.graphql_api('ProfileCometAppCollectionPhotosRendererPaginationQuery', '3217177821672058',
variables)
if 'errors' in res:
return {'success': False, 'errors': res['errors']}
else:
return {'success': True, 'data': res['data']}
except Exception as e:
return {'success': False, 'errors': str(e)}
def CometMediaViewerPhotoDeleteActionMutation(self, photo_id):
"""
删除照片
"""
variables = {"feedLocation": "COMET_MEDIA_VIEWER",
"input": {"photo_id": photo_id, "actor_id": self.uid,
"client_mutation_id": self.get_client_mutation_id()}, "isProfilePic": False, "scale": 2,
"renderLocation": None,
"privacySelectorRenderLocation": "COMET_MEDIA_VIEWER", "useDefaultActor": False}
try:
res = self.graphql_api('CometMediaViewerPhotoDeleteActionMutation', '3292223457513194',
variables)
if 'errors' in res:
return {'success': False, 'errors': res['errors']}
else:
return {'success': True, 'data': res['data']}
except Exception as e:
return {'success': False, 'errors': str(e)}
###############graph接口 start ##############
def checkVaild(self):
try:
res = self.graph.get_object('me')
return {'success': True}
except GraphAPIError as e:
return {'success': False, 'error': str(e)}
def photoNumber(self, fbid=None):
res = self.graph.get_connections(fbid or 'me', 'albums')
data = res['data']
size = 0
max_time = None
for single in data:
size += single['count']
time = datetime.datetime.strptime(single['updated_time'], '%Y-%m-%dT%H:%M:%S+0000')
if not max_time:
max_time = time
elif time > max_time:
max_time = time
return {'success': True, 'size': size, 'updated_time': (max_time and int(max_time.timestamp())) or 0}
def getAlbumsCover(self, fbid=None, type='profile'):
'''type可选为 cover profile wall'''
res = self.graph.get_connections(fbid or 'me', 'albums')
data = res['data']
cover_photo = None
for item in data:
if item['type'] == type:
cover_photo = item['cover_photo']
break
return cover_photo
def friendListNew(self, fbid=None, limit=None, after=None, before=None):
args = {
'fields': 'id,name,gender,birthday,location{location{city,country}}'
}
if limit:
args['limit'] = limit
if after:
args['after'] = after
if before:
args['before'] = before
if after and before:
raise Exception("错误参数,after/before 不能都有值 ")
try:
res = self.graph.get_connections(fbid or 'me', 'friends', **args)
res.get('paging', {}).pop('next', None)
res.get('paging', {}).pop('previous', None)
return {'success': True, **res}
except GraphAPIError as err:
return {'success': False, 'error': str(err)}
def infoNew(self, fbid):
fields = 'id,name,gender,locale,languages,link,updated_time,verified,birthday,education,email' \
',favorite_athletes,favorite_teams,work,picture{url},hometown{location{city,country,country_code}}' \
',location{location{city,country,country_code}}'
return self.graph.get_object(fbid, fields=fields)
def countryCode(self, place_id):
fields = 'id,name,location{city,country,country_code}'
return self.graph.get_object(place_id, fields=fields)
def mutliQuery(self, ids, unicode=False, include_fields: list = []):
ext_field = ''
if isinstance(include_fields, list) and include_fields:
ext_field = "," + ','.join(include_fields)
sql_tmpl = 'SELECT uid,name,friend_count,sex,current_location,locale,online_presence,birthday_date' + ext_field + ' FROM user WHERE uid = {}'
fql_queries = dict()
if isinstance(ids, str): ids = [ids]
for index, id in enumerate(ids):
if index >= 100:
break
fql_queries[id] = sql_tmpl.format(id)
fql_queries = parse.quote_plus(json.dumps(fql_queries))
fql_queries_url = "https://api.facebook.com/method/fql.multiquery?"
fql_queries_url += "access_token=%s&queries=%s&format=json" % (self.get_token(), fql_queries)
res = self._state._session.get(fql_queries_url)
try:
res = res.json()
for result_set in res:
try:
name = result_set['fql_result_set'][0]['name']
result_set['fql_result_set'][0]['langdetect'] = common.detectdetect(name)
if unicode:
try:
uname = name.encode('unicode_escape').decode().replace('\\u', '%u')
except:
uname = name
result_set['fql_result_set'][0]['name'] = uname
except:
pass
return res
except:
return res.text
def execute(self, sql):
f = furl.furl('https://graph.facebook.com/fql')
f.add({'q': sql, 'access_token': self.get_token()})
string = None
try:
res = self._state._session.get(f.url)
string = res.text
return res.json()
except:
if string:
return {'error': string}
else:
return {'error': '网络异常'}
def pageCommentList(self, page_id):
sql = 'SELECT fromid, text FROM comment ' \
'WHERE post_id IN (SELECT post_id FROM stream WHERE source_id = {})'.format(page_id)
return self.execute(sql)
def pagePostList(self, page_id):
sql = 'select post_id, created_time, type from stream where source_id={}'.format(page_id)
return self.execute(sql)
def postLikeList(self, post_id, limit=50, shown_ids: list = []):
if not shown_ids:
if '_' in post_id:
post_id = post_id[post_id.index('_') + 1:]
feedback = 'feedback:%s' % post_id
res = self.graphql_api('UFI2ReactionsCountTooltipContentQuery', '1579110358832101',
{"feedbackTargetID": base64.b64encode(feedback.encode()).decode()})
try:
return {'success': True, 'data': res['data']['feedback']['reactors']['nodes']}
except:
return {'success': False, "data": []}
else:
try:
f = furl.furl('https://www.facebook.com/ufi/reaction/profile/browser/fetch/')
ids = ','.join(shown_ids)
params = {
'total_count': self._get_reactions_like_count(post_id),
'limit': limit,
'reaction_type': 1,
'shown_ids': ids,
'ft_ent_identifier': post_id.split('_')[1],
'fb_dtsg_ag': self._state.fb_dtsg_ag
}
res = self._get(f.url, params)
data = parse_html.get_reaction_profile(res)
return {'success': True, 'data': data}
except Exception as e:
return {'success': False, "data": [], 'reason': str(e)}
def _get_reactions_like_count(self, post_id):
f = furl.furl('https://graph.facebook.com/v2.6/' + post_id)
f.add({
'fields': 'reactions.type(LIKE).summary(total_count)',
'access_token': self.get_token()
})
try:
res = self._state._session.get(f.url).json()
return res['reactions']['summary']['total_count']
except:
return 0
def MyFriendsList(self, exclude=[]):
all = {'name', 'friend_count', 'sex', 'current_location', 'locale', 'online_presence', 'birthday_date'} # pic
if exclude:
for a in all.copy():
if a in exclude: all.remove(a)
sql = f'SELECT uid,{",".join(list(all))} FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())'
return self.execute(sql)
def getIdFromUsername(self, username):
sql = f'SELECT uid, name,username FROM user WHERE username ="{username}"'
data = self.execute(sql)['data']
return data[0]
def showLargePicture(self, fbid='me'):
return self.graph.get_connections(fbid, 'picture', type='large', redirect=False)
@retrying.retry(stop_max_attempt_number=2)
def getGender(self, *ids):
fql_queries = dict()
for id in ids:
sql_tmpl = 'SELECT sex FROM user WHERE uid = {}'
fql_queries[id] = sql_tmpl.format(id)
fql_queries = parse.quote_plus(json.dumps(fql_queries))
fql_queries_url = "https://api.facebook.com/method/fql.multiquery?"
fql_queries_url += "access_token=%s&queries=%s&format=json" % (self.get_token(), fql_queries)
res = self._state._session.get(fql_queries_url)
res = res.json()
all = {}
for item in res:
try:
sex = 'boy' if item['fql_result_set'][0]['sex'] == 'male' else 'girl'
except:
sex = None
all[item['name']] = sex
return all
def sendPoke(self, fbid):
url = 'https://www.facebook.com/pokes/inline/'
data = {
'dom_id_replace': 'u_fetchstream_8_1',
'is_hide': 0,
'poke_target': fbid,
'ext': int(time.time()),
'hash': common.random_hash(),
}
res = self._post(url, data)
html = parse_html.get_domops_3(res)
if 'data-hovercard' in html:
return {'success': True, 'msg': parse_html.get_div_text(html)}
else:
return {'success': False, 'msg': parse_html.get_div_text(html)}
def checkPageCommunity(self, page_id):
result = self.graph.get_object(page_id, fields='is_community_page,were_here_count,link')
if result['is_community_page']:
return {'success': True}
else:
return {'success': False}
def _getVisitorsUrl(self, result):
response = self._state._session.get(result)
try:
url = 'https://www.facebook.com' + re.findall(r'href="(/search/\d+/visitors.*?)"', response.text)[0]
except:
url = None
return url
def searchVisitorsByPage(self, page_id, ext_data=None):
if not ext_data:
url = self._getVisitorsUrl('https://www.facebook.com/' + page_id)
if not url:
raise Exception("非公开主页")
r = self._state._session.get(url)
fbid = parse_html.get_all_raw_id(r.text)
data = parse_html.get_ext_data(r)
return {'fbid_list': fbid, 'ext_data': data}
else:
data = {'data': demjson.encode(ext_data), 'fb_dtsg_ag': self._state.fb_dtsg_ag}
res = self._state._get('https://www.facebook.com/ajax/pagelet/generic.php/BrowseScrollingSetPagelet', data)
fbid = parse_html.get_all_raw_id(res['payload'])
ext_data.pop('page_number', None)
ext_data.update(parse_html.get_ext_data(res))
return {
'fbid_list': fbid,
'ext_data': ext_data,
}
def momentDetail(self, story_id, include_fields=None):
default_fileds = {'created_time', 'message', 'name', 'picture', 'type', 'status_type', 'source'}
if include_fields and isinstance(include_fields, list):
for field in include_fields:
if not isinstance(field, str):
field = str(field)
default_fileds.add(field)
if not story_id: raise Exception('story_id 不能为空')
return self.graph.get_object(story_id, fields=','.join(default_fileds))
def likeObject(self, object_id):
try:
object_id = common.parse_post_id(object_id)
self.graph.put_like(object_id)
return {'success': True, 'object_id': object_id}
except Exception as e:
return {'success': False, 'error': str(e), 'object_id': object_id}
def commentObject(self, object_id, message=None, image_url=None):
try:
object_id = common.parse_post_id(object_id)
if not image_url:
return {'success': True, 'data': self.graph.put_comment(object_id, message), 'object_id': object_id}
else:
if not message:
data = self.graph.put_object(object_id, "comments", attachment_url=image_url)
else:
data = self.graph.put_object(object_id, "comments", message=message, attachment_url=image_url)
return {'success': True, 'data': data, 'object_id': object_id}
except Exception as e:
return {'success': False, 'error': str(e), 'object_id': object_id}
def feedListNew(self, fbid='me', simple=False, ext_data=None):
if ext_data and isinstance(ext_data, dict):
pass
else:
ext_data = {'limit': 20}
if simple:
ext_data['fields'] = 'id,created_time'
res = self.graph.request("{0}/{1}/{2}".format('v7.0', fbid, 'feed'), ext_data)
f = furl.furl(res['paging']['next'])
f.args.pop('access_token', None)
result = {
'items': res['data'],
'ext_data': dict(f.args)
}
return result
###############graph接口 end ##############
def _parse_payload(self, topic, m):
if topic == '/sr_res':
for frame in m['frames']:
try:
data = frame['data']['data']
sub_payload = json.loads(base64.b64decode(data).decode())
except:
continue
else:
if sub_payload.get('type') == 'skywalker' and sub_payload['topic'].startswith(
'gqls/web_notification_receive_subscribe'):
payload = json.loads(sub_payload['payload'])
notification = payload['web_notification_receive_subscribe']['notification']
notify = {}
notify['alert_id'] = notification['alert_id']
notify['creation_time'] = notification['creation_time']
notify['notif_type'] = notification['notif_type']
notify['url'] = notification['url']
notify['tracking'] = json.loads(notification['tracking'])
try:
notify['text'] = notification['title']['text']
notify['actor_id'] = notification['title']['ranges'][0]['entity']['id']
except:
notify['actor_id'] = None
self.onWebNotice(notify)
else:
super()._parse_payload(topic, m)
def onWebNotice(self, kwargs):
print(kwargs)
def get_client_mutation_id(self):
key = f'{self.uid}:client_mutation_id'
client_mutation_id = cache.get(key=key, default=0)
result = client_mutation_id + 1
cache.set(key=key, value=client_mutation_id + 1, ttl=24 * 60 * 60)
return str(result)
def graphql_api(self, friendly_name, doc_id, variables: dict, is_mutli_json=False):
data = {
'av': self.uid,
'fb_dtsg': self._state._fb_dtsg,
'jazoest': self._state.jazoest,
'fb_api_caller_class': 'RelayModern',
'fb_api_req_friendly_name': friendly_name,
'doc_id': doc_id,
'variables': json.dumps(variables)
}
if is_mutli_json:
return self._post_multi('/api/graphql/', data)
else:
return self._post('/api/graphql/', data)
def _post_multi(self, url, data, files=None):
data.update(self._state.get_params())
r = self._state._session.post(_util.prefix_url(url), data=data, files=files)
content = _util.check_request(r)
return common.parse_mutli_json(content)
def searchForPosts(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": f"keywords_blended_posts({text})", "role": "GRAMMAR",
"type": "SEE_ALL"}
filters = ['{"name":"merged_public_posts","args":""}']
return self._search_paginated_results_query(experience, text, ext_data, filters)
def searchForPeople(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "PEOPLE_TAB"}
return self._search_paginated_results_query(experience, text, ext_data, fields='profile')
def searchForVideo(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "VIDEOS_TAB"}
return self._search_paginated_results_query(experience, text, ext_data)
def searchForPhoto(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": f"photos-keyword({text})", "role": "GRAMMAR", "type": "SEE_ALL"}
return self._search_paginated_results_query(experience, text, ext_data, fields='url')
def searchForPages(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "PAGES_TAB"}
return self._search_paginated_results_query(experience, text, ext_data, fields='profile')
def searchForPlaces(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "PLACES_TAB"}
return self._search_paginated_results_query(experience, text, ext_data, fields='place_view_model')
def searchForGroupsNew(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "GROUPS_TAB"}
return self._search_paginated_results_query(experience, text, ext_data, fields='profile')
def searchForEvents(self, text, ext_data=None):
experience = {"fbid": None, "grammar_bqf": None, "role": None, "type": "EVENTS_TAB"}
return self._search_paginated_results_query(experience, text, ext_data, fields='profile')
def topGameList(self, ext_data=None):
variables = {"count": 12, "cursor": ext_data, "query_string": None, "scale": 2, "sort_order": None}
res = self.graphql_api('CometGamingVideoGameDirectoryListPaginationQuery', '3693217124027057', variables)
try:
items = [edge['node'] for edge in res['data']['gaming_video']['top_games']['edges']]
page_info = res['data']['gaming_video']['top_games']['page_info']
result = {
'items': items,
'has_next_page': page_info['has_next_page'],
'ext_data': page_info['end_cursor']
}
except:
result = {
'items': [],
'has_next_page': False,
'ext_data': None
}
return result
def topStreamerList(self, ext_data=None):
variables = {"count": 8, "cursor": ext_data, "query_string": None, "scale": 2}
res = self.graphql_api('CometGamingSuggestedStreamersSeeAllPaginationQuery', '3072924529443801', variables)
try:
items = [edge['node'] for edge in res['data']['gaming_video']['suggested_streamers']['edges']]
page_info = res['data']['gaming_video']['suggested_streamers']['page_info']
result = {
'items': items,
'has_next_page': page_info['has_next_page'],
'ext_data': page_info['end_cursor']
}
except:
result = {
'items': [],
'has_next_page': False,
'ext_data': None
}
return result
def topSmallGameList(self, ext_data=None):
variables = {"after": ext_data or "0", "first": 10, "game_type": 400}
res = self.graphql_api('CometInstantGamesHubContentPaginationQuery', '3070852522973571', variables)
try:
items = [edge['node'] for edge in res['data']['recommendedGames']['edges']]
page_info = res['data']['recommendedGames']['page_info']
result = {
'items': items,
'has_next_page': page_info['has_next_page'],
'ext_data': page_info['end_cursor']
}
except:
result = {
'items': [],
'has_next_page': False,
'ext_data': None
}
return result
def _search_paginated_results_query(self, experience, text, ext_data, filters=None, fields=None):
params = {"UFI2CommentsProvider_commentsKey": "CometSearchResultsInitialResultsQuery", "allow_streaming": False,
"args": {"callsite": "COMET_GLOBAL_SEARCH",
"config": {"bootstrap_config": None, "exact_match": False, "high_confidence_config": None,
"watch_config": None},
"context": {"bsid": common.random_uuid(), "tsid": str(random.random())},
"experience": experience,
"filters": filters or [],
"text": f"{text}"}, "count": 5,
"cursor": ext_data,
"displayCommentsContextEnableComment": False, "displayCommentsContextIsAdPreview": False,
"displayCommentsContextIsAggregatedShare": False, "displayCommentsContextIsStorySet": False,
"displayCommentsFeedbackContext": None, "feedLocation": "SEARCH", "feedbackSource": 23,
"fetch_filters": True,
"focusCommentID": None, "isComet": True, "privacySelectorRenderLocation": "COMET_STREAM",
"renderLocation": None, "scale": 2, "stream_initial_count": 0, "useDefaultActor": False}
res = self.graphql_api('CometSearchResultsPaginatedResultsQuery', '3386315934712504', params)
items = []
for node in res['data']['serpResponse']['results']['edges']:
view_model = node['relay_rendering_strategy']['view_model']
if fields:
view_model = view_model[fields]
if isinstance(view_model, dict):
for k, _ in view_model.copy().items():
if k.startswith('__'):
view_model.pop(k, None)
items.append(view_model)
page_info = res['data']['serpResponse']['results']['page_info']
result = {
'items': items,
'has_next_page': page_info['has_next_page'],
'ext_data': page_info['end_cursor']
}
return result
def feedsList(self, ext_data=None):
variables = {"RELAY_INCREMENTAL_DELIVERY": True, "UFI2CommentsProvider_commentsKey": "CometModernHomeFeedQuery",
"clientQueryId": common.random_uuid(), "connectionClass": "EXCELLENT", "count": 5,
"cursor": ext_data,
"displayCommentsContextEnableComment": None, "displayCommentsContextIsAdPreview": None,
"displayCommentsContextIsAggregatedShare": None, "displayCommentsContextIsStorySet": None,
"displayCommentsFeedbackContext": None, "feedLocation": "NEWSFEED", "feedStyle": "DEFAULT",
"feedbackSource": 1, "focusCommentID": None, "isComet": True, "orderby": ["TOP_STORIES"],
"privacySelectorRenderLocation": "COMET_STREAM", "recentVPVs": [], "refreshMode": None,
"renderLocation": "homepage_stream", "scale": 2, "useDefaultActor": False}
res = self.graphql_api('CometNewsFeedPaginationQuery', '4217891168228894', variables, is_mutli_json=True)
try:
result = {
'items': res['viewer']['news_feed']['edges'],
'has_next_page': res['viewer']['news_feed']['page_info']['has_next_page'],
'ext_data': res['viewer']['news_feed']['page_info']['end_cursor']
}
except:
result = {
'items': [],
'has_next_page': False,
'ext_data': None
}
return result
def playGame(self, game_id):
payload = {
"input": {"client_mutation_id": self.get_client_mutation_id(),
"actor_id": self.uid,
"app_id": game_id,
"grant_permissions": ["PUBLIC_INFO", "CONNECTED_PLAYERS", "FRIENDS_CAN_SEE",
"SOCIAL_GAME_ACTIVITY", "PUBLIC_INFO_V2"],
"enable_global_tos": False}}
self.graphql_api('InstantGamesRelayPermissions_GrantPermissionsMutation', '1405747992825083', payload)
payload = {
"input": {"client_mutation_id": self.get_client_mutation_id(),
"actor_id": self.uid,
"game_id": game_id, "request": "QE_PARAM_NO_EXPOSURE_FETCH",
"session_id": common.random_uuid(), "sdk_version": "6.3", "data": "{}"}}
self.graphql_api('InstantGamesPassThroughAgent_PassThroughMutation', '1753085494721576', payload)
return {'success': True}