forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_grapper.py
171 lines (140 loc) · 4.47 KB
/
image_grapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
# -*- coding: utf-8 -*-
#importing required libraries
import json
from os import chdir, system
from os import walk
from os.path import curdir
from os.path import pardir
from urllib.parse import urlencode
from urllib.request import urlopen, Request
import requests
import ssl
from bs4 import BeautifulSoup
from create_dir import create_directory
ssl._create_default_https_context = ssl._create_unverified_context
GOOGLE_IMAGE = \
'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&'
WALLPAPERS_KRAFT = 'https://wallpaperscraft.com/search/keywords?'
usr_agent = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
}
FX = {
1: 'search_for_image',
2: 'download_wallpapers_1080p',
3: 'view_images_directory',
4: 'set_directory',
5: 'quit',
}
# Download images from google images
def search_for_image():
print('Enter data to download Images: ')
data = input()
search_query = {'q': data}
search = urlencode(search_query)
print(search)
g = GOOGLE_IMAGE + search
request = Request(g, headers=usr_agent)
r = urlopen(request).read()
sew = BeautifulSoup(r, 'html.parser')
images = []
# print(sew.prettify())
results = sew.findAll('div', {'class': 'rg_meta'})
for re in results:
(link, Type) = (json.loads(re.text)['ou'],
json.loads(re.text)['ity'])
images.append(link)
counter = 0
for re in images:
rs = requests.get(re)
with open('img' + str(counter) + '.jpg', 'wb') as file:
file.write(rs.content)
# urlretrieve(re, 'img' + str(count) + '.jpg')
counter += 1
return True
def download_wallpapers_1080p():
cont = set() # Stores the links of images
temp = set() # Refines the links to download images
print('Enter data to download wallpapers: ')
data = input()
search_query = {'q': data}
search = urlencode(search_query)
print(search)
g = WALLPAPERS_KRAFT + search
request = Request(g, headers=usr_agent)
r = urlopen(request).read()
sew = BeautifulSoup(r, 'html.parser')
count = 0
for links in sew.find_all('a'):
if 'wallpaperscraft.com/download' in links.get('href'):
cont.add(links.get('href'))
for re in cont:
# print all valid links
# print('https://wallpaperscraft.com/image/' + re[31:-10] + '_' + re[-9:] + '.jpg')
temp.add('https://wallpaperscraft.com/image/' + re[31:-10] + '_'
+ re[-9:] + '.jpg')
# Goes to Each link and downloads high resolution images
for re in temp:
rs = requests.get(re)
with open('img' + str(count) + '.jpg', 'wb') as file:
file.write(rs.content)
# urlretrieve(re, 'img' + str(count) + '.jpg')
count += 1
return True
###################
def view_images_directory():
for (folders, subfolder, files) in walk(curdir):
for folder in subfolder:
print(folder)
return True
#############
def set_directory():
print('Enter the directory to be set: ')
data = input()
chdir(data + ':\\')
print('Enter name for the folder: ')
data = input()
create_directory(data)
return True
##############
def quit():
print('''
-------------------------***Thank You For Using***-------------------------
''')
return False
run = True
print('''
***********[First Creating Folder To Save Your Images}***********
''')
create_directory('Images')
DEFAULT_DIRECTORY = pardir + '\\Images'
chdir(DEFAULT_DIRECTORY)
count = 0
while run:
print('''
-------------------------WELCOME-------------------------
1. Search for image
2. Download Wallpapers 1080p
3. View Images in your directory
4. Set directory
5. Exit
-------------------------*******-------------------------
''')
choice = input()
try:
fx = FX[int(choice)]
run = globals()[fx]()
except KeyError:
system('clear')
if count <= 5:
count += 1
print("----------enter proper key-------------")
else:
system('clear')
print("You have attemted 5 times , try again later")
run = False