Wait longer after HTTP err 429 (Too Many Requests)
This commit is contained in:
parent
566ef02b94
commit
ad34fc09b6
@ -7,12 +7,12 @@ import getpass
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import random
|
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import textwrap
|
||||||
import time
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
from argparse import ArgumentParser, SUPPRESS
|
from argparse import ArgumentParser, SUPPRESS
|
||||||
@ -83,6 +83,10 @@ class ConnectionException(InstaloaderException):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TooManyRequests(ConnectionException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_default_session_filename(username: str) -> str:
|
def get_default_session_filename(username: str) -> str:
|
||||||
"""Returns default session filename for given username."""
|
"""Returns default session filename for given username."""
|
||||||
dirname = tempfile.gettempdir() + "/" + ".instaloader-" + getpass.getuser()
|
dirname = tempfile.gettempdir() + "/" + ".instaloader-" + getpass.getuser()
|
||||||
@ -468,6 +472,8 @@ class Instaloader:
|
|||||||
resp = sess.get('https://www.instagram.com/' + url, params=params)
|
resp = sess.get('https://www.instagram.com/' + url, params=params)
|
||||||
if resp.status_code == 404:
|
if resp.status_code == 404:
|
||||||
raise QueryReturnedNotFoundException("404")
|
raise QueryReturnedNotFoundException("404")
|
||||||
|
if resp.status_code == 429:
|
||||||
|
raise TooManyRequests("429 - Too Many Requests")
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
|
raise ConnectionException("HTTP error code {}.".format(resp.status_code))
|
||||||
resp_json = resp.json()
|
resp_json = resp.json()
|
||||||
@ -482,8 +488,13 @@ class Instaloader:
|
|||||||
error_string = "JSON Query to {}: {}".format(url, err)
|
error_string = "JSON Query to {}: {}".format(url, err)
|
||||||
if tries <= 1:
|
if tries <= 1:
|
||||||
raise ConnectionException(error_string)
|
raise ConnectionException(error_string)
|
||||||
else:
|
self.error(error_string + " [retrying]")
|
||||||
self.error(error_string + " [retrying]")
|
if isinstance(err, TooManyRequests):
|
||||||
|
text_for_429 = ("HTTP error code 429 was returned because too many queries occured in the last time. "
|
||||||
|
"Please do not use Instagram in your browser or run multiple instances of Instaloader "
|
||||||
|
"in parallel. The request is retried in about four minutes.")
|
||||||
|
print(textwrap.fill(text_for_429), file=sys.stderr)
|
||||||
|
time.sleep(660/3)
|
||||||
self._sleep()
|
self._sleep()
|
||||||
self.get_json(url, params, sess, tries - 1)
|
self.get_json(url, params, sess, tries - 1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user