SHARE
TWEET

Python Instagram Ripper

jamiepfuller Jan 25th, 2017 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. #-*-coding:utf8;-*-
  3. #qpy:2
  4. #qpy:console
  5.  
  6. #
  7. # A (very ugly) script to grab all Images from (PUBLIC) Instagram for a collection of users and send a pushbullet notification.
  8. # On subsequent runs it will only download new images so can be scheduled under crontab to monitor and rip many accounts
  9. # This script will automatically create sub-folders for each of the collection of (Public) users in the "people" list.
  10. # but does require the initial save path to exist.
  11. #
  12. # Written by Jamie Fuller 2017, more on https://www.jamiefuller.com
  13. #
  14.  
  15.  
  16.  
  17.  
  18.  
  19. import urllib2
  20. import socket
  21. import sys
  22. import string
  23. import os
  24. from os.path import basename
  25.  
  26. ## the list below should be populated with the people you wish to download. (WARNING, the jamiepfuller account contains some NSFW material)
  27. people=["jamiepfuller","artduino","discovering_mechatronics"]
  28.  
  29. ## the variable below controls the save path, please ensure this exists and is writable.
  30. savepath="/home/pi/Instagram/"
  31.  
  32. summary=""
  33. for p in people:
  34.     if not os.path.exists(savepath+p):
  35.         os.makedirs(savepath+p)
  36.     count=0
  37.     noteof=0
  38.     cursor=""
  39.     while noteof==0:
  40.         url = 'https://www.instagram.com/%s/%s' % (p,cursor)
  41.         print url
  42.         request=urllib2.Request(url)
  43.         try:
  44.             usock = urllib2.urlopen(request)
  45.         except urllib2.HTTPError as e:
  46.             error_message = e.read()
  47.             print error_message
  48.         else:
  49.             try:
  50.                 data = usock.read()
  51.             except:
  52.                 print "\n\n\nerror!!!!"
  53.             else:
  54.                 if data.find('"has_next_page": true')>0:
  55.                     cursor='?max_id=%s' % data[data.find('end_cursor')+14:data.find('"',data.find('end_cursor')+14)]
  56.                 else:
  57.                     noteof=1
  58.                 usock.close()
  59.                 arr=data.split('display_src')
  60.                 ars=iter(arr)
  61.                 next(ars)
  62.                 for a in ars:
  63.                     if (a.find(".jpg")>0):
  64.                         url2=a[4:a.find(".jpg")] + ".jpg"
  65.                         file_name=basename(url2)
  66.                         if os.path.isfile(savepath+p+"/"+file_name) :
  67.                             print "skipping: %s" % file_name
  68.                             noteof=1
  69.                             break
  70.                         else:
  71.                             print "%d, Downloading: %s" % (count+1,file_name)
  72.                             request=urllib2.Request(url2)
  73.                             try:
  74.                                 usock = urllib2.urlopen(request, timeout=10)
  75.                             except urllib2.HTTPError as e:
  76.                                 error_message = e.read()
  77.                                 print error_message
  78.                             except socket.timeout as e:
  79.                                 print type(e)  
  80.                             else:
  81.                                 f = open(savepath+p+"/"+file_name, 'wb')
  82.                                 try:
  83.                                     data2 = usock.read()
  84.                                 except urllib2.HTTPError as e:
  85.                                     error_message = e.read()
  86.                                     print error_message
  87.                                 except socket.timeout as e:
  88.                                     print type(e)  
  89.                                 else:
  90.                                     usock.close()
  91.                                     f.write(data2)
  92.                                 f.close()
  93.                                 count=count+1
  94.     if (count>0): summary="%s\n%d new image(s) for %s" % (summary,count,p)
  95.        
  96.    
  97. if summary<>"":
  98.     print summary
  99.     #remember to add your own details for pushbullet below.
  100.     call="curl -u YOURACCESSTOKENGOESHERE: https://api.pushbullet.com/v2/pushes -d type=note -d title='Instagram updates' -d body='%s' -d device_iden=YOURDEVICEIDGOESHERE" % summary
  101.     response=os.system(call + " > /dev/null")
RAW Paste Data
Top