forked from fablab-wue/piTelex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txDevTwitter.py
224 lines (183 loc) · 7.12 KB
/
txDevTwitter.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
#!/usr/bin/python3
# -*- coding: future_fstrings -*-
"""
Fernschreiber Twitter Client
requires python-fstrings
pip3 install future-fstrings
"""
__author__ = "DirkNiggemann"
__email__ = "[email protected]"
__copyright__ = "CC 2019 DirkNiggemann"
__license__ = "CC0"
__version__ = "0.0.1"
import threading
import time
import datetime
import queue
import tweepy
from twitivity import Event
import json
import txDevITelexCommon
import txCode
import log
import logging
logging.basicConfig(level=logging.WARN)
logger = logging.getLogger(__name__)
def LOG(text:str, level:int=3):
log.LOG('\033[5;30;46m<'+text+'>\033[0m', level)
class TelexTwitter(txDevITelexCommon.TelexITelexCommon):
def __init__(self, **params):
super().__init__()
self.id = 'Twt'
self.running = True
self.chars_buffer = ''
self._is_online = False
self.twitter_client = Twitter_Client(params.get("consumer_key", ""), params.get("consumer_secret", ""), params.get("access_token_key", ""), params.get("access_token_secret", ""), params.get("follow", []), params.get("track", []), params.get("languages", []), params.get("url", []), params.get("host", []), params.get("port", []))
self.thread = threading.Thread(target=self.thread_function, name='Twitter_Handler')
self.thread.start()
def exit(self):
self.twitter_client.stop()
self.running = False
self.thread.join()
# =====
def read(self) -> str:
if self._rx_buffer:
if self._is_online:
return self._rx_buffer.pop(0)
else:
self._is_online = True
return '\x1bA'
def write(self, a: str, source: str):
if len(a) != 1:
if a == '\x1bA':
self._is_online = True
if a == '\x1bZ':
self._is_online = False
if a == '\x1bWB':
self._is_online = True
self._rx_buffer.append('\x1bA')
return
if a not in "<>":
self._tx_buffer.append(a)
# =====
def add_chars(self, chars):
for char in chars:
if char == '\r':
continue
if char == '\n':
self.twitter_client.send_msg(self.chars_buffer)
self.chars_buffer = ''
continue
self.chars_buffer += char
def thread_function(self):
"""
Twitter client handler
"""
last_date = None
while self.running:
try:
data = self.twitter_client.get_msg()
if data is not None:
if isinstance(data, dict):
#LOG(json.dumps(data), 1)
events = data['tweet_create_events']
uname = events[0]['user']['screen_name']
ctime = datetime.datetime.strptime(events[0]['created_at'], '%a %b %d %H:%M:%S %z %Y')
txt = events[0]['text']
else:
uname = data.user.screen_name
ctime = data.created_at
txt = data.text
#if data['type'] == 'ACTION':
msg = f'={uname} = {txt}'
#if data['msg'].startswith(f'{self.irc_client.nick}:'):
# msg = f'{msg}\a\a\a' # BEL
LOG(uname)
if '@' + uname in self.twitter_client.follow or not self.twitter_client.follow:
msg = f'{ctime.strftime("%H:%M")} {msg}\n\r'
if last_date != ctime.timetuple().tm_yday:
msg = f'{ctime.strftime("%A %d %B")}\n\r {msg}'
last_date = ctime.timetuple().tm_yday
text = txCode.BaudotMurrayCode.ascii_to_tty_text(msg)
# TODO: insert linebreak after 65 characters
for a in text:
self._rx_buffer.append(a)
if self._tx_buffer:
a = self._tx_buffer.pop(0)
data = str(a.encode('ASCII'), 'utf8').lower()
self.add_chars(data)
except Exception as e:
LOG(str(e),1)
LOG('end connection', 2)
self._connected = False
#######
class Twitter_Client():
class UserStreamListener(tweepy.StreamListener):
def __init__(self, client):
super().__init__()
self.client = client
def on_status(self, status):
LOG(f'IN: {status}', 3)
self.client.q.put(status)
class ActivityStreamEvent(Event):
CALLBACK_URL: str = "https://telex.mupptastic.org.uk:4330/listener"
def __init__(self, client, url):
super().__init__()
self.client = client
self.CALLBACK_URL = url
def on_data(self, data: json) -> None:
LOG(f'IN: {data}', 3)
self.client.q.put(data)
LANGUAGES = ['en']
def __init__(self, consumer_key, consumer_secret, access_token_key, access_token_secret, follow, track, languages, url, host, port):
try:
self.auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
self.auth.set_access_token(
access_token_key,
access_token_secret)
self.api = tweepy.API(self.auth)
except tweepy.error.TweepError as e:
LOG(str(e),1)
self.q = queue.Queue()
self.follow = follow
self.track = track
self.languages = languages
self.url = url
self.host = host
self.port = port
self.running = True
self.thread = threading.Thread(target=self.thread_function, name='Twitter_Client')
self.thread.start()
def send_msg(self, msg):
try:
status = self.api.update_status(msg)
except UnicodeDecodeError:
LOG("Your message could not be encoded. Perhaps it contains non-ASCII characters? ",1)
LOG("Try explicitly specifying the encoding with the --encoding flag",1)
except tweepy.error.TweepError as e:
LOG(str(e),1)
LOG(f'TWEET: {msg}', 3)
def stop(self, quit_msg='Wah!'):
self.running = False
self.thread.join()
#self.api.close()
del self
def get_msg(self):
if self.q.empty():
return None
return self.q.get()
def thread_function(self):
while self.running:
try:
if self.follow:
self.idl = [ str(self.api.get_user(screen_name=u).id) for u in self.follow ]
self.listener = Twitter_Client.UserStreamListener(self)
self.stream = tweepy.Stream(auth=self.api.auth, listener=self.listener)
self.stream.filter(follow=self.idl, track=self.track)
time.sleep(10000000)
else:
LOG("EEP", 1)
stream_events = Twitter_Client.ActivityStreamEvent(self, self.url)
stream_events._server.run(debug=False, ssl_context='adhoc', host=self.host, port = self.port)
except tweepy.error.TweepError as e:
LOG(str(e), 1)