1
0
Fork 0
mirror of synced 2024-06-29 11:30:30 +12:00

Beta optout, fix for random imgur json error

I have no idea what I'm doing, this is my first Python dabbling
This commit is contained in:
Erik Johnson 2021-01-07 10:45:16 +01:00 committed by GitHub
parent 37fdd87ab9
commit c6a346eb90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -114,7 +114,7 @@ class Imgur:
@staticmethod
def getData(link):
cookies = {"over18": "1"}
cookies = {"over18": "1", "postpagebeta": "0"}
res = requests.get(link, cookies=cookies)
if res.status_code != 200: raise ImageNotFound(f"Server responded with {res.status_code} to {link}")
pageSource = requests.get(link, cookies=cookies).text
@ -125,11 +125,17 @@ class Imgur:
STARTING_STRING_LENGHT = len(STARTING_STRING)
try:
startIndex = pageSource.index(STARTING_STRING) + STARTING_STRING_LENGHT
endIndex = pageSource.index(ENDING_STRING)
endIndex = pageSource.index(ENDING_STRING, startIndex)
except ValueError:
raise NotADownloadableLinkError(f"Could not read the page source on {link}")
data = pageSource[startIndex:endIndex].strip()[:-1]
while pageSource[endIndex] != "}":
endIndex=endIndex-1
try:
data = pageSource[startIndex:endIndex+2].strip()[:-1]
except:
pageSource[endIndex+1]='}'
data = pageSource[startIndex:endIndex+3].strip()[:-1]
return json.loads(data)