-
Notifications
You must be signed in to change notification settings - Fork 0
/
topUrl_10.py
375 lines (324 loc) · 11.3 KB
/
topUrl_10.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from selenium import webdriver
from bs4 import BeautifulSoup
from urlparse import urlparse
from os.path import basename
import requests
import urllib2
import urllib
import os
import re
import sys
import preProcess as pre_process
import time
reload(sys)
sys.setdefaultencoding('UTF-8')
#QUERY = raw_input("Enter word")
#filepath = ['/home/administrator/Desktop/rp/top_url/ ball/ ball_urls','/home/administrator/Desktop/rp/top_url/cc/ cc_urls']
#similar_image = ['/home/administrator/Desktop/rp/top_url/ ball/','/home/administrator/Desktop/rp/top_url/cc/']
filepath = [
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling four/umpire signalling four_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling noball/umpire signalling noball_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling out/umpire signalling out_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling six/umpire signalling six_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling wide/umpire signalling wide_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/wicketkeeper stumping behind the wickets/wicketkeeper stumping behind the wickets_urls',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/wicket keeper taking a catch/wicket keeper taking a catch_urls']
similar_image = [
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling four/'
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling noball/',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling out/',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling six/',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/umpire signalling wide/',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/wicketkeeper stumping behind the wickets/',
'/media/administrator/2452AA4852AA1F14/RP/Cricket/wicket keeper taking a catch/']
contain = 0
while contain < len(filepath):
p = similar_image[contain]
base1 = p + "Simailar_Images"
try:
os.stat(base1)
except:
os.makedirs(base1)
with open(filepath[contain]) as f:
lines = f.readlines()
n_files = 0
while n_files < len(lines):
val = lines[n_files]
v1 = val.split(':',1)
os.makedirs(base1 + "/"+v1[0])
QUERY = "Similar_images"
print "--------------Creating Directory--------------"
base = base1
base = base + "/"+v1[0]
#base = QUERY
try:
os.stat(base)
except:
os.makedirs(base)
location= base+"/"+QUERY+"_Text"
try:
os.stat(location)
except:
os.makedirs(location)
location2 = base+"/"+QUERY+"_ProcessedText"
try:
os.stat(location2)
except:
os.makedirs(location2)
location3 = base+"/"+QUERY+"_keywords"
try:
os.stat(location3)
except:
os.makedirs(location3)
img_location = base+"/"+QUERY+"_Images"
try:
os.stat(img_location)
except:
os.makedirs(img_location)
url = "http://galleryhip.com/red-apple-png.html"
cnt = 46
img_name = "Large_Red_Apples_PNG_Clipart.png"
def getImage(img, count, name):
#STORE IMAGES
#for img in img_url:
#raw_img = urllib.urlopen(img).read()
#add the directory for your image here
#DIR="/home/shuchita/Documents/3-2/SOP/code/Data/"
#cntr = len([i for i in os.listdir(DIR) if image_type in i]) + 1
#print cntr
#f = open(DIR + image_type + "_"+ str(cntr)+".jpg", 'wb')
#f.write(raw_img)
#f.close()
if ".png" in name:
ext = ".PNG"
else:
ext = ".JPG"
try:
urllib.urlretrieve(img,img_location+"/"+str(count)+ext)
except:
print "image fail to load"
img_fail.append(count)
def getText(url, cnt, img_name, QUERY):
response = requests.get(url,timeout=500)
soup = BeautifulSoup(response.text)
print "Getting text for image " , img_name
f = open(location+"/"+str(cnt)+".txt", 'w')
f1 = open(location2+"/"+str(cnt)+".txt", 'w')
f2 = open(location3+"/"+str(cnt)+".txt", 'w')
page_text = ""
#find the image on page
matches = [soup.find(href = re.compile(img_name)) , soup.find(src = re.compile(img_name)), soup.find(content =re.compile(img_name) )]
try:
tag = next(t for t in matches if t is not None)
except:
tag = None
print "Could not locate image on page"
#Get alt text if available
try:
alt = tag('img').get('alt')
if not alt:
raise exc
except:
try:
alt = tag.get('alt')
if not alt:
raise exc
except:
alt = ""
print "alt not found"
if not tag:
print "Yet not found! Access denied!!"
page_fail.append(cnt)
#RANDOM
try:
random_p = soup.body('p')
num_p = len(random_p)
num_cur = 1
num_found = min(num_p, 4)
while(num_cur < num_found):
tex = random_p[random.randint(0,num_p)].text
if tex:
page_text+=tex
num_cur+=1
except:
pass
#if anchor tag or img tag =>
elif tag.name == 'a' or tag.name == 'img':
#Traverse until enough text is found...
print "Is anchor or image tag"
lis = tag.parent.parent('p')
tag = tag.parent.parent
try:
while len(page_text.split(' '))<20 :
#print "Inside while"
for para in lis:
page_text += para.text + " "
if(len(page_text.split(' '))>300):
break
tag = tag.parent
if tag is None:
break
lis = tag.parent('p')
except:
print "execption for there is no text"
pass
#Traverse only two levels...
# for para in tag.parent.parent('p'):
# print "inside prev impl, found paras"
# page_text += para.text + " "
# if len(page_text.split())<20:
# for para in tag.parent.parent.parent('p'):
# page_text += para.text + " "
elif tag.name == 'meta':
for tag_elem in tag.parent('meta'):
if tag_elem.get('content'):
page_text += tag_elem.get('content')+ " "
#write to file
try:
title=soup.title.contents[0].encode('ascii','ignore')
if title is None:
raise exc
except:
title = ""
page_url_info = ""
try:
page_url_info = ' '.join(str(url).split('/')[3:])
except:
page_url_info = ' '.join(str(url).split('/')[2:])
page_text = title + "\n"+ alt + "\n"+ img_name+ "\n" + page_url_info + "\n" + page_text
#print '\n', page_text
#raw_input("check content ^^")
f.write ('%s' %(title) + '%s' %(alt) + "\n\n")
f.write(page_text)
new_text = pre_process.process_text(page_text, QUERY, False)
# kw_text = pre_process.process_text(page_text, QUERY, True)
#print new_text
#raw_input("check processed ^^")
f1.write(new_text)
# f2.write(kw_text)
f.close()
f1.close()
f2.close()
#debug
#getText(url, cnt, img_name)
############################ MAke file for urls!!! #######################
count=0
url=[]
img_url = []
page_url = []
image_succ = []
tags = dict()
i=0
res=[]
base2 = similar_image[contain]
f3 = open(base2+"/"+"QueryDetails"+".txt", 'a')
f3.write( "\n" + v1[0] + " : " + "\t");
f3.close()
try:
if not os.path.isfile(base+"/"+QUERY+"_urls"):
#Make the file!!!
f = open(base+"/"+QUERY+"_urls", "w")
#Trying to disable plugins to enable faster page loading
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.styleshet', 2)
firefox_profile.set_preference('permissions.default.image', 2)
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false')
browser = webdriver.Firefox(firefox_profile=firefox_profile)
image_url = v1[1]
google_image_search_url= "https://www.google.co.il/searchbyimage?hl=en-IL&image_url="
image_url_encoded = urllib.quote_plus(image_url)
image_search_url= google_image_search_url + image_url_encoded
print "Links of visually similar images"
# open google base search
browser.get(image_search_url)
# click on the "Visually similar images"
browser.find_element_by_link_text('Visually similar images').click()
# get results list
results = browser.find_elements_by_css_selector(".rg_di.rg_el.ivg-i a")
#links = []
print "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"
print results
# iterate results
for result in results:
# get link from result object
link = result.get_attribute('href')
print link
print "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
# link will be in the format: http://www.google.co.il/imgres?imgurl=...&imgrefurl=...&h=...&w=...&tbnid=...&docid=...&hl=...&ei=...&tbm=...
# we only need imagefurl, lets fetch it
#parsed_link = urlparse.urlparse(link)
#page_link = urlparse.parse_qs(parsed_link.query)['imgrefurl']
#print link
#links.append(page_link)
if len(url)<10 :
url.append(link)
countx=0
for u in url:
x=u.find("&imgrefurl")
x=x
y = u.find("imgurl=")
y=y+7
url_jpg=u[y:x]
f.write(str(countx)+":"+str(url_jpg)+"\n")
countx=countx+1
f.close()
#raw_input("check file")
#Read urls from file
f = open(base+"/"+QUERY+"_urls")
a = f.readline().replace("\n", "")
while not a == "":
#url.append(a)
a = f.readline().replace("\n", "")
page_fail = []
img_fail = []
start_time = 0
counter2=0
for im in url:
#print "original::: ", str(count), "\n", im
refer_url = urlparse(str(im))
#print "\n after urlparse:: \n", refer_url
img_url.append(refer_url.query.split("&")[0].replace("imgurl=",""))
page_url.append(refer_url.query.split("&")[1].replace("imgrefurl=",""))
print count
print 'Image: ' + img_url[count] +"\n"
print 'Page: ' + page_url[count] +"\n\n"
img_name = basename(urlparse(str(img_url[count])).path)
img_name = img_name.split('%')[0]
#print img_name
#raw_input("check name")
####################### GET PAGE TEXT ########################
f3 = open(base2+"/"+"QueryDetails"+".txt", 'a')
f3.write(" " + str(count) +",")
f3.close()
try:
start_time = time.time()
getText(page_url[count], count, img_name, QUERY)
image_succ.append(count + "\n")
except Exception as e:
print "exception at getText"
print e
if count not in page_fail:
page_fail.append(count)
counter2=counter2+1
#getText(page_url[count], count, img_name, QUERY)
try:
#getImage(img_url[count], count, img_name)
a=0
except:
if count not in img_fail:
img_fail.append(count)
count =count+1
if count%10 == 0:
print "\n\n", "Could not get text from => ", page_fail
print "\nCould not get following images => ", img_fail
print "\n\n", "Could not get text from => ", page_fail
print "\nCould not get following images => ", img_fail
n_files = n_files+1
browser.close()
except Exception as e:
print e
print "\n\n", "Could not get text from => "
print "\nCould not get following images => "
n_files = n_files+1
browser.close()
contain = contain + 1