Guest User

Untitled

a guest
Sep 11th, 2021
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from pathlib import Path
  3. from urllib.request import urlopen, Request
  4. from time import sleep
  5.  
  6. def downloadurl(url):
  7.     print("Downloading album", url)
  8.     dpath=Path(list(filter(None, url.split('/')))[-1])
  9.     dpath.mkdir(exist_ok=True)
  10.     running=True
  11.     pagenum="1"
  12.     headers={"User-agent": 'Mozilla'}
  13.     while running:
  14.         sleep(0.1)
  15.         s=urlopen(Request(url,headers=headers)).read().decode("utf-8")
  16.         idx=s.find("<div class=\"main-image\">")
  17.         pat="<a href="
  18.         idx=s.find(pat,idx)+len(pat)+1
  19.         close=s.find('"',idx)
  20.         imgurl = s[idx:close]
  21.         print(f"Downloading page {pagenum} from {imgurl}")
  22.         with urlopen(Request(imgurl, headers=headers)) as r, (dpath / (pagenum+"."+imgurl.rsplit(".",1)[-1])).open('wb') as f:
  23.             for chunk in iter(lambda: r.read(4096),b''):
  24.                 f.write(chunk)
  25.         pat="<a class=\"on-popunder next page-numbers\" href="
  26.         idx=s.find(pat)+len(pat)+1
  27.         close=s.find('"',idx)
  28.         url=s[idx:close]
  29.         pagenum=url.split('/')[-1]
  30.         running = s[close+1:close+6] == '>Next'
  31. import sys
  32. if __name__=='__main__':
  33.     if len(sys.argv)>1:
  34.         for url in sys.argv[1:]:
  35.             downloadurl(url)
  36.     else:
  37.         url=input("Wintoddlers, please enter a url:")
  38.         downloadurl(url)
Advertisement
Add Comment
Please, Sign In to add comment