-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy path__init__.py
More file actions
142 lines (110 loc) · 3.95 KB
/
__init__.py
File metadata and controls
142 lines (110 loc) · 3.95 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
# -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.
Python port was written by Xavier Grangier for Recrutae
Gravity.com licenses this file
to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import time
import hashlib
import re
import os
import goose
import codecs
import urlparse
class BuildURL(object):
def __init__(self, url, finalurl=None):
self.url = url
self.finalurl = finalurl
def getHostname(self, o):
if o.hostname:
return o.hotname
elif self.finalurl:
oo = urlparse(self.finalurl)
if oo.hostname:
return oo.hostname
return None
def getScheme(self, o):
if o.scheme:
return o.scheme
elif self.finalurl:
oo = urlparse(self.finalurl)
if oo.scheme:
return oo.scheme
return 'http'
def getUrl(self):
"""\
"""
url_obj = urlparse(self.url)
scheme = self.getScheme(url_obj)
hostname = self.getHostname(url_obj)
class FileHelper(object):
@classmethod
def loadResourceFile(self, filename):
if not os.path.isabs('filename'):
dirpath = os.path.dirname(goose.__file__)
path = os.path.join(dirpath, 'resources', filename)
else:
path = filename
try:
f = codecs.open(path, 'r', 'utf-8')
content = f.read()
f.close()
return content
except IOError:
raise IOError("Couldn't open file %s" % path)
class ParsingCandidate(object):
def __init__(self, urlString, link_hash):
self.urlString = self.url = urlString
self.link_hash = link_hash
class RawHelper(object):
@classmethod
def get_parsing_candidate(self, url, raw_html):
if isinstance(raw_html, unicode):
raw_html = raw_html.encode('utf-8')
link_hash = '%s.%s' % (hashlib.md5(raw_html).hexdigest(), time.time())
return ParsingCandidate(url, link_hash)
class URLHelper(object):
@classmethod
def get_parsing_candidate(self, url_to_crawl):
# replace shebang is urls
final_url = url_to_crawl.replace('#!', '?_escaped_fragment_=') \
if '#!' in url_to_crawl else url_to_crawl
link_hash = '%s.%s' % (hashlib.md5(final_url.encode('utf-8')).hexdigest(), time.time())
return ParsingCandidate(final_url, link_hash)
class StringReplacement(object):
def __init__(self, pattern, replaceWith):
self.pattern = pattern
self.replaceWith = replaceWith
def replaceAll(self, string):
if not string:
return u''
return string.replace(self.pattern, self.replaceWith)
class ReplaceSequence(object):
def __init__(self):
self.replacements = []
#@classmethod
def create(self, firstPattern, replaceWith=None):
result = StringReplacement(firstPattern, replaceWith or u'')
self.replacements.append(result)
return self
def append(self, pattern, replaceWith=None):
return self.create(pattern, replaceWith)
def replaceAll(self, string):
if not string:
return u''
mutatedString = string
for rp in self.replacements:
mutatedString = rp.replaceAll(mutatedString)
return mutatedString