This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
import json | |
from pymongo import MongoClient | |
import time | |
def fetchAndStash(url, client): | |
try: | |
response = urllib.request.urlopen(url + '&count=100') | |
f = response.read() | |
payload = f.decode('utf-8') | |
j = json.loads(payload) | |
if j['meta']['code'] == 200 : # Request worked | |
for i in j['data']: # Loop through Instagram posts and save them | |
try: | |
# Hardcoded Database/Collection names.. change this | |
result = client.DeepDress.InstagramV4.insert({'_id' : i['link'], 'payload' : i, 'image' : i['images']['low_resolution']['url']}) | |
except: | |
print("bad batch url: " + url + '; tried to insert ' + i) | |
with open('error', 'a') as of: | |
of.write(f.decode('utf-8')) | |
time.sleep(1) # Be nice | |
fetchAndStash(j['pagination']['next_url'], client) # Follow the white rabbit | |
else : | |
print(url) | |
except: | |
print("Failed url ", url) | |
def main(): | |
client = MongoClient() | |
url = 'https://api.instagram.com/v1/tags/renttherunway/media/recent/?client_id=your_client_id_here' | |
fetchAndStash(url, client) # could hit stack overflow.. but unlikely | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment