Passed
Push — master ( b90ced...a5e1e1 )
by Ian
06:05
created

build.rsudp.c_telegram   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 174
Duplicated Lines 6.9 %

Test Coverage

Coverage 61.76%

Importance

Changes 0
Metric Value
wmc 26
eloc 106
dl 12
loc 174
ccs 63
cts 102
cp 0.6176
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A Telegrammer.getq() 0 10 2
B Telegrammer.__init__() 0 37 5
A Telegrammer.run() 12 12 4
B Telegrammer._when_alarm() 0 34 5
B Telegrammer._when_img() 0 37 8
A Telegrammer.auth() 0 3 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 1
import os, sys
2 1
import time
3 1
import rsudp.raspberryshake as rs
4 1
from rsudp import printM, printW, printE, helpers
5 1
from rsudp.test import TEST
6 1
import telegram as tg
7
8 1
class Telegrammer(rs.ConsumerThread):
9
	'''
10
	 .. versionadded:: 0.4.2
11
12
	.. |telegram| raw:: html
13
14
		<a href="https://t.me/" target="_blank">Telegram</a>
15
16
	.. |sasmex_use| raw:: html
17
18
		<a href="https://t.me/sasmex" target="_blank">Mexican Early Warning System (SASMEX)</a>
19
20
	|telegram| is a free messaging service which,
21
	among other things, is suited to quickly broadcasting automatic
22
	notifications via an API.
23
	It is used by the |sasmex_use| and PanamaIGC.
24
25
	:param str token: bot token from Telegram bot creation
26
	:param str chat_id: Telegram chat ID number that this module will post to
27
	:param bool send_images: whether or not to send images. if False, only alerts will be sent.
28
	:param queue.Queue q: queue of data and messages sent by :class:`rsudp.c_consumer.Consumer`
29
30
	'''
31 1
	def __init__(self, token, chat_id, testing=False,
32
				 q=False, send_images=False, extra_text=False,
33
				 sender='Telegram'):
34
		"""
35
		Initializing the Telegram message posting thread.
36
37
		"""
38 1
		super().__init__()
39 1
		self.sender = sender
40 1
		self.alive = True
41 1
		self.send_images = send_images
42 1
		self.token = token
43 1
		self.chat_id = chat_id
44 1
		self.extra_text = ' %s' % (extra_text) if extra_text else ''
45 1
		self.testing = testing
46 1
		self.fmt = '%Y-%m-%d %H:%M:%S.%f'
47 1
		self.region = ' - region: %s' % rs.region.title() if rs.region else ''
48
49 1
		if q:
50 1
			self.queue = q
51
		else:
52
			printE('no queue passed to consumer! Thread will exit now!', self.sender)
53
			sys.stdout.flush()
54
			self.alive = False
55
			sys.exit()
56
57 1
		if not self.testing:
58
			self.telegram = tg.Bot(token=self.token)
59
		else:
60 1
			printW('The Telegram module will not post to Telegram in Testing mode.',
61
					self.sender, announce=False)
62
63 1
		self.livelink = u'live feed ➡️ https://stationview.raspberryshake.org/#?net=%s&sta=%s' % (rs.net, rs.stn)
64 1
		self.message0 = '(Raspberry Shake station %s.%s%s) Event detected at' % (rs.net, rs.stn, self.region)
65 1
		self.last_message = False
66
67 1
		printM('Starting.', self.sender)
68
69
70 1
	def auth(self):
71
		if not self.testing:
72
			self.telegram = tg.Bot(token=self.token)
73
74
75 1
	def getq(self):
76 1
		d = self.queue.get()
77 1
		self.queue.task_done()
78
79 1
		if 'TERM' in str(d):
80 1
			self.alive = False
81 1
			printM('Exiting.', self.sender)
82 1
			sys.exit()
83
		else:
84 1
			return d
85
86
87 1
	def _when_alarm(self, d):
88
		'''
89
		Send a telegram in an alert scenario.
90
91
		:param bytes d: queue message
92
		'''
93 1
		event_time = helpers.fsec(helpers.get_msg_time(d))
94 1
		self.last_event_str = '%s' % (event_time.strftime(self.fmt)[:22])
95 1
		message = '%s %s UTC%s - %s' % (self.message0, self.last_event_str, self.extra_text, self.livelink)
96 1
		response = None
97 1
		try:
98 1
			printM('Sending alert...', sender=self.sender)
99 1
			printM('Telegram message: %s' % (message), sender=self.sender)
100 1
			if not self.testing:
101
				response = self.telegram.sendMessage(chat_id=self.chat_id, text=message)
102
			else:
103 1
				TEST['c_telegram'][1] = True
104
105
		except Exception as e:
106
			printE('Could not send alert - %s' % (e))
107
			try:
108
				printE('Waiting 5 seconds and trying to send again...', sender=self.sender, spaces=True)
109
				time.sleep(5)
110
				self.auth()
111
				printM('Telegram message: %s' % (message), sender=self.sender)
112
				if not self.testing:
113
					response = self.telegram.sendMessage(chat_id=self.chat_id, text=message)
114
				else:
115
					# if you are here in testing mode, there is a problem
116
					TEST['c_telegram'][1] = False
117
			except Exception as e:
118
				printE('Could not send alert - %s' % (e))
119
				response = None
120 1
		self.last_message = message
121
122
123 1
	def _when_img(self, d):
124
		'''
125
		Send a telegram image in when you get an ``IMGPATH`` message.
126
127
		:param bytes d: queue message
128
		'''
129 1
		if self.send_images:
130 1
			imgpath = helpers.get_msg_path(d)
131 1
			response = None
132 1
			if os.path.exists(imgpath):
133 1
				with open(imgpath, 'rb') as image:
134 1
					try:
135 1
						if not self.testing:
136
							printM('Uploading image to Telegram %s' % (imgpath), self.sender)
137
							response = self.telegram.sendPhoto(chat_id=self.chat_id, photo=image)
138
							printM('Sent image', sender=self.sender)
139
						else:
140 1
							printM('Image ready to send - %s' % (imgpath), self.sender)
141 1
							TEST['c_telegramimg'][1] = True
142
					except Exception as e:
143
						printE('Could not send image - %s' % (e))
144
						try:
145
							if not self.testing:
146
								printM('Waiting 5 seconds and trying to send again...', sender=self.sender)
147
								time.sleep(5.1)
148
								self.auth()
149
								printM('Uploading image to Telegram (2nd try) %s' % (imgpath), self.sender)
150
								response = self.telegram.sendPhoto(chat_id=self.chat_id, photo=image)
151
								printM('Sent image', sender=self.sender)
152
							else:
153
								# if you are here in testing mode, there is a problem
154
								TEST['c_telegramimg'][1] = False
155
						except Exception as e:
156
							printE('Could not send image - %s' % (e))
157
							response = None
158
			else:
159
				printM('Could not find image: %s' % (imgpath), sender=self.sender)
160
161
162 1 View Code Duplication
	def run(self):
163
		"""
164
		Reads data from the queue and sends a message if it sees an ALARM or IMGPATH message
165
		"""
166 1
		while True:
167 1
			d = self.getq()
168
169 1
			if 'ALARM' in str(d):
170 1
				self._when_alarm(d)
171
172 1
			elif 'IMGPATH' in str(d):
173
				self._when_img(d)
174