Skip to content

Instantly share code, notes, and snippets.

@guimondmm
Created March 21, 2018 14:01
Show Gist options
  • Save guimondmm/99823acda9223a8132fc31ae0213c760 to your computer and use it in GitHub Desktop.
Save guimondmm/99823acda9223a8132fc31ae0213c760 to your computer and use it in GitHub Desktop.
Find stubborn Facebook posts that won't delete from the Activity Log. Those posts might reappear on your Timeline if they are not fully deleted.
#!/usr/bin/env python3
## Find stubborn Facebook posts that won't delete from the Activity Log.
## Those posts might reappear on your Timeline if they are not fully deleted.
import http, json, sys, webbrowser
try:
import requests
except ImportError:
sys.exit("\x1b[1;33mPlease install the \"requests\" module using\n$ pip3 install requests\x1b[0m")
try:
token = sys.argv[1] # access_token from https://developers.facebook.com/tools/explorer/
except IndexError:
print("\x1b[1;33mPass the Facebook Graph API access_token as an argument.\nhttps://developers.facebook.com/tools/explorer/\x1b[0m")
sys.exit(1)
def getFacebookFeed(url):
r = requests.get(url)
if r.status_code is not 200: # 200 OK
sys.exit("\x1b[1;31m" + str(r.status_code) + '\x20' + http.HTTPStatus(r.status_code).phrase + "\nIs the access_token expired? https://developers.facebook.com/tools/explorer/\x1b[0m") # e.g. 400 Bad Request
graph = json.loads(r.text) # parses the response from Facebook as a Python object
postURLs = [] # the posts' permalinks
try:
for post in graph["data"]:
postURLs.append(post["permalink_url"])
except:
pass
try:
nextPage = graph["paging"]["next"] # Facebook lists a limited number of posts per page
except KeyError:
nextPage = None
return postURLs, nextPage
baseURL = "https://graph.facebook.com/v2.12/me/feed?fields=id%2Cpermalink_url&access_token=" + token
print("Fetching a list of posts from Facebook. Please wait.")
postURLs, nextPage = getFacebookFeed(baseURL)
while nextPage is not None:
nextURLs, nextPage = getFacebookFeed(nextPage)
postURLs.extend(nextURLs)
if len(postURLs) > 0:
print("\x1b[1;32m" + str(len(postURLs)) + "\x1b[0m\x1b[1m post" + (" was" if len(postURLs) == 1 else "s were") + " found.\x1b[0m\nUnfortunately, they cannot be deleted using the Facebook API.\nThey will be opened in the default web browser, max. 10 tabs at a time.")
for i in range(0, len(postURLs)):
if i % 10 == 0:
try:
input("Press \x1b[1mEnter\x1b[0m to open the browser or \x1b[1mCTRL+C\x1b[0m to quit.")
except KeyboardInterrupt:
sys.exit(0)
webbrowser.open_new_tab(postURLs[i])
else:
print("No posts were found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment