-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproducer_factory.py
77 lines (54 loc) · 2.55 KB
/
producer_factory.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
import producer
import requestor
import threading
import twitter
import twitter_api
import yahoo_api
class ProducerFactory():
TWITTER_TOPIC: str = 'twitter'
YAHOO_TOPIC: str = 'stock'
TIMEOUT: int = 10 # in seconds
TWITTER_AUTH: twitter_api.TwitterAuthentication = twitter_api.TwitterAuthentication(
'Here insert a consumer_key',
'Here insert a consumer_secret',
'Here insert a access_token_key',
'Here insert a access_token_secret'
)
# (Producer, Requestor, topic_name, topic_key)
_registrations: [(producer.Producer, requestor.Requestor, str, str)] = []
_running = False
def __init__(self, host: str, port: int, user_token: str, password_token: str):
self._host = host
self._port = port
self._user_token = user_token
self._password_token = password_token
def start(self):
if self._running:
print('The factory is already running.')
return
self._running = True
for i, e in enumerate(self._registrations):
thread = threading.Thread(target = e[0].start, args = (e[1], e[2], e[3], self.TIMEOUT))
thread.start()
print(f'The factory has been started with {len(self._registrations)} producers.')
def stop(self):
for i, e in enumerate(self._registrations):
e[0].stop()
self._running = False
print(f'The factory is terminating {len(self._registrations)} producers.')
self._registrations.clear()
def register_twitter(self, search_word: str):
req = self._create_twitter_requestor(search_word)
self._register(req, self.TWITTER_TOPIC, search_word)
def register_yahoo(self, stock_name: str):
req = self._create_yahoo_requestor(stock_name)
self._register(req, self.YAHOO_TOPIC, stock_name)
def _register(self, req: requestor.Requestor, topic_name: str, topic_key: str):
prod = self._create_producer()
self._registrations.append((prod, req, topic_name, topic_key))
def _create_producer(self) -> producer.Producer:
return producer.Producer(self._host, self._port, self._user_token, self._password_token)
def _create_twitter_requestor(self, search_word: str) -> requestor.Requestor:
return twitter_api.TwitterAPI(self.TWITTER_AUTH, search_word)
def _create_yahoo_requestor(self, stock_name: str) -> requestor.Requestor:
return yahoo_api.YahooFinanceAPI(stock_name)