From e1d05908b5ba1ba877b6ae4b364472c77c7df768 Mon Sep 17 00:00:00 2001 From: Saurabh Bhan Date: Sun, 26 Jun 2016 14:00:47 +0530 Subject: [PATCH] Initial Commit --- wallpy.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 wallpy.py diff --git a/wallpy.py b/wallpy.py new file mode 100644 index 0000000..60a62e0 --- /dev/null +++ b/wallpy.py @@ -0,0 +1,38 @@ +######################################################## +# Program to Download Wallpapers from # +# alpha.wallhaven.cc # +# # +# Author - Saurabh Bhan # +# # +# dated- 26 June 2016 # +######################################################## + +import os +import bs4 +import re +import requests + +os.makedirs('Wallhaven', exist_ok=True) +url = 'https://alpha.wallhaven.cc/latest' +urlreq = requests.get(url) +soup = bs4.BeautifulSoup(urlreq.text, 'lxml') +soupid = soup.findAll('a', {'class': 'preview'}) +res = re.compile(r'\d+') +imgid = res.findall(str(soupid)) +imgext = ['jpg', 'png', 'bmp'] +for i in range(len(imgid)): + url = 'http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-%s.' % imgid[ + i] + iurl = url + imgext[0] + imgreq = requests.get(iurl) + er = imgreq.status_code + if er == 404: + iurl == url + imgext[1] + er2 = imgreq.status_code + if er2 == 404: + iurl == url + imgext[2] + print('Downloading: ' + iurl) + imageFile = open(os.path.join('Wallhaven', os.path.basename(iurl)), 'ab') + for chunk in imgreq.iter_content(100000): + imageFile.write(chunk) + imageFile.close()