forked from atyant-yadav/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagramphotodownloader.py
50 lines (50 loc) · 1.58 KB
/
instagramphotodownloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
""" Instagram Photo Downloader
----------------------------------------
"""
from sys import argv
import urllib
from bs4 import BeautifulSoup
import datetime
def ShowHelp():
print 'Insta Image Downloader'
print ''
print 'Usage:'
print 'insta.py [OPTION] [URL]'
print ''
print 'Options:'
print '-u [Instagram URL]\tDownload single photo from Instagram URL'
print '-f [File path]\t\tDownload Instagram photo(s) using file list'
print '-h, --help\t\tShow this help message'
print ''
print 'Example:'
print 'python insta.py -u https://instagram.com/p/xxxxx'
print 'python insta.py -f /home/username/filelist.txt'
print ''
exit()
def DownloadSingleFile(fileURL):
print 'Downloading image...'
f = urllib.urlopen(fileURL)
htmlSource = f.read()
soup = BeautifulSoup(htmlSource,'html.parser')
metaTag = soup.find_all('meta', {'property':'og:image'})
imgURL = metaTag[0]['content']
fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") + '.jpg'
urllib.urlretrieve(imgURL, fileName)
print 'Done. Image saved to disk as ' + fileName
if __name__ == '__main__':
if len(argv) == 1:
ShowHelp()
if argv[1] in ('-h', '--help'):
ShowHelp()
elif argv[1] == '-u':
instagramURL = argv[2]
DownloadSingleFile(instagramURL)
elif argv[1] == '-f':
filePath = argv[2]
f = open(filePath)
line = f.readline()
while line:
instagramURL = line.rstrip('\n')
DownloadSingleFile(instagramURL)
line = f.readline()
f.close()