Skip to content

Instantly share code, notes, and snippets.

@AnimeshShaw
Created June 28, 2015 19:52
Show Gist options
  • Save AnimeshShaw/24d51dc5f1a242d7d499 to your computer and use it in GitHub Desktop.
Save AnimeshShaw/24d51dc5f1a242d7d499 to your computer and use it in GitHub Desktop.
Python-Programming-Society File Download Bot
"""
Python Programming Society Files Downloader
===========================================
Python Programmers Society is a Facebook group which connects people who love
to program in Python and want to spread the word about the fastest growing
programming language in the world.
Total Members :- 36,600+ and Counting.
Author: Psycho_Coder <Animesh Shaw>
Date: 29th June, 2015.
"""
import csv
from urllib import unquote
import facepy
import requests
access_token = "Your Token"
try:
graph = facepy.GraphAPI(access_token, verify_ssl_certificate=True, timeout=3000, version='2.3')
except facepy.OAuthError as e:
print(e.message)
print("Connection Established")
grp_id = "714685238558448"
lim = 500
files_dict = graph.get(grp_id + "/files", retry=5, limit=lim, fields='download_link,id,updated_time')
print("Records have been fetched from the Group.")
files_count = len(files_dict['data'])
print("Number of Files present in the Group :- " + str(files_count))
download_links = {}
csv_db_fieldnames = ['id', 'filename', 'download_link', 'updated_time']
#Save all the File names with other metadata as CSV file for records
with open('PPS-Fileddb.csv', 'wb') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_db_fieldnames)
writer.writeheader()
for i in range(files_count):
link = files_dict['data'][i]['download_link']
file_name = link[link.rfind('/') + 1:]
#Save the link in a new dictionary which we refer to download the files using requests
download_links[file_name] = link
files_dict['data'][i]['filename'] = file_name
writer.writerow(files_dict['data'][i])
print("All the records have been written to CSV file and saved in the current directory.")
#Location to Save the files
save_dir = "/home/psychocoder/Documents/PPS/"
#Download the files using Python requests library.
for filename in download_links.iterkeys():
req = requests.get(download_links.get(filename), stream=True)
filename = unquote(filename)
with open(save_dir + filename, 'wb') as f:
print("Downloading File :- " + filename)
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
print("\nAll Files have been downloaded. Keep Learning and Love Anime Boobies :D")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment