Skip to content

Instantly share code, notes, and snippets.

@jinhoyoo
Last active December 16, 2023 14:56
Show Gist options
  • Save jinhoyoo/0e7799d371e297dd28e91b8404001ada to your computer and use it in GitHub Desktop.
Save jinhoyoo/0e7799d371e297dd28e91b8404001ada to your computer and use it in GitHub Desktop.
Crawl your facebook post for retrospective for V9.0
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
from datetime import datetime
import requests
# Input data. Get the data from GRAPH API explorer. (https://developers.facebook.com/tools/explorer/?classic=1)
ACCESS_TOKEN="YOUR_GRAPH_API_TOKEN"
YEAR ="THE_YEAR_YOU_WANNA_CRAWL_THE_POSTS"
# 1st request.
def get_post(url, year):
facebook_post_head_url = "https://www.facebook.com/"
first_day = datetime.strptime(f'{year}-1-1T00:00:00+0000', "%Y-%m-%dT%H:%M:%S+0000")
headers = {'content-type': 'application/json'}
rs = requests.get(url=url, headers=headers)
if rs.status_code == 200:
posts = rs.json()["data"]
next_url = rs.json()["paging"]["next"]
for p in posts:
current_date_time = datetime.strptime(p['created_time'], "%Y-%m-%dT%H:%M:%S+0000")
if 'message' in p:
post_msg = p['message'].replace("\n"," ")
post_id = p['id'].split('_')[1]
print( f"{current_date_time}; {post_msg};{facebook_post_head_url}/{post_id}" )
if current_date_time > first_day:
get_post(next_url, year)
else:
print("Not accessible")
def main():
# Seed URL for your post.
seed_url= f"https://graph.facebook.com/v15.0/me/feed?format=json&access_token={ACCESS_TOKEN}"
get_post(seed_url, YEAR)
if __name__ == "__main__":
main()
@jinhoyoo
Copy link
Author

upgraded for V9.0 Graph API and gave the link of the post.

@jinhoyoo
Copy link
Author

Upgrade v15.0 version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment