-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #8
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,7 @@ class ACRAPI(): | |
|
||
|
||
|
||
def noisy(filePath): | ||
def noisy(self): | ||
config = config_noisy | ||
|
||
'''This module can recognize ACRCloud by most of audio/video file. | ||
|
@@ -59,7 +59,7 @@ def noisy(filePath): | |
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1]. | ||
#re.recognize_by_file(filePath, 0, 10) | ||
logger.info('ACR: Processing request...') | ||
buf = open(filePath, 'rb').read() | ||
buf = open(self, 'rb').read() | ||
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1]. | ||
data = re.recognize_by_filebuffer(buf, 0, 60) | ||
data = json.loads(data) | ||
|
@@ -69,7 +69,7 @@ def noisy(filePath): | |
|
||
|
||
|
||
def hum(filePath): | ||
def hum(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
config = config_hum | ||
|
||
'''This module can recognize ACRCloud by most of audio/video file. | ||
|
@@ -80,7 +80,7 @@ def hum(filePath): | |
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1]. | ||
#re.recognize_by_file(filePath, 0, 10) | ||
logger.info('ACR: Processing request...') | ||
buf = open(filePath, 'rb').read() | ||
buf = open(self, 'rb').read() | ||
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1]. | ||
data = re.recognize_by_filebuffer(buf, 0, 10) | ||
data = json.loads(data) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ | |
logger.info('ACR Cloud pinged successfully!') | ||
break | ||
else: | ||
logger.warning('ACR Cloud ping error code: '+str(ACR_PING_CODE)+', retrying in 20 seconds') | ||
logger.warning( | ||
f'ACR Cloud ping error code: {str(ACR_PING_CODE)}, retrying in 20 seconds' | ||
) | ||
|
||
Comment on lines
-22
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
time.sleep(20) | ||
except: | ||
logger.warning('Unable to ping ACR Cloud, retrying in 10 seconds') | ||
|
@@ -57,10 +60,8 @@ def error(update, context): | |
#update.effective_message.reply_text('⚠️ Telegram closed the connection. Please try again.') | ||
#logbot(update, '⚠️ Telegram closed the connection. Please try again.') | ||
logger.info('existing connection closed (error exception catch temp code), pass') | ||
pass | ||
elif '[WinError 32] The process cannot access the file because it is being used by another process' in str(context.error): | ||
logger.info('File cannot be accessed (likely deleted), being used by another process, pass') | ||
pass | ||
Comment on lines
-60
to
-63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
else: | ||
if update != None: | ||
text = "⚠️ An error occured, sorry for any inconvenience caused.\nThe developer has been notified and will look into this issue as soon as possible." | ||
|
@@ -108,7 +109,7 @@ def restart(update, context): | |
def sendMsg(update, context): | ||
logusr(update) | ||
processed = SIDProcessor.commandArgs(update, context) | ||
if processed == None: | ||
if processed is None: | ||
Comment on lines
-111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
logbotsend(update, context, '⚠️ Invalid syntax! <i>Make sure your spacing is correct</i>') | ||
helpCMD(update, context) | ||
elif processed[0] == 'too_long': | ||
|
@@ -238,7 +239,25 @@ def humProcess(update, context): | |
maintenance = 0 | ||
|
||
dp.add_error_handler(error) # Handle uncaught exceptions | ||
if maintenance == 1: | ||
if maintenance == 0: | ||
dp.add_handler(CommandHandler('start', startCMD)) # Respond to '/start' | ||
dp.add_handler(CommandHandler('mydata', mydataCMD)) # Respond to '/mydata' | ||
dp.add_handler(CommandHandler('help', helpCMD)) # Respond to '/help' | ||
dp.add_handler(CommandHandler('limit', limitCMD)) # Respond to '/limit' | ||
|
||
# Handle different types of file uploads | ||
dp.add_handler(MessageHandler(Filters.audio, noisyProcess)) | ||
dp.add_handler(MessageHandler(Filters.video, noisyProcess)) | ||
dp.add_handler(MessageHandler(Filters.voice, humProcess)) | ||
|
||
|
||
dp.add_handler(MessageHandler(Filters.photo, invalidFiletype)) # Notify user of invalid file upload | ||
dp.add_handler(MessageHandler(Filters.document, invalidFiletype)) # Notify user of invalid file upload | ||
dp.add_handler(CommandHandler('r', restart, filters=Filters.user(username=devusername))) # Allow the developer to restart the bot | ||
dp.add_handler(CommandHandler('send', sendMsg, filters=Filters.user(username=devusername))) # Allow the developer to send messages to users | ||
dp.add_handler(MessageHandler(Filters.command, unknownCMD)) # Notify user of invalid command | ||
dp.add_handler(MessageHandler(Filters.text, helpCMD)) # Respond to text | ||
elif maintenance == 1: | ||
Comment on lines
-241
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
logger.info('- - - - MAINTENANCE MODE ENABLED - - - -') | ||
dp.add_handler(CommandHandler('start', startCMD)) # Respond to '/start' | ||
|
||
|
@@ -260,34 +279,14 @@ def humProcess(update, context): | |
|
||
dp.add_handler(MessageHandler(Filters.text, maintenanceINFO)) # Respond to text | ||
|
||
elif maintenance == 0: | ||
dp.add_handler(CommandHandler('start', startCMD)) # Respond to '/start' | ||
dp.add_handler(CommandHandler('mydata', mydataCMD)) # Respond to '/mydata' | ||
dp.add_handler(CommandHandler('help', helpCMD)) # Respond to '/help' | ||
dp.add_handler(CommandHandler('limit', limitCMD)) # Respond to '/limit' | ||
|
||
# Handle different types of file uploads | ||
dp.add_handler(MessageHandler(Filters.audio, noisyProcess)) | ||
dp.add_handler(MessageHandler(Filters.video, noisyProcess)) | ||
dp.add_handler(MessageHandler(Filters.voice, humProcess)) | ||
|
||
|
||
dp.add_handler(MessageHandler(Filters.photo, invalidFiletype)) # Notify user of invalid file upload | ||
dp.add_handler(MessageHandler(Filters.document, invalidFiletype)) # Notify user of invalid file upload | ||
dp.add_handler(CommandHandler('r', restart, filters=Filters.user(username=devusername))) # Allow the developer to restart the bot | ||
dp.add_handler(CommandHandler('send', sendMsg, filters=Filters.user(username=devusername))) # Allow the developer to send messages to users | ||
dp.add_handler(MessageHandler(Filters.command, unknownCMD)) # Notify user of invalid command | ||
dp.add_handler(MessageHandler(Filters.text, helpCMD)) # Respond to text | ||
logger.info('Loaded: Handlers') | ||
|
||
|
||
logger.info('Loading Complete!') | ||
if heroku_enabled == 'True': | ||
logger.info('Initialising Heroku webhook...') | ||
PORT = int(os.environ.get('PORT', int(heroku_port))) | ||
u.start_webhook(listen=heroku_listen, | ||
port=int(PORT), | ||
url_path=token) | ||
u.start_webhook(listen=heroku_listen, port=PORT, url_path=token) | ||
u.bot.setWebhook(heroku_webhook + token) | ||
logger.info('Heroku webhook initialised') | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
|
||
|
||
ver='0.2.4' | ||
botName=f'SongID' | ||
botName = 'SongID' | ||
botVer=f'{botName} {ver}' | ||
botAt=f'@SongIDBot' | ||
botAt = '@SongIDBot' | ||
Comment on lines
-7
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
botUsername='SongIDbot' | ||
downloadDIR='data/downloads' | ||
|
||
|
@@ -69,7 +69,10 @@ def logusr(update): | |
# Send a message to the user | ||
def botsend(update, context, msg): | ||
if hasattr(update.message, 'reply_text'): | ||
update.message.reply_text(str(msg)+f'\n\n<i>{botAt} <code>{ver}</code></i>', parse_mode=telegram.ParseMode.HTML) | ||
update.message.reply_text( | ||
f'{str(msg)}\n\n<i>{botAt} <code>{ver}</code></i>', | ||
parse_mode=telegram.ParseMode.HTML, | ||
) | ||
Comment on lines
-72
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def devsend(update, context, msg): | ||
if '{update.message.text}' in msg: | ||
|
@@ -82,7 +85,11 @@ def devsend(update, context, msg): | |
|
||
# Send a message to the user and log the message sent | ||
def logbotsend(update, context, msg): | ||
update.message.reply_text(str(msg)+f'\n\n<i>{botAt} <code>{ver}</code></i>', parse_mode=telegram.ParseMode.HTML) | ||
update.message.reply_text( | ||
f'{str(msg)}\n\n<i>{botAt} <code>{ver}</code></i>', | ||
parse_mode=telegram.ParseMode.HTML, | ||
) | ||
|
||
Comment on lines
-85
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
logger.info(f'[@{botUsername}][{botName}][M:{update.effective_message.message_id}]: {msg}') | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ def saveUserData(): | |
# Format milliseconds to minutes:seconds (used for track-length) | ||
def msConvert(ms): | ||
ms = int(ms) | ||
seconds=int(ms/1000) | ||
seconds = ms // 1000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return time.strftime('%M:%S', time.gmtime(seconds)) | ||
|
||
|
||
|
@@ -51,7 +51,7 @@ def authorised(update): | |
# Download the users uploaded file | ||
def fileDownload(update, context): | ||
file_id = None | ||
while file_id == None: | ||
while file_id is None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# Get the file-id | ||
try: | ||
file_id = update.effective_message.audio.file_id | ||
|
@@ -77,7 +77,7 @@ def fileDownload(update, context): | |
try: | ||
file_info = context.bot.get_file(file_id) | ||
file_size = file_info["file_size"] | ||
if 20000000 - int(file_size) >= 0: | ||
if int(file_size) <= 20000000: | ||
web_path = file_info["file_path"] # Get the original filename | ||
extension = os.path.splitext(f'{web_path}')[1] # Get the file extension (.mp3, .mp4 etc) | ||
fileName = f'{update.effective_chat.id}_{update.effective_message.message_id}_{file_id}{extension}' | ||
|
@@ -210,109 +210,133 @@ def dataProcess(update, context, data): | |
class SIDProcessor(): | ||
|
||
# Add user data to the 'userdata' variable and save it to disk | ||
def addUserData(update, apiCalls, lastCall): | ||
userdata[f'{update.effective_user.id}'] = {'username': f'{update.effective_chat.username}', 'name': f'{update.effective_user.first_name} {update.effective_user.last_name}', 'api_calls': f'{apiCalls}', 'last_call': f'{lastCall}'} | ||
logger.info(f'User data added/updated: [{update.effective_user.id}: {update.effective_user.username}, {update.effective_user.first_name} {update.effective_user.last_name}, {apiCalls}, {lastCall}]') | ||
def addUserData(self, apiCalls, lastCall): | ||
userdata[f'{self.effective_user.id}'] = { | ||
'username': f'{self.effective_chat.username}', | ||
'name': f'{self.effective_user.first_name} {self.effective_user.last_name}', | ||
'api_calls': f'{apiCalls}', | ||
'last_call': f'{lastCall}', | ||
} | ||
|
||
logger.info( | ||
f'User data added/updated: [{self.effective_user.id}: {self.effective_user.username}, {self.effective_user.first_name} {self.effective_user.last_name}, {apiCalls}, {lastCall}]' | ||
) | ||
|
||
Comment on lines
-213
to
+224
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
saveUserData() | ||
|
||
# Get user data for the respective user | ||
def getUserData(update): | ||
def getUserData(self): | ||
#update = json.loads(update) | ||
logger.debug('getUserData(0/2)') | ||
|
||
# May be completely unnecessary, just in case | ||
if hasattr(update, 'effective_chat'): | ||
if hasattr(self, 'effective_chat'): | ||
# Get users Telegram ID | ||
userID=str(update.effective_chat.id) | ||
userID = str(self.effective_chat.id) | ||
# Add user to userdata in case they did not initially send /start | ||
# to prevent AttributeErrors occurring from their key not being | ||
# present in the database | ||
# (https://github.com/smcclennon/SongID/issues/6#issuecomment-1021517621) | ||
if userID not in userdata: | ||
SIDProcessor.addUserData(update, '0', '0') | ||
SIDProcessor.addUserData(self, '0', '0') | ||
logger.debug('getUserData(1/2)') | ||
|
||
data = userdata[f'{update.effective_user.id}'] | ||
data = userdata[f'{self.effective_user.id}'] | ||
Comment on lines
-219
to
+244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
logger.debug('getUserData(2/2)') | ||
return data | ||
|
||
|
||
|
||
|
||
# If authorised, download the users uploaded file and send it to the API response processor, and then delete the downloaded file from disk | ||
def fileProcess(update, context, processor): | ||
logusr(update) | ||
for attempt in range(0,10): | ||
def fileProcess(self, context, processor): | ||
logusr(self) | ||
for _ in range(10): | ||
try: | ||
logger.info('fileProcess: Attempting to send ChatAction.TYPING') | ||
context.bot.sendChatAction(chat_id=update.effective_chat.id, action=telegram.ChatAction.TYPING, timeout=10) | ||
context.bot.sendChatAction( | ||
chat_id=self.effective_chat.id, | ||
action=telegram.ChatAction.TYPING, | ||
timeout=10, | ||
) | ||
|
||
logger.info('fileProcess: Successfully sent ChatAction.TYPING') | ||
except: | ||
logger.info('fileProcess: Failed to send ChatAction.TYPING') | ||
continue | ||
logger.info('fileProcess: Breaking from ChatAction loop') | ||
break | ||
if authorised(update): | ||
context.bot.sendChatAction(chat_id=update.effective_chat.id, action=telegram.ChatAction.RECORD_AUDIO, timeout=20) | ||
fileName = fileDownload(update, context) | ||
if authorised(self): | ||
context.bot.sendChatAction( | ||
chat_id=self.effective_chat.id, | ||
action=telegram.ChatAction.RECORD_AUDIO, | ||
timeout=20, | ||
) | ||
|
||
fileName = fileDownload(self, context) | ||
if fileName != 'FILE_TOO_BIG': | ||
deleteSuccess = 0 | ||
while deleteSuccess != 5: | ||
try: | ||
if processor == 'noisy': | ||
dataProcess(update, context, ACRAPI.noisy(f'{downloadDIR}/{fileName}')) | ||
dataProcess(self, context, ACRAPI.noisy(f'{downloadDIR}/{fileName}')) | ||
os.remove(f'{downloadDIR}/{fileName}') | ||
elif processor == 'clear': | ||
dataProcess(update, context, ACRAPI.clear(f'{downloadDIR}/{fileName}')) | ||
dataProcess(self, context, ACRAPI.clear(f'{downloadDIR}/{fileName}')) | ||
os.remove(f'{downloadDIR}/{fileName}') | ||
elif processor == 'hum': | ||
dataProcess(update, context, ACRAPI.hum(f'{downloadDIR}/{fileName}')) | ||
dataProcess(self, context, ACRAPI.hum(f'{downloadDIR}/{fileName}')) | ||
os.remove(f'{downloadDIR}/{fileName}') | ||
deleteSuccess = 5 | ||
except: | ||
deleteSuccess+=1 | ||
continue | ||
else: | ||
timeLeft_int = timeLeft(update) | ||
timeLeft_int = timeLeft(self) | ||
if timeLeft_int == 1: | ||
time_msg = f'{timeLeft_int} second' | ||
else: | ||
time_msg = f'{timeLeft_int} seconds' | ||
logbotsend(update, context, f'Due to an increased volume of requests, a 20 second cooldown has been put in place to benefit the user.\n\nPlease wait {time_msg} before making another request') | ||
context.bot.send_message(devid, f'User @{update.effective_user.username} ({update.effective_chat.id}) hit the cooldown ({time_msg} left)') | ||
logbotsend( | ||
self, | ||
context, | ||
f'Due to an increased volume of requests, a 20 second cooldown has been put in place to benefit the user.\n\nPlease wait {time_msg} before making another request', | ||
) | ||
|
||
context.bot.send_message( | ||
devid, | ||
f'User @{self.effective_user.username} ({self.effective_chat.id}) hit the cooldown ({time_msg} left)', | ||
) | ||
Comment on lines
-243
to
+308
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# Split the command arguments into an array | ||
def commandArgs(update, context): | ||
def commandArgs(self, context): | ||
whitespace=[] | ||
keysplit=1 | ||
msgsplit=2 | ||
command = update.message.text | ||
command = self.message.text | ||
split = command.split(' ') # Split the message with each space | ||
if len(split) < 3: | ||
return None | ||
if len(split) >= 3: | ||
key = split[1] | ||
while key == "": # If the user used irregular spacing | ||
keysplit+=1 | ||
whitespace.append(keysplit-1) | ||
key = split[keysplit] | ||
key = split[1] | ||
while key == "": # If the user used irregular spacing | ||
keysplit+=1 | ||
whitespace.append(keysplit-1) | ||
key = split[keysplit] | ||
|
||
message = command | ||
for space in whitespace: | ||
message = message.replace(split[space], '', 1) | ||
message = message.replace(split[0]+' ', '', 1) | ||
message = message.replace(split[keysplit]+' ', '', 1) | ||
message = command | ||
for space in whitespace: | ||
message = message.replace(split[space], '', 1) | ||
message = message.replace(f'{split[0]} ', '', 1) | ||
message = message.replace(f'{split[keysplit]} ', '', 1) | ||
|
||
if len(message) > 5000: | ||
return ['too_long', len(message)-5000] | ||
return [key, message] | ||
if len(message) > 5000: | ||
return ['too_long', len(message)-5000] | ||
return [key, message] | ||
Comment on lines
-284
to
+332
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
|
||
# Find the parent key(s) within a dictionary | ||
def find_key(d, value): # https://stackoverflow.com/a/15210253 | ||
for k,v in d.items(): | ||
def find_key(self, value): # https://stackoverflow.com/a/15210253 | ||
for k,v in self.items(): | ||
if isinstance(v, dict): | ||
p = SIDProcessor.find_key(v, value) | ||
if p: | ||
if p := SIDProcessor.find_key(v, value): | ||
Comment on lines
-311
to
+339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
return [k] + p | ||
elif v == value: | ||
return [k] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
ACRAPI.noisy
refactored with the following changes:self
(instance-method-first-arg-name
)