Skip to content

Instantly share code, notes, and snippets.

@sumonst21
Created May 13, 2023 19:47
Show Gist options
  • Save sumonst21/6eb20e79e8b600113df2f0351cfd595d to your computer and use it in GitHub Desktop.
Save sumonst21/6eb20e79e8b600113df2f0351cfd595d to your computer and use it in GitHub Desktop.
For my little brother
import facebook
import requests
import json
import os
# Your access token
#access_token = '<your-access-token>'
access_token = os.environ.get('FACEBOOK_ACCESS_TOKEN')
# The Facebook API endpoint to get your posts
api_endpoint = f'https://graph.facebook.com/v16.0/me?fields=id,name,posts&access_token={access_token}'
# The keyword to search for in post messages
keyword = 'my'
# Send the initial request to the API
response = requests.get(api_endpoint)
data = response.json()
# Loop through all posts, handling pagination as needed
while True:
# Get the list of posts in the current data set
posts = data['posts']['data']
# Loop through each post
for post in posts:
# Check if the post message contains the keyword
if 'message' in post and keyword in post['message']:
# Do something with the post ID, like print it out
#print(post['id'])
# post url
print(f"https://www.facebook.com/{post['id']}")
# Check if there is another page of posts to fetch
if 'paging' in data and 'next' in data['paging']:
# If there is, send another request using the next page URL
next_page_url = data['paging']['next']
response = requests.get(next_page_url)
data = response.json()
else:
# If there are no more pages, break out of the loop
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment