-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinfoMediator.py
238 lines (186 loc) · 8.31 KB
/
infoMediator.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
import zmq
import json
from article import article
from authorCard import authorCard
from publisherCard import publisherCard
from citationsNetwork import citationsNetwork
class infoMediator:
'''
Mediator Design Pattern:
class infoMediator is the core mediator to
handle client(URL) and producer(author/publisher/citations crawling class)
'''
def __init__(self):
# TCP connection on local host 5555
context = zmq.Context()
self.socket = context.socket(zmq.REP)
self.socket.bind("tcp://*:5555")
# status indicator
self.URL_status = False
self.URL = None
# Information variables
self.article_content = None
self.author_card = None
self.publisher_card = None
self.citation_network = None
def start(self):
'''
:return:
'''
while(True):
command = self.socket.recv()
if str(command) == "STOP":
return
self.control_center(command.decode("utf-8"))
def control_center(self, command):
'''
:param command: a string, command messsage from the server
'''
'''
request for processing user's search query: Article URL
@ wait for command: "URL (Article URL)" eg "URL www.google.com"
@ send "Okey" if URL is accessible and processed by crawler
@ send "Failed" if URL is not accessible or crawling system faile
'''
if "URL" in command:
self.URL = command.split(" ")[1]
self.process_URL()
if self.URL_status:
self.socket.send_string("Okey")
else:
self.socket.send_string("Failed")
return
'''
request for getting article content
@ wait for command: "Article"
@ send JSON String"{article_title, article_content, author_name, publisher_name, article_reliability}"
@ send "None" if there is no URL processed first.
'''
if command == "Article" and self.URL_status:
article_cotent = self.get_article_content()
self.socket.send_string(article_cotent)
return
'''
request for getting author's cards
@ wait for command: "Author card"
@ send JSON String "{author_name, author_introduction, author_reliability_score, author_bias_score}"
@ send "None" if there is no URL processed first or failed to crawl the information
'''
if command == "Author card" and self.URL_status:
author_card = self.get_author_card()
self.socket.send_string(author_card)
return
'''
request for getting publisher's cards
@ wait for command: "Publisher card"
@ send JSON String "{publisher_name, publisher_introduction, publisher_reliability_score}"
'''
if command == "Publisher card" and self.URL_status:
publisher_card = self.get_publisher_card()
self.socket.send_string(publisher_card)
return
if command == "Citation network" and self.URL_status:
citation_network = self.get_citation_network()
self.socket.send_string(citation_network)
return
'''
@ send "Error" if command is not recognized
'''
self.socket.send_string("Error")
return
def process_URL(self):
'''
Set up article class to process and store information in self.article_content
set up author card class to process and store in self.author_card
:return: set self.URL_status to True if the article is crawled successfully
Otherwise, set self.URL_status to False
'''
# set up article class
new_article = article(self.URL)
article_result = new_article.get()
if article_result is not None:
self.URL_status = True
# store article information to self.article_content
self.article_content = dict()
self.article_content["article_title"] = article_result["article_title"]
self.article_content["article_content"] = article_result["article_content"]
self.article_content["author_name"] = article_result["author_name"]
self.article_content["publisher_name"] = article_result["publisher_name"]
self.article_content["article_reliability"] = article_result["article_reliability"]
self.article_content["article_bias"] = article_result["article_bias"]
# set up author card class
new_author_card = authorCard(article_id=article_result["article_id"],
author_page_link=article_result["author_page_link"],
profile=article_result["profile"],
author_name=article_result["author_name"])
author_result = new_author_card.get()
# store author information to self.author_card
if author_result is not None:
self.author_card = dict()
self.author_card["author_name"] = author_result["author_name"]
self.author_card["author_introduction"] = author_result["author_intro"]
self.author_card["author_reliability_score"] = author_result["author_reliability"]
self.author_card["author_bias_score"] = author_result["author_bias"]
self.author_card["author_link"] = author_result["author_link"]
else:
self.author_card = None
#set up publisher card class
new_publisher_card = publisherCard(article_result["profile"])
publisher_result = new_publisher_card.get()
# store publisher information to self.publisher_card
if publisher_result is not None:
self.publisher_card = dict()
self.publisher_card["publisher_name"] = publisher_result["publisher_name"]
self.publisher_card["publisher_introduction"] = publisher_result["publisher_intro"]
self.publisher_card["publisher_reliability_score"] = publisher_result["publisher_reliability_score"]
self.publisher_card["publisher_link"] = publisher_result["publisher_link"]
# set up citation network class
new_citation_network = citationsNetwork(article_result["article_id"])
citation_network_result = new_citation_network.get()
if citation_network_result is not None:
# store partial information in article_content
self.article_content["article_paragraphs"] = citation_network_result["article_paragraphs"]
self.article_content["citation_links"] = citation_network_result["citation_links"]
# store specific citation info to self.citation_network
self.citation_network = citation_network_result["citation_info"]
else:
self.citation_network = None
else:
self.article_content = dict()
self.URL_status = False
def get_article_content(self):
'''
return make article_content into a json string
:return: a json string
'''
if self.article_content is not None:
return json.dumps(self.article_content)
else:
return "None"
def get_author_card(self):
'''
:return: a json string of self.author_card if crawling and analysis are succesfull
Otherwise return None
'''
if self.author_card is not None:
return json.dumps(self.author_card)
else:
return "None"
def get_publisher_card(self):
'''
:return: a json string of self.publisher_card if crawling and analysis are succesfull
Otherwise return None
'''
if self.publisher_card is not None:
return json.dumps(self.publisher_card)
else:
return "None"
def get_citation_network(self):
'''
:return: a json string of self.citation_network if crawling and analysis are succesfull
Otherwise return None
'''
if self.citation_network is not None:
return json.dumps(self.citation_network)
else:
return "None"