Sixem

Python Instagram Downloader

Dec 5th, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. #!/usr/bin/python
  2. #Usage: Scroll down to the bottom of a instagram page, copy the LIVE HTML source and save it as a file.
  3. #Usage: Then start the script with "source.html" "/home/me/downloads"
  4. import sys
  5. import urllib2
  6. import re
  7. import os
  8. from os.path import basename
  9. x=0
  10. infomsg=('\033[92m<INFO>\033[0m')
  11. completemsg=('\033[94m<COMPLETE>\033[0m')
  12. container=[]
  13. sorted_container=[]
  14. rpath=str(sys.argv[1])
  15. dpath=str(sys.argv[2])
  16. with open(rpath, 'r') as ss:
  17.     data=ss.read().replace('\n', '')
  18. print('%s Input file is \033[94m%s\033[0m containing %s characters' % (infomsg,basename(rpath),len(data)))
  19. parts=data.split('instagram.com/')
  20. for i in parts:
  21.     if i.startswith('p/'):
  22.         container.append((i.split('/')[1]))
  23. [sorted_container.append(item) for item in container if item not in sorted_container]
  24. print('%s Analyzed code and found %s items out of %s potention items' % (infomsg,len(sorted_container),len(container)))
  25. print('%s Initiating download' % infomsg)
  26. for p in sorted_container:
  27.     url=('http://instagram.com/p/%s/' % p)
  28.     req=urllib2.Request(url,headers={'User-Agent' : 'Mozilla Firefox'})
  29.     u=urllib2.urlopen(req)
  30.     iurl=(str(u.read().split('<meta property="og:image" content="')[1]).split('"')[0])
  31.     req=urllib2.Request(iurl,headers={'User-Agent' : 'Mozilla Firefox'})
  32.     i=urllib2.urlopen(req)
  33.     sFile=open(('%s/%s' % (dpath,p)),'w')
  34.     sFile.write(i.read())
  35.     sFile.close()
  36.     if os.path.isfile(('%s/%s' % (dpath,p))):
  37.         x+=1
  38.         print('%s %s %s/%s' % (completemsg,p,x,len(sorted_container)))
  39. print('%s %s/%s images downloaded!' % (completemsg,x,len(sorted_container)))
Advertisement
Add Comment
Please, Sign In to add comment