Advertisement
Guest User

ifmtail

a guest
Nov 20th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. #ifmtail
  2.  
  3. import os
  4. import sys
  5. import requests
  6. from time import sleep
  7. from bs4 import BeautifulSoup
  8.  
  9. session = requests.Session()
  10. session.headers.update({
  11.         'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'
  12.     })
  13.  
  14. def printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
  15.     formatStr = "{0:." + str(decimals) + "f}"
  16.     percent = formatStr.format(100 * (iteration / float(total)))
  17.     filledLength = int(round(barLength * iteration / float(total)))
  18.     bar = '█' * filledLength + '-' * (barLength - filledLength)
  19.     sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix))
  20.     if iteration == total:
  21.         sys.stdout.write('\n')
  22.     sys.stdout.flush()
  23.  
  24. def createDir(nameFolder):
  25.     os.mkdir(nameFolder)
  26.     os.chdir(nameFolder)
  27.  
  28. def saveWeb(url):
  29.     response = session.get(url)
  30.     nameDir = url.split('/')[-1].split('.')[0]
  31.     createDir(nameDir)
  32.     return response.text
  33.  
  34. def parseWeb(text):
  35.     listFile = list()
  36.     soup = BeautifulSoup(text, 'html.parser')
  37.     for link in soup.find_all('div', {'class': 'image-link'}, 'a'):
  38.         listFile.append(link.a.get('href'))
  39.     return listFile
  40.  
  41. def downloadFile(url):
  42.     head = 'https://2ch.hk'
  43.     items = list(range(0, len(url)))
  44.     i = 0
  45.     l = len(items)
  46.  
  47.     for part in items:
  48.         r = requests.get(head+url[part])
  49.         if r.status_code == 200:
  50.             file = open(url[part].split('/')[-1], 'wb')
  51.             file.write(r.content)
  52.             sleep(0.1)
  53.             i += 1
  54.             printProgress(i, l, prefix = 'Progress:', suffix = '', barLength = 50)
  55.  
  56. if len(sys.argv) == 2:
  57.     try:
  58.         data = saveWeb(sys.argv[1])
  59.         downloadFile(parseWeb(data))
  60.     except:
  61.         print('error... url not found')
  62.    
  63. else:
  64.     print('pls example: dwach.py https://2ch.hk/b/res/%RANDOM_NUMBERS%.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement