-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawlerManager.py
More file actions
142 lines (115 loc) · 5.08 KB
/
CrawlerManager.py
File metadata and controls
142 lines (115 loc) · 5.08 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 -*-
"""
Created on Tue Mar 3 18:20:57 2020
@author: jindou
"""
import subprocess
from StellarLog.StellarLog import CLog
from diskcache import Cache
class CUrlList:
def __init__(self,index,logInfo:dict):
self.index = index
self.logInfo:dict = logInfo
self.preInfoList:list = list()
self._list:list = list()
def append(self,url:str,preInfo:list):
self._list.append(url)
self.preInfoList.append(preInfo)
def replace(self,List:list,preInfoList:list):
if(type(preInfoList[0])!=list):
raise ValueError('preInfoList must be list of list')
self._list = List
self.preInfoList = preInfoList
def clear(self):
self._list.clear()
self.preInfoList.clear()
def exportJson(self):
import json
jsonDict = {'index':self.index,
'logInfo':self.logInfo,
'preInfo':self.preInfoList,
'urlList':self._list}
jsonStr = json.dumps(jsonDict)
return jsonStr
def __getitem__(self,idx):
tempDict = self.logInfo.copy()
tempDict['type'] = 'subList'
oUrlList = CUrlList(self.index,tempDict)
oUrlList._list = self._list[idx]
oUrlList.preInfoList = self.preInfoList[idx]
return oUrlList
def __len__(self):
return len(self._list)
class CCrawlerManager:
def __init__(self,name,workDirectory:str, oLog:CLog,cachePath:str, cacheAgentPath:str):
self.workDirectory = workDirectory
self.jobsList = None
self.oLog = oLog
self.outputFolder = workDirectory
self.name = name + '_crawler'
self.jobCnt = 0
self._cachePathCrawler = cachePath
self._cachePathAgent = cacheAgentPath
self.cache = Cache(cachePath)
def _newProcess(self,crawlerName,oUrlCacheKey:str):
outFilePath = 'file:///' + self.outputFolder + self.name + '.json'
# print(outFilePath,urlsFilePath)
# process = subprocess.Popen(['scrapy','crawl',crawlerName,'-o',outFilePath,'-a',
# 'cacheCrawlerPath='+ self._cachePathCrawler,'-a',
# 'cacheKey='+oUrlCacheKey,'-a',
# 'cacheAgentPath=' + self._cachePathAgent],
# shell=True,
# cwd=self.workDirectory)
# print('scrapy','crawl',crawlerName,'-o',outFilePath,'-a',
# 'cacheCrawlerPath='+ self._cachePathCrawler,'-a',
# 'cacheKey='+oUrlCacheKey,'-a',
# 'cacheAgentPath=' + self._cachePathAgent)
process = subprocess.Popen(['scrapy','crawl',crawlerName,'-a',
'cacheCrawlerPath='+ self._cachePathCrawler,'-a',
'cacheKey='+oUrlCacheKey,'-a',
'cacheAgentPath=' + self._cachePathAgent],
shell=True,
cwd=self.workDirectory)
# print('scrapy','crawl',crawlerName,'-a',
# 'cacheCrawlerPath='+ self._cachePathCrawler,'-a',
# 'cacheKey='+oUrlCacheKey,'-a',
# 'cacheAgentPath=' + self._cachePathAgent)
return process
def engineStart(self,jobsList:list):
for oUrlList in jobsList:
oUrlList.index = self.jobCnt
tempKey = self._prepareJob(oUrlList.exportJson())
self.oLog.safeRecordTime(str(oUrlList.index)+"start")
temp = self._newProcess('general',tempKey)
# temp.wait()
self.oLog.safeRecordTime(str(oUrlList.index)+"end")
return temp
def _prepareJob(self,content:str):
# key = str(self.jobCnt)
# if(self.cache.get(key)==False):
# raise ValueError("this key exists in the cache")
# return None
# else:
# self.cache[key] = content
# self.jobCnt+=1
# return key
self.jobCnt+=1
# print(self.cache.directory)
key = self.cache.push(content)
return str(key)
def closeCache(self):
self.cache.close()
class CContentExtract():
def __init__(self,mode='boilerpipe'):
if(mode=='boilerpipe'):
self.Module = self._importBoilerpipe3Extractor()
else:
raise ValueError("doesn't have this kind of mode")
def boilerpipe(self,htmlText):
extractor = self.Module(extractor='ArticleExtractor', html=htmlText)
title = extractor.getTitle()
content = extractor.getText()
return title,content
def _importBoilerpipe3Extractor(self):
from boilerpipe.extract import Extractor
return Extractor