Skip to content

Commit fb2f9ee

Browse files
authored
Merge pull request #16 from smcclennon/beta
v1.0.0 - Docker Support & Stability Improvements
2 parents 2ee0890 + 32cfd10 commit fb2f9ee

File tree

6 files changed

+65
-35
lines changed

6 files changed

+65
-35
lines changed

app/ACRAPI.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313
"access_secret": env['acr']['clear']['access_secret'],
1414
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_AUDIO,
1515
"debug": False,
16-
"timeout": env['acr']['clear']['timeout']
16+
"timeout": int(env['acr']['clear']['timeout'])
1717
},
1818
"noisy": {
1919
"host": env['acr']['noisy']['host'],
2020
"access_key": env['acr']['noisy']['access_key'],
2121
"access_secret": env['acr']['noisy']['access_secret'],
2222
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_AUDIO,
2323
"debug": False,
24-
"timeout": env['acr']['noisy']['timeout']
24+
"timeout": int(env['acr']['noisy']['timeout'])
2525
},
2626
"hum": {
2727
"host": env['acr']['hum']['host'],
2828
"access_key": env['acr']['hum']['access_key'],
2929
"access_secret": env['acr']['hum']['access_secret'],
3030
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_BOTH,
3131
"debug": False,
32-
"timeout": env['acr']['hum']['timeout']
32+
"timeout": int(env['acr']['hum']['timeout'])
3333
}
3434
}
3535
logger.info('Loaded: ACR Config')
@@ -49,15 +49,18 @@ def noisy(filePath):
4949
'''This module can recognize ACRCloud by most of audio/video file.
5050
Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
5151
Video: mp4, mkv, wmv, flv, ts, avi ...'''
52-
re = ACRCloudRecognizer(config['noisy'])
52+
re_config = config['noisy']
53+
re = ACRCloudRecognizer(re_config)
5354

5455
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
5556
#re.recognize_by_file(filePath, 0, 10)
56-
logger.info('ACR: Processing request...')
57+
logger.info('ACR: Processing Noisy request...')
58+
logger.debug(re_config)
5759
buf = open(filePath, 'rb').read()
5860
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
5961
data = re.recognize_by_filebuffer(buf, 0, 60)
6062
data = json.loads(data)
63+
logger.debug(data)
6164
logger.info('ACR: Processing complete!')
6265
return data
6366

@@ -69,14 +72,17 @@ def hum(filePath):
6972
'''This module can recognize ACRCloud by most of audio/video file.
7073
Audio: mp3, wav, m4a, flac, aac, amr, ape, ogg ...
7174
Video: mp4, mkv, wmv, flv, ts, avi ...'''
72-
re = ACRCloudRecognizer(config['hum'])
75+
re_config = config['hum']
76+
re = ACRCloudRecognizer(re_config)
7377

7478
#recognize by file path, and skip 0 seconds from from the beginning of sys.argv[1].
7579
#re.recognize_by_file(filePath, 0, 10)
76-
logger.info('ACR: Processing request...')
80+
logger.info('ACR: Processing Hum request...')
81+
logger.debug(re_config)
7782
buf = open(filePath, 'rb').read()
7883
#recognize by file_audio_buffer that read from file path, and skip 0 seconds from from the beginning of sys.argv[1].
7984
data = re.recognize_by_filebuffer(buf, 0, 10)
8085
data = json.loads(data)
86+
logger.debug(data)
8187
logger.info('ACR: Processing complete!')
8288
return data

app/SongID.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import urllib.request # Check for internet connectivity
1111

1212

