Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ACRAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ACRAPI():



def noisy(filePath):
def noisy(self):
Copy link
Author

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:

config = config_noisy

'''This module can recognize ACRCloud by most of audio/video file.
Expand All @@ -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)
Expand All @@ -69,7 +69,7 @@ def noisy(filePath):



def hum(filePath):
def hum(self):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ACRAPI.hum refactored with the following changes:

config = config_hum

'''This module can recognize ACRCloud by most of audio/video file.
Expand All @@ -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)
Expand Down
51 changes: 25 additions & 26 deletions SongID.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 22-22 refactored with the following changes:

time.sleep(20)
except:
logger.warning('Unable to ping ACR Cloud, retrying in 10 seconds')
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function error refactored with the following changes:

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."
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function sendMsg refactored with the following changes:

logbotsend(update, context, '⚠️ Invalid syntax! <i>Make sure your spacing is correct</i>')
helpCMD(update, context)
elif processed[0] == 'too_long':
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 241-290 refactored with the following changes:

logger.info('- - - - MAINTENANCE MODE ENABLED - - - -')
dp.add_handler(CommandHandler('start', startCMD)) # Respond to '/start'

Expand All @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions SongIDCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 7-9 refactored with the following changes:

botUsername='SongIDbot'
downloadDIR='data/downloads'

Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function botsend refactored with the following changes:


def devsend(update, context, msg):
if '{update.message.text}' in msg:
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function logbotsend refactored with the following changes:

logger.info(f'[@{botUsername}][{botName}][M:{update.effective_message.message_id}]: {msg}')


Expand Down
114 changes: 69 additions & 45 deletions SongIDProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function msConvert refactored with the following changes:

return time.strftime('%M:%S', time.gmtime(seconds))


Expand Down Expand Up @@ -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:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function fileDownload refactored with the following changes:

# Get the file-id
try:
file_id = update.effective_message.audio.file_id
Expand All @@ -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}'
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SIDProcessor.addUserData refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SIDProcessor.getUserData refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SIDProcessor.fileProcess refactored with the following changes:

# 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SIDProcessor.commandArgs refactored with the following changes:



# 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SIDProcessor.find_key refactored with the following changes:

return [k] + p
elif v == value:
return [k]
Loading