Skip to content

Commit 5846e2c

Browse files
committed
Added initial Docker compatibility
Now using env vars rather than config files Source code moved to app folder Removed Heroku support Removed obsolete files
1 parent 455c46e commit 5846e2c

14 files changed

+126
-268
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
__pycache__
22
*.png
33
*.psd
4-
token.json
5-
userdata.json
6-
data/downloads
4+
#userdata.json
5+
set_env_vars.sh

Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Python image from the Docker Hub
2+
FROM python:3.9-slim
3+
4+
# Install Git
5+
RUN apt-get update && apt-get install -y git
6+
7+
# Set the working directory in the container
8+
WORKDIR /app
9+
10+
# Copy the requirements file
11+
COPY requirements.txt .
12+
13+
# Install dependencies
14+
RUN pip install --no-cache-dir -r requirements.txt
15+
16+
# Copy the current directory contents into the container at /app
17+
COPY app .
18+
19+
# Command to run your application
20+
CMD ["python", "SongID.py"]

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

ACRAPI.py renamed to app/ACRAPI.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,33 @@
66

77

88
# Get the ACRCloud config
9-
with open('data/acrcloud.json', 'r') as f:
10-
config = json.load(f)
11-
logger.info('Loaded: ACR Config')
12-
13-
14-
# Get the ACRCloud access & private keys
15-
acrkey = all_tokens["acr"]
16-
17-
18-
19-
20-
config_clear = config["clear"]
21-
config_clear["access_key"] = acrkey["clear"]["access_key"]
22-
config_clear["access_secret"] = acrkey["clear"]["access_secret"]
23-
config_clear["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_AUDIO
24-
config_clear["debug"] = False
25-
26-
27-
config_noisy = config["noisy"]
28-
config_noisy["access_key"] = acrkey["noisy"]["access_key"]
29-
config_noisy["access_secret"] = acrkey["noisy"]["access_secret"]
30-
config_noisy["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_AUDIO
31-
config_noisy["debug"] = False
32-
33-
34-
config_hum = config["hum"]
35-
config_hum["access_key"] = acrkey["hum"]["access_key"]
36-
config_hum["access_secret"] = acrkey["hum"]["access_secret"]
37-
config_hum["recognize_type"] = ACRCloudRecognizeType.ACR_OPT_REC_BOTH
38-
config_hum["debug"] = False
39-
9+
config = {
10+
"clear": {
11+
"host": env['acr']['clear']['host'],
12+
"access_key": env['acr']['clear']['access_key'],
13+
"access_secret": env['acr']['clear']['access_secret'],
14+
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_AUDIO,
15+
"debug": False,
16+
"timeout": env['acr']['clear']['timeout']
17+
},
18+
"noisy": {
19+
"host": env['acr']['noisy']['host'],
20+
"access_key": env['acr']['noisy']['access_key'],
21+
"access_secret": env['acr']['noisy']['access_secret'],
22+
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_AUDIO,
23+
"debug": False,
24+
"timeout": env['acr']['noisy']['timeout']
25+
},
26+
"hum": {
27+
"host": env['acr']['hum']['host'],
28+
"access_key": env['acr']['hum']['access_key'],
29+
"access_secret": env['acr']['hum']['access_secret'],
30+
"recognize_type": ACRCloudRecognizeType.ACR_OPT_REC_BOTH,
31+
"debug": False,
32+
"timeout": env['acr']['hum']['timeout']
33+
}
34+
}
35+
logger.info('Loaded: ACR Config')
4036

4137

4238
# Functions for sending files to the ACRCloud API and getting a response

SongID.py renamed to app/SongID.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,6 @@ def humProcess(update, context):
282282

283283

284284
logger.info('Loading Complete!')
285-
if heroku_enabled == 'True':
286-
logger.info('Initialising Heroku webhook...')
287-
PORT = int(os.environ.get('PORT', int(heroku_port)))
288-
u.start_webhook(listen=heroku_listen,
289-
port=int(PORT),
290-
url_path=token)
291-
u.bot.setWebhook(heroku_webhook + token)
292-
logger.info('Heroku webhook initialised')
293-
else:
294-
u.start_polling()
295-
logger.info('Standard polling initialised')
285+
u.start_polling()
286+
logger.info('Standard polling initialised')
296287
u.idle()

SongIDCore.py renamed to app/SongIDCore.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import sentry_sdk
44

55

6-
ver='0.2.4'
6+
ver='1.0.0-beta1'
77
botName=f'SongID'
88
botVer=f'{botName} {ver}'
99
botAt=f'@SongIDBot'
1010
botUsername='SongIDbot'
11-
downloadDIR='data/downloads'
11+
downloadDIR='downloads'
1212

1313

1414
# Initialise the logger and format it's output
@@ -21,18 +21,44 @@
2121
logger = logging.getLogger(__name__)
2222

2323

24-
# Load private information regarding the telegram bot
25-
with open('data/token.json', 'r') as f:
26-
all_tokens = json.load(f)
27-
telegramConfig = all_tokens["telegram"]
28-
token = telegramConfig["token"]
29-
devid = telegramConfig["devid"]
30-
devusername = telegramConfig["devusername"]
31-
heroku_enabled = all_tokens["heroku"]["enabled"]
32-
heroku_webhook = all_tokens["heroku"]["webhook"]
33-
heroku_listen = all_tokens["heroku"]["listen"]
34-
heroku_port = all_tokens["heroku"]["port"]
35-
sentry_dsn = all_tokens["sentry"]["dsn"]
24+
# Load environment variables
25+
env = {
26+
'sentry_dsn': os.getenv('SONGID_SENTRY_DSN'),
27+
'telegram': {
28+
'bot_token': os.getenv('SONGID_TELEGRAM_BOT_TOKEN'),
29+
'dev_id': os.getenv('SONGID_TELEGRAM_DEV_ID'),
30+
'dev_username': os.getenv('SONGID_TELEGRAM_DEV_USERNAME')
31+
},
32+
'acr': {
33+
'clear': {
34+
'host': os.getenv('SONGID_ACR_CLEAR_HOST'),
35+
'access_key': os.getenv('SONGID_ACR_CLEAR_ACCESS_KEY'),
36+
'access_secret': os.getenv('SONGID_ACR_CLEAR_ACCESS_SECRET'),
37+
'recognize_type': os.getenv('SONGID_ACR_CLEAR_RECOGNIZE_TYPE'),
38+
'timeout': os.getenv('SONGID_ACR_CLEAR_TIMEOUT')
39+
},
40+
'noisy': {
41+
'host': os.getenv('SONGID_ACR_NOISY_HOST'),
42+
'access_key': os.getenv('SONGID_ACR_NOISY_ACCESS_KEY'),
43+
'access_secret': os.getenv('SONGID_ACR_NOISY_ACCESS_SECRET'),
44+
'recognize_type': os.getenv('SONGID_ACR_NOISY_RECOGNIZE_TYPE'),
45+
'timeout': os.getenv('SONGID_ACR_NOISY_TIMEOUT')
46+
},
47+
'hum': {
48+
'host': os.getenv('SONGID_ACR_HUM_HOST'),
49+
'access_key': os.getenv('SONGID_ACR_HUM_ACCESS_KEY'),
50+
'access_secret': os.getenv('SONGID_ACR_HUM_ACCESS_SECRET'),
51+
'recognize_type': os.getenv('SONGID_ACR_HUM_RECOGNIZE_TYPE'),
52+
'timeout': os.getenv('SONGID_ACR_HUM_TIMEOUT')
53+
}
54+
}
55+
}
56+
57+
print(env)
58+
token = env['telegram']['bot_token']
59+
devid = env['telegram']['dev_id']
60+
devusername = env['telegram']['dev_username']
61+
sentry_dsn = env['sentry_dsn']
3662

3763
sentry_sdk.init(
3864
dsn=sentry_dsn,
@@ -93,4 +119,4 @@ def logbot(update, msg):
93119

94120

95121

96-
logger.info('Loaded: SongIDFramework')
122+
logger.info('Loaded: SongIDFramework')

SongIDProcessor.py renamed to app/SongIDProcessor.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,22 +218,9 @@ def addUserData(update, apiCalls, lastCall):
218218
# Get user data for the respective user
219219
def getUserData(update):
220220
#update = json.loads(update)
221-
logger.debug('getUserData(0/2)')
222-
223-
# May be completely unnecessary, just in case
224-
if hasattr(update, 'effective_chat'):
225-
# Get users Telegram ID
226-
userID=str(update.effective_chat.id)
227-
# Add user to userdata in case they did not initially send /start
228-
# to prevent AttributeErrors occurring from their key not being
229-
# present in the database
230-
# (https://github.com/smcclennon/SongID/issues/6#issuecomment-1021517621)
231-
if userID not in userdata:
232-
SIDProcessor.addUserData(update, '0', '0')
233-
logger.debug('getUserData(1/2)')
234-
221+
logger.debug('getUserData(0/1)')
235222
data = userdata[f'{update.effective_user.id}']
236-
logger.debug('getUserData(2/2)')
223+
logger.debug('getUserData(1/1)')
237224
return data
238225

239226

File renamed without changes.
File renamed without changes.

data/acrcloud.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)