13-
os.system(f'title _ _ ---==== SongID {ver} ====--- _ _') # Set the windows console window title
14-
1513
while True:
1614
try:
1715
ACR_PING_CODE = urllib.request.urlopen("https://identify-eu-west-1.acrcloud.com").getcode()
@@ -128,10 +126,7 @@ def sendMsg(update, context):
128126
# (When the user adds a telegram bot, they are forced to send '/start')
129127
def startCMD(update, context):
130128
logusr(update)
131-
userID=str(update.effective_chat.id)
132-
username=str(update.effective_chat.username)
133-
if userID not in userdata:
134-
SIDProcessor.addUserData(update, '0', '0')
129+
SIDProcessor.addUserIfNotExists(update)
135130
botsend(update, context, f'''<b>{botName}</b> is a Telegram bot that can identify music, similar to Shazam
136131
137132
Key Features:
@@ -195,6 +190,7 @@ def invalidFiletype(update, context):
195190
# Send the user the data we have saved on them when they send '/mydata'
196191
def mydataCMD(update, context):
197192
logusr(update)
193+
SIDProcessor.addUserIfNotExists(update)
198194
data=SIDProcessor.getUserData(update)
199195
user = update.effective_chat.id
200196
username = data["username"]
@@ -224,15 +220,18 @@ def maintenanceINFO(update, context):
224220

225221

226222
def noisyProcess(update, context):
223+
SIDProcessor.addUserIfNotExists(update)
227224
SIDProcessor.fileProcess(update, context, 'noisy')
228225

229226

230227
# Currently not in use
231228
def clearProcess(update, context):
229+
SIDProcessor.addUserIfNotExists(update)
232230
SIDProcessor.fileProcess(update, context, 'clear')
233231

234232

235233
def humProcess(update, context):
234+
SIDProcessor.addUserIfNotExists(update)
236235
SIDProcessor.fileProcess(update, context, 'hum')
237236

238237
maintenance = 0

app/SongIDCore.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
import telegram, json, time, os
1+
import telegram, json, time, os, logging, sentry_sdk
22
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler, MessageQueue
3-
import sentry_sdk
43

54

6-
ver='1.0.0-beta3'
5+
ver='1.0.0'
76
botName=f'SongID'
87
botVer=f'{botName} {ver}'
98
botAt=f'@SongIDBot'
109
botUsername='SongIDbot'
1110
downloadDIR='downloads'
1211

1312

14-
# Initialise the logger and format it's output
15-
import logging
16-
logging.basicConfig(
17-
level=logging.INFO,
18-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
19-
datefmt='%d-%m-%Y %H:%M:%S',
20-
)
21-
logger = logging.getLogger(__name__)
22-
23-
2413
# Load environment variables
2514
env = {
15+
'environment': os.getenv('SONGID_ENVIRONMENT', 'undefined'),
2616
'sentry_dsn': os.getenv('SONGID_SENTRY_DSN'),
17+
'log_level': os.getenv('SONGID_LOG_LEVEL'),
2718
'telegram': {
2819
'bot_token': os.getenv('SONGID_TELEGRAM_BOT_TOKEN'),
2920
'dev_id': os.getenv('SONGID_TELEGRAM_DEV_ID'),
@@ -57,16 +48,41 @@
5748
token = env['telegram']['bot_token']
5849
devid = env['telegram']['dev_id']
5950
devusername = env['telegram']['dev_username']
51+
loglevel = env['log_level'].upper()
6052
sentry_dsn = env['sentry_dsn']
6153

62-
sentry_sdk.init(
63-
dsn=sentry_dsn,
64-
release=ver,
65-
sample_rate=1.0,
66-
traces_sample_rate=1.0,
67-
attach_stacktrace=True,
68-
with_locals=True
54+
55+
# Initialise the logger and format it's output
56+
if loglevel == 'DEBUG':
57+
loglevel = logging.DEBUG
58+
elif loglevel == 'INFO':
59+
loglevel = logging.INFO
60+
elif loglevel == 'WARNING':
61+
loglevel = logging.WARNING
62+
elif loglevel == 'ERROR':
63+
loglevel = logging.ERROR
64+
else:
65+
loglevel = logging.INFO
66+
print('Invalid log level specified, defaulting to INFO')
67+
68+
print(f'Initializing logger with log level {loglevel}')
69+
logging.basicConfig(
70+
level=loglevel,
71+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
72+
datefmt='%Y-%m-%d %H:%M:%S',
6973
)
74+
logger = logging.getLogger(__name__)
75+
76+
if env['environment'] != 'development':
77+
sentry_sdk.init(
78+
dsn=sentry_dsn,
79+
release=ver,
80+
environment=env['environment'],
81+
sample_rate=1.0,
82+
traces_sample_rate=1.0,
83+
attach_stacktrace=True,
84+
with_locals=True
85+
)
7086

7187

7288
# Load data/userdata.json into the variable 'userdata'

app/SongIDProcessor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ def dataProcess(update, context, data):
210210

211211
class SIDProcessor():
212212

213+
def addUserIfNotExists(update):
214+
userID=str(update.effective_chat.id)
215+
username=str(update.effective_chat.username)
216+
if userID not in userdata:
217+
logger.info(f'User does not exist: {update.effective_user.id}')
218+
SIDProcessor.addUserData(update, '0', '0')
219+
213220
# Add user data to the 'userdata' variable and save it to disk
214221
def addUserData(update, apiCalls, lastCall):
215222
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}'}

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ services:
88
- songid-data:/app/data # Use a named volume for the data directory
99
environment:
1010
- PYTHONUNBUFFERED=1 # See logs in real-time
11-
- SONGID_SENTRY_DSN=${SONGID_SENTRY_DSN} # Error logging (https://[email protected]/0)
11+
- SONGID_LOG_LEVEL=${SONGID_LOG_LEVEL} # DEBUG, INFO, WARNING, ERROR
12+
- SONGID_ENVIRONMENT=${SONGID_ENVIRONMENT} # production, staging, development
13+
- SONGID_SENTRY_DSN=${SONGID_SENTRY_DSN} # Remote error logging (https://[email protected]/0)
1214
- SONGID_TELEGRAM_BOT_TOKEN=${SONGID_TELEGRAM_BOT_TOKEN}
1315
- SONGID_TELEGRAM_DEV_ID=${SONGID_TELEGRAM_DEV_ID} # Developer ID (000000000)
1416
- SONGID_TELEGRAM_DEV_USERNAME=${SONGID_TELEGRAM_DEV_USERNAME} # Developer username (@username)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
git+https://github.com/acrcloud/acrcloud_sdk_python.git@8354634bb24e737d67918dfc507cfc4b1d0fbc1e
1+
pyacrcloud==1.0.2
22
python-telegram-bot==13.7
33
sentry-sdk==1.5.4

0 commit comments

Comments
 (0)