-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
199 lines (182 loc) · 6.73 KB
/
model.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
from bs4 import BeautifulSoup as soup
import pymongo
import requests
import urllib.request
#create connection
dbmongo = pymongo.MongoClient(username="admin",password="root",authSource="admin")
#------------------
LinksLinksCollection = dbmongo.links.links
LinksUrlCollection = dbmongo.links.urls
LinksErrorUrlCollection = dbmongo.links.errorUrl
#------------------
TestdbUrlsCollection = dbmongo.testdb.urls
TestdbStudentsCollection = dbmongo.testdb.students
#----------------------------------------------------------------------------------
def getList(query="default",linkType="all",skip=1):
search_query = {'$regex': "", '$options': 'i'}
search_query["$regex"] = query
links = []
num = 0
if linkType == "all":
for item in LinksLinksCollection.find({'link': search_query}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
#db.links.find({$and:[{"link":{$regex:"cold",$options:"i"}},{"link":{$regex:"skin",$options:"i"}}]})
if linkType == "video":
for item in LinksLinksCollection.find({"$and":[{"type":"video"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "audio":
for item in LinksLinksCollection.find({"$and":[{"type":"audio"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "image":
for item in LinksLinksCollection.find({"$and":[{"type":"image"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "compressed":
for item in LinksLinksCollection.find({"$and":[{"type":"compressed"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "executable":
for item in LinksLinksCollection.find({"$and":[{"type":"executable"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "disk":
for item in LinksLinksCollection.find({"$and":[{"type":"disk"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
if linkType == "text":
for item in LinksLinksCollection.find({"$and":[{"type":"text"},{'link': search_query}]}).limit(10):
#print(item["name"])
if links.__len__()-1 != num:
links.append({})
links[num]['id'] = str(item["_id"])
links[num]['name'] = item["name"]
links[num]['link'] = item["link"]
links[num]['type'] = item["type"]
#links.append({}) extra append should be created
num += 1
return links
def Crawl(url):
try:
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
headers = {'User-Agent': user_agent}
request = urllib.request.Request(url,headers={'User-Agent': user_agent})
response = urllib.request.urlopen(request)
page_html = response.read()
response.close()
print("Parsing URL..."+ url)
page_soup = soup(page_html, "html.parser")
links = page_soup.findAll("a",href=True)
except:
LinksErrorUrlCollection.insert({"ErrorUrl":url})
print("ERROR:"+url)
try:
dirLink = []
num = 0
for link in links:
completeLink = url+link["href"]
print(link["href"],url)
if link["href"].endswith("/") and not link["href"].endswith("../") and not link["href"].startswith("/"):
#call the crawl function again
Crawl(str(url+link["href"]))
else:
if dirLink.__len__()-1 != num:
dirLink.append({})
fullLink=str(url+link["href"])
if fullLink.lower().endswith(("mp4","mkv","3gp","avi","mov","mpg","mpeg","wmv","m4v")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="video"
num += 1
elif fullLink.lower().endswith(("mp3","aif","mid","midi","mpa","ogg","wav","wma","wpl")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="audio"
num += 1
elif fullLink.lower().endswith(("rar","zip","deb","pkg","tar.gz",".z","rpm",".7z","arj")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="compressed"
num += 1
elif fullLink.lower().endswith(("bin","dmg","iso","toast","vcd")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="disk"
num += 1
elif fullLink.lower().endswith(("exe","apk","bat","com","jar",".py",".wsf")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="executable"
num += 1
elif fullLink.lower().endswith(("ai","bmp","gif","ico","jpeg","png","jpg","tif","svg")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="image"
num += 1
elif fullLink.lower().endswith(("pdf","txt","doc","rtf","wpd","docx","odt","wps","wks")):
dirLink[num]["name"]=link.text
dirLink[num]["link"]=fullLink
dirLink[num]["type"]="text"
num += 1
else:
pass
if dirLink != [{}]:
LinksLinksCollection.insert_many(dirLink)
LinksUrlCollection.insert({"url":url})
#return dirLink
except:
LinksErrorUrlCollection.insert({"ErrorUrl":url})
print("ERROR in second try:"+url)
if __name__ == '__main__':
print(Crawl(input("Enter:")))