|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +from os import chdir |
| 4 | +import requests |
| 5 | +from bs4 import BeautifulSoup |
| 6 | +from urllib.request import urlopen, Request |
| 7 | +from urllib.parse import urlencode |
| 8 | +from os import walk |
| 9 | +import json |
| 10 | +from os.path import curdir |
| 11 | +from urllib.request import urlretrieve |
| 12 | +from os.path import pardir |
| 13 | +from create_dir import create_directory |
| 14 | + |
| 15 | +GOOGLE_IMAGE = 'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&' |
| 16 | +WALLPAPERS_KRAFT = 'https://wallpaperscraft.com/search/keywords?' |
| 17 | +usr_agent = { |
| 18 | + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', |
| 19 | + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', |
| 20 | + 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', |
| 21 | + 'Accept-Encoding': 'none', |
| 22 | + 'Accept-Language': 'en-US,en;q=0.8', |
| 23 | + 'Connection': 'keep-alive' |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +def image_grabber(ch): |
| 28 | + |
| 29 | + # Download images from google images |
| 30 | + if ch == 1: |
| 31 | + print('Enter data to download Images: ') |
| 32 | + data = input() |
| 33 | + search_query = {'q': data} |
| 34 | + search = urlencode(search_query) |
| 35 | + print(search) |
| 36 | + g = GOOGLE_IMAGE + search |
| 37 | + request = Request(g, headers=usr_agent) |
| 38 | + r = urlopen(request).read() |
| 39 | + sew = BeautifulSoup(r, 'html.parser') |
| 40 | + images = [] |
| 41 | + # print(sew.prettify()) |
| 42 | + results = sew.findAll("div",{"class":"rg_meta"}) |
| 43 | + for re in results: |
| 44 | + link, Type = json.loads(re.text)["ou"] , json.loads(re.text)["ity"] |
| 45 | + images.append(link) |
| 46 | + counter = 0 |
| 47 | + for re in images: |
| 48 | + rs = requests.get(re) |
| 49 | + with open('img' + str(counter) + '.jpg', 'wb') as file: |
| 50 | + file.write(rs.content) |
| 51 | + # urlretrieve(re, 'img' + str(count) + '.jpg') |
| 52 | + counter += 1 |
| 53 | + return True |
| 54 | + |
| 55 | + elif ch == 2: |
| 56 | + cont = set() # Stores the links of images |
| 57 | + temp = set() # Refines the links to download images |
| 58 | + |
| 59 | + print('Enter data to download wallpapers: ') |
| 60 | + data = input() |
| 61 | + search_query = {'q': data} |
| 62 | + search = urlencode(search_query) |
| 63 | + print(search) |
| 64 | + g = WALLPAPERS_KRAFT + search |
| 65 | + request = Request(g, headers=usr_agent) |
| 66 | + r = urlopen(request).read() |
| 67 | + sew = BeautifulSoup(r, 'html.parser') |
| 68 | + count = 0 |
| 69 | + for links in sew.find_all('a'): |
| 70 | + if 'wallpaperscraft.com/download' in links.get('href'): |
| 71 | + cont.add(links.get('href')) |
| 72 | + for re in cont: |
| 73 | + # print all valid links |
| 74 | + # print('https://wallpaperscraft.com/image/' + re[31:-10] + '_' + re[-9:] + '.jpg') |
| 75 | + temp.add('https://wallpaperscraft.com/image/' + re[31:-10] + '_' + re[-9:] + '.jpg') |
| 76 | + |
| 77 | + # Goes to Each link and downloads high resolution images |
| 78 | + |
| 79 | + for re in temp: |
| 80 | + rs = requests.get(re) |
| 81 | + with open('img' + str(count) + '.jpg', 'wb') as file: |
| 82 | + file.write(rs.content) |
| 83 | + # urlretrieve(re, 'img' + str(count) + '.jpg') |
| 84 | + count += 1 |
| 85 | + |
| 86 | + return True |
| 87 | + |
| 88 | + elif ch == 3: |
| 89 | + for folders, subfolder, files in walk(curdir): |
| 90 | + for folder in subfolder: |
| 91 | + print(folder) |
| 92 | + return True |
| 93 | + |
| 94 | + elif ch == 4: |
| 95 | + print('Enter the directory to be set: ') |
| 96 | + data = input() |
| 97 | + chdir(data + ':\\') |
| 98 | + print('Enter name for the folder: ') |
| 99 | + data = input() |
| 100 | + create_directory(data) |
| 101 | + return True |
| 102 | + |
| 103 | + elif ch == 5: |
| 104 | + print( |
| 105 | + ''' |
| 106 | +-------------------------***Thank You For Using***------------------------- |
| 107 | + ''' |
| 108 | + ) |
| 109 | + return False |
| 110 | + |
| 111 | + |
| 112 | +run = True |
| 113 | + |
| 114 | +print( |
| 115 | + ''' |
| 116 | +***********[First Creating Folder To Save Your Images}*********** |
| 117 | + ''' |
| 118 | + ) |
| 119 | + |
| 120 | +create_directory('Images') |
| 121 | +DEFAULT_DIRECTORY = pardir + '\\Images' |
| 122 | +chdir(DEFAULT_DIRECTORY) |
| 123 | + |
| 124 | +while run: |
| 125 | + print(''' |
| 126 | +-------------------------WELCOME------------------------- |
| 127 | + 1. Search for image |
| 128 | + 2. Download Wallpapers 1080p |
| 129 | + 3. View Images in your directory |
| 130 | + 4. Set directory |
| 131 | + 5. Exit |
| 132 | +-------------------------*******------------------------- |
| 133 | + ''') |
| 134 | + choice = input() |
| 135 | + run = image_grabber(int(choice)) |
0 commit comments