forked from AnthonySigogne/web-search-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.py
More file actions
176 lines (154 loc) · 5.71 KB
/
crawler.py
File metadata and controls
176 lines (154 loc) · 5.71 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import os
import url
import scrapy
import base64
import io
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.http import Request
from language import languages
from collections import Counter
from PIL import Image
from rq.decorators import job
from rq import Queue
from elasticsearch_dsl.connections import connections
hosts = [os.getenv("HOST")]
http_auth = (os.getenv("USERNAME"), os.getenv("PASSWORD"))
port = os.getenv("PORT")
client = connections.create_connection(hosts=hosts, http_auth=http_auth, port=port)
class SingleSpider(scrapy.spiders.CrawlSpider):
"""
Single page spider.
"""
name = "spider"
handle_httpstatus_list = [301, 302, 303] # redirection allowed
es_client=None # elastic client
redis_conn=None # redis client
def parse(self, response):
yield pipeline(response, self)
class Crawler(scrapy.spiders.CrawlSpider):
"""
Explore a website and index all urls.
"""
name = 'crawler'
handle_httpstatus_list = [301, 302, 303] # redirection allowed
rules = (
# Extract all inner domain links with state "follow"
Rule(LinkExtractor(), callback='parse_items', follow=True, process_links='links_processor'),
)
es_client=None # elastic client
redis_conn=None # redis client
def links_processor(self,links):
"""
A hook into the links processing from an existing page, done in order to not follow "nofollow" links
"""
ret_links = list()
if links:
for link in links:
if not link.nofollow:
ret_links.append(link)
return ret_links
def parse_items(self, response):
"""
Parse and analyze one url of website.
"""
yield pipeline(response, self)
def pipeline(response, spider) :
"""
Index a page.
"""
# skip rss or atom urls
if not response.css("html").extract_first() :
return
# get domain
domain = url.domain(response.url)
# extract title
title = response.css('title::text').extract_first()
title = title.strip() if title else ""
# extract description
description = response.css("meta[name=description]::attr(content)").extract_first()
description = description.strip() if description else ""
# get main language of page, and main content of page
lang = url.detect_language(response.body)
if lang not in languages :
raise InvalidUsage('Language not supported')
body, boilerplate = url.extract_content(response.body, languages.get(lang))
# weight of page
weight = 3
if not title and not description :
weight = 0
elif not title :
weight = 1
elif not description :
weight = 2
if body.count(" ") < boilerplate.count(" ") or not url.create_description(body) :
# probably bad content quality
weight -= 1
# -- TEST -- #
"""keywords = Counter()
text_for_keywords = "%s\n%s\n%s"%(title, description, bestbody)
r = requests.post('http://localhost:5001/keywords_from_text', data = {'text':text_for_keywords})
data = r.json()
#print(hit.url, data)
for k in data["keywords"] :
keywords[k] += 1
keywords = " ".join(["%s "%(kw)*score for kw, score in keywords.most_common(100)])"""
# index url and data
res = spider.es_client.index(index="web-%s"%lang, doc_type='page', id=response.url, body={
"url":response.url,
"domain":domain,
"title":title,
"description":description,
"body":body,
"weight":weight
})
# try to create thumbnail from page
img_link = response.css("meta[property='og:image']::attr(content)").extract_first()
if not img_link :
img_link = response.css("meta[name='twitter:image']::attr(content)").extract_first()
if img_link :
q = Queue(connection=spider.redis_conn)
q.enqueue(create_thumbnail, response.url, lang, img_link)
# check for redirect url
if response.status in spider.handle_httpstatus_list and 'Location' in response.headers:
newurl = response.headers['Location']
meta = {'dont_redirect': True, "handle_httpstatus_list" : spider.handle_httpstatus_list}
meta.update(response.request.meta)
return Request(url = newurl.decode("utf8"), meta = meta, callback=spider.parse)
def create_thumbnail(url_id, lang, link) :
"""
Create a thumbnail from image link.
"""
print("create thumbnail of : %s"%link)
size = 143, 143
r = requests.get(link, stream=True) # get image data
if r.status_code == 200:
img = Image.open(io.BytesIO(r.content))
format_ = img.format # format of image (mime type: jpg, png,...)
longer_side = max(img.size)
horizontal_padding = (longer_side - img.size[0]) / 2
vertical_padding = (longer_side - img.size[1]) / 2
# crop image
img = img.crop(
(
-horizontal_padding,
-vertical_padding,
img.size[0] + horizontal_padding,
img.size[1] + vertical_padding
)
)
img.thumbnail(size) # create thumbnail
# encode in base64
buffer_ = io.BytesIO()
img.save(buffer_, format=format_)
img_str = b"data:image/%s;base64,%s"%(format_.lower().encode("utf8"),base64.b64encode(buffer_.getvalue()))
img_str = img_str.decode("utf8")
# finally, save into elasticsearch
url = client.get(index="web-%s"%lang, doc_type='page', id=url_id)
url["_source"]["thumbnail"] = img_str
res = client.index(index="web-%s"%lang, doc_type='page', id=url_id, body=url["_source"])
return 1
return 0