Raise exceptions instead of printing Quatsch and then returning
This commit is contained in:
parent
0df54143e5
commit
a3282ed7a5
13
instagram.py
Normal file → Executable file
13
instagram.py
Normal file → Executable file
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
import requests, re, json, datetime, shutil, os
|
import requests, re, json, datetime, shutil, os
|
||||||
|
|
||||||
|
class DownloaderException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def get_json(name, id = 0):
|
def get_json(name, id = 0):
|
||||||
r = requests.get('http://www.instagram.com/'+name, \
|
r = requests.get('http://www.instagram.com/'+name, \
|
||||||
params={'max_id': id})
|
params={'max_id': id})
|
||||||
@ -45,7 +48,7 @@ def download_pic(name, url, date_epoch, outputlabel=None):
|
|||||||
shutil.copyfileobj(r.raw, f)
|
shutil.copyfileobj(r.raw, f)
|
||||||
os.utime(filename, (datetime.datetime.now().timestamp(), date_epoch))
|
os.utime(filename, (datetime.datetime.now().timestamp(), date_epoch))
|
||||||
else:
|
else:
|
||||||
print("ERROR: file \'" + url + "\' could not be downloaded")
|
raise DownloaderException("file \'" + url + "\' could not be downloaded")
|
||||||
|
|
||||||
def saveCaption(name, date_epoch, caption):
|
def saveCaption(name, date_epoch, caption):
|
||||||
filename = name.lower() + '/' + epochToString(date_epoch) + '.txt'
|
filename = name.lower() + '/' + epochToString(date_epoch) + '.txt'
|
||||||
@ -92,20 +95,18 @@ def download_profilepic(name, url):
|
|||||||
shutil.copyfileobj(r.raw, f)
|
shutil.copyfileobj(r.raw, f)
|
||||||
os.utime(filename, (datetime.datetime.now().timestamp(), date_object.timestamp()))
|
os.utime(filename, (datetime.datetime.now().timestamp(), date_object.timestamp()))
|
||||||
else:
|
else:
|
||||||
print("ERROR: file \'" + url + "\' could not be downloaded")
|
raise DownloaderException("file \'" + url + "\' could not be downloaded")
|
||||||
|
|
||||||
def download(name, ProfilePicOnly = False, DownloadVideos = True):
|
def download(name, ProfilePicOnly = False, DownloadVideos = True):
|
||||||
data = get_json(name)
|
data = get_json(name)
|
||||||
totalcount = data["entry_data"]["ProfilePage"][0]["user"]["media"]["count"]
|
totalcount = data["entry_data"]["ProfilePage"][0]["user"]["media"]["count"]
|
||||||
if len(data["entry_data"]) == 0:
|
if len(data["entry_data"]) == 0:
|
||||||
print("ERROR: user does not exist")
|
raise DownloaderException("user does not exist")
|
||||||
return None
|
|
||||||
else:
|
else:
|
||||||
download_profilepic(name, data["entry_data"]["ProfilePage"][0]["user"]["profile_pic_url"])
|
download_profilepic(name, data["entry_data"]["ProfilePage"][0]["user"]["profile_pic_url"])
|
||||||
if len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0 \
|
if len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0 \
|
||||||
and not ProfilePicOnly:
|
and not ProfilePicOnly:
|
||||||
print("ERROR: no pics found")
|
raise DownloaderException("no pics found")
|
||||||
return None
|
|
||||||
if not ProfilePicOnly:
|
if not ProfilePicOnly:
|
||||||
count = 1
|
count = 1
|
||||||
while not get_last_id(data) is None:
|
while not get_last_id(data) is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user