Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit 3d8aafd

Browse files
committed
who unfriended me, added
1 parent 61ea8d0 commit 3d8aafd

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
*.csv
22
*.log
33
*.json
4+
*.bat
45
config.txt
5-
chromedriver.exe
6-
run.bat
6+
chromedriver.exe

facebook-friends.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# --------------- Import -----------------
2-
import os, datetime, time, csv
2+
import os, datetime, time, csv, pprint
33
from selenium import webdriver
44
from selenium.webdriver.chrome.options import Options
55
from selenium.webdriver.common.by import By
@@ -8,7 +8,7 @@
88
from sys import argv
99
import random
1010
# --------------- -----------------
11-
print("\n" * 10)
11+
print("\n" * 50)
1212
os.environ["DEBUSSY"] = "1"
1313
# --------------- Configure browser session -----------------
1414
wd_options = Options()
@@ -48,17 +48,8 @@ def scroll_to_bottom():
4848
def scan_friends():
4949
print('Scanning page for friends...')
5050
friends = []
51-
# friend_cards = browser.find_elements_by_xpath("//div[@data-sigil='undoable-action']/a")
5251
friend_cards = browser.find_elements_by_css_selector("div#root div.timeline div[data-sigil='undoable-action'] a")
5352
for friend in friend_cards:
54-
# if friend.get_attribute('data-hovercard') is None:
55-
# print(" %s (INACTIVE)" % friend.text)
56-
# friend_id = friend.get_attribute('ajaxify').split('id=')[1]
57-
# friend_active = 0
58-
# else:
59-
# print(" %s" % friend.text)
60-
# friend_id = friend.get_attribute('data-hovercard').split('id=')[1].split('&')[0]
61-
# friend_active = 1
6253
friend_name = friend.text
6354
friend_id = friend.get_attribute('href')
6455
if friend_name:
@@ -87,12 +78,7 @@ def scrape_1st_degrees():
8778
writer = csv.writer(open(csvOut, 'w', encoding="utf-8"))
8879
writer.writerow(['id','name'])
8980

90-
#Get your unique Facebook ID
91-
# profile_icon = browser.find_element_by_css_selector("[data-click='profile_icon'] > a > span > img")
92-
# myid = profile_icon.get_attribute("id")[19:]
93-
# profile_icon = browser.find_element_by_css_selector("[id='profile_tab_jewel'] > a")
94-
# myid = profile_icon.get_attribute("href")
95-
# myid = 0
81+
#Get my Facebook id
9682
time.sleep(1)
9783
browser.get("https://m.facebook.com/home.php")
9884
time.sleep(1)
@@ -113,13 +99,15 @@ def scrape_1st_degrees():
11399
writer.writerow([friend['id'],str(friend['name'])])
114100

115101
print("Successfully saved to %s" % csvOut)
102+
103+
return csvOut
116104
# --------------- Scrape 2nd degree friends. ---------------
117105
#This can take several days if you have a lot of friends!!
118106
def scrape_2nd_degrees():
119107
#Prep CSV Output File
120108
csvOut = '2nd-degree_%s.csv' % now.strftime("%Y-%m-%d_%H%M")
121109
writer = csv.writer(open(csvOut, 'w', encoding="utf-8"))
122-
writer.writerow(['A_id', 'B_id', 'A_name','B_name','active'])
110+
writer.writerow(['A_id', 'B_id', 'A_name','B_name'])
123111

124112
#Load friends from CSV Input File
125113
script, filename = argv
@@ -128,7 +116,7 @@ def scrape_2nd_degrees():
128116
print("------------------------------------------")
129117
for idx,friend in enumerate(myfriends):
130118
#Load URL of friend's friend page
131-
scrape_url = "https://www.facebook.com/"+ friend['uid'] + "/friends?source_ref=pb_friends_tl"
119+
scrape_url = "https://m.facebook.com/"+ friend['uid'] + "/friends?source_ref=pb_friends_tl"
132120
browser.get(scrape_url)
133121

134122
#Scan your friends' Friends page (2nd-degree friends)
@@ -139,10 +127,34 @@ def scrape_2nd_degrees():
139127
#Write friends to CSV File
140128
print('Writing friends to CSV...')
141129
for person in their_friends:
142-
writer.writerow([friend['uid'],person['id'],friend['name'],person['name'],person['active']])
130+
writer.writerow([friend['uid'],person['id'],friend['name'],person['name']])
131+
132+
print("friend #%d done" % (idx+1))
133+
134+
print("Successfully saved to %s" % csvOut)
143135
# --------------- Check Disconnected Friends ---------------
144136
def who_unfriended_me():
145-
137+
#get old frineds and scrape current friends
138+
#then compare between them
139+
script, filename, action = argv
140+
old_friends = load_csv(filename)
141+
current_friends = load_csv(scrape_1st_degrees())
142+
disconnections = [x for x in old_friends if x not in current_friends]
143+
144+
print("\n" * 10)
145+
print("=== Who Unfriended Me? ===\n\n")
146+
pp = pprint.PrettyPrinter(indent=4)
147+
pp.pprint(disconnections)
148+
print("\n" * 10)
149+
150+
#save to file
151+
csvOut = '1st-degree-disconnections_%s.csv' % now.strftime("%Y-%m-%d_%H%M")
152+
writer = csv.writer(open(csvOut, 'w', encoding="utf-8"))
153+
writer.writerow(['name','id'])
154+
for friend in disconnections:
155+
writer.writerow([friend['uid'],friend['name']])
156+
157+
print("Successfully saved to %s" % csvOut)
146158
# --------------- Start Scraping ---------------
147159
now = datetime.now()
148160
configPath = "config.txt"
@@ -153,12 +165,14 @@ def who_unfriended_me():
153165
password = configObj.get('credentials', 'password')
154166
else:
155167
print('Enter the config path')
156-
# fb_login(configObj)
168+
fb_login(configObj)
157169

158-
if len(argv) is 1:
159-
# scrape_1st_degrees()
160-
print(load_csv("1st-degree_2021-05-26_1554.csv"))
161-
elif len(argv) is 2:
170+
if len(argv) == 1:
171+
scrape_1st_degrees()
172+
elif len(argv) == 2:
162173
scrape_2nd_degrees()
174+
elif len(argv) == 3:
175+
script, filename, action = argv
176+
if action == "un": who_unfriended_me()
163177
else:
164178
print("Invalid # of arguments specified. Use none to scrape your 1st degree connections, or specify the name of the CSV file as the first argument.")

0 commit comments

Comments
 (0)