From a3282ed7a5afe3dfe801c233b82b33b14bd1fb9f Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 17 Jun 2016 21:38:21 +0200 Subject: [PATCH] Raise exceptions instead of printing Quatsch and then returning --- instagram.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) mode change 100644 => 100755 instagram.py diff --git a/instagram.py b/instagram.py old mode 100644 new mode 100755 index f85d6e5..bcfc9a0 --- a/instagram.py +++ b/instagram.py @@ -2,6 +2,9 @@ import requests, re, json, datetime, shutil, os +class DownloaderException(Exception): + pass + def get_json(name, id = 0): r = requests.get('http://www.instagram.com/'+name, \ params={'max_id': id}) @@ -45,7 +48,7 @@ def download_pic(name, url, date_epoch, outputlabel=None): shutil.copyfileobj(r.raw, f) os.utime(filename, (datetime.datetime.now().timestamp(), date_epoch)) else: - print("ERROR: file \'" + url + "\' could not be downloaded") + raise DownloaderException("file \'" + url + "\' could not be downloaded") def saveCaption(name, date_epoch, caption): filename = name.lower() + '/' + epochToString(date_epoch) + '.txt' @@ -92,20 +95,18 @@ def download_profilepic(name, url): shutil.copyfileobj(r.raw, f) os.utime(filename, (datetime.datetime.now().timestamp(), date_object.timestamp())) else: - print("ERROR: file \'" + url + "\' could not be downloaded") + raise DownloaderException("file \'" + url + "\' could not be downloaded") def download(name, ProfilePicOnly = False, DownloadVideos = True): data = get_json(name) totalcount = data["entry_data"]["ProfilePage"][0]["user"]["media"]["count"] if len(data["entry_data"]) == 0: - print("ERROR: user does not exist") - return None + raise DownloaderException("user does not exist") else: download_profilepic(name, data["entry_data"]["ProfilePage"][0]["user"]["profile_pic_url"]) if len(data["entry_data"]["ProfilePage"][0]["user"]["media"]["nodes"]) == 0 \ and not ProfilePicOnly: - print("ERROR: no pics found") - return None + raise DownloaderException("no pics found") if not ProfilePicOnly: count = 1 while not get_last_id(data) is None: