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

Fix bugs #22

Open
wants to merge 10 commits 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
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
jobs:
build-and-test:

runs-on: ubuntu-latest
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 3 additions & 5 deletions geniust/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@

# Enable logging
LOG_LEVEL_NUM: int = getattr(logging, LOG_LEVEL)
logging.getLogger("telegram").setLevel(
logging.INFO if LOG_LEVEL_NUM <= logging.INFO else LOG_LEVEL
)
logging.getLogger("telegram").setLevel(LOG_LEVEL)
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger("geniust")
logger.setLevel(LOG_LEVEL)
Expand Down Expand Up @@ -136,7 +134,7 @@ def main_menu(update: Update, context: CallbackContext) -> int:
text = context.bot_data["texts"][language]["main_menu"]

message = update.message if update.message else update.callback_query.message
is_chat_group = "group" in message
is_chat_group = hasattr(message, "group")
reply_to_message_id = message.message_id if is_chat_group else None

logger.debug(
Expand Down Expand Up @@ -472,7 +470,7 @@ def main():

dp = updater.dispatcher
dp.bot_data["texts"]: Dict[Any, str] = texts
database = Database(DATABASE_URL.replace("postgres", "postgresql+psycopg2"))
database = Database(DATABASE_URL.replace("postgres", "postgresql"))
dp.bot_data["db"]: Database = database
dp.bot_data["genius"]: GeniusT = GeniusT()
dp.bot_data["lyricsgenius"]: lg.Genius = lg.Genius(
Expand Down
22 changes: 18 additions & 4 deletions geniust/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import threading
import os

import tekore as tk
import tornado.ioloop
Expand All @@ -15,7 +16,13 @@
from geniust.utils import log


class CronHandler(RequestHandler):
class BaseHandler(RequestHandler):
def prepare(self):
if self.request.protocol == "http":
self.redirect("https://" + self.request.host, permanent=False)


class CronHandler(BaseHandler):
"""Handles cron-job requests"""

def get(self):
Expand All @@ -27,7 +34,7 @@ def head(self):
self.finish()


class TokenHandler(RequestHandler):
class TokenHandler(BaseHandler):
"""Handles redirected URLs from Genius

This class will handle the URLs that Genius
Expand Down Expand Up @@ -128,7 +135,7 @@ def get(self):
self.bot.send_message(chat_id, text)


class PreferencesHandler(RequestHandler): # pragma: no cover
class PreferencesHandler(BaseHandler): # pragma: no cover
def initialize(self, auths, recommender) -> None:
self.auths = auths
self.recommender = recommender
Expand Down Expand Up @@ -180,6 +187,9 @@ def __init__(
self, bot_token, server_port, auths, database, texts, username, dispatcher
):
super().__init__()
parent_dir = os.path.abspath(
os.path.join(os.path.realpath(__file__), os.pardir)
)
app = tornado.web.Application(
[
url(r"/get", CronHandler),
Expand All @@ -202,8 +212,12 @@ def __init__(
),
]
)
ssl_options = {
"certfile": os.path.join(parent_dir, "fullchain.pem"),
"keyfile": os.path.join(parent_dir, "privkey.pem"),
}
# noinspection PyTypeChecker
self.webhooks = WebhookServer("0.0.0.0", server_port, app, None)
self.webhooks = WebhookServer("0.0.0.0", server_port, app, None, ssl_options)

def run(self):
"""start web hook server"""
Expand Down
21 changes: 21 additions & 0 deletions scripts/db_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# install postgres
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql


# set up database
sudo -i -u postgres

psql
\password
CREATE TABLE user_data (
chat_id BIGINT UNIQUE,
include_annotations BOOLEAN DEFAULT True,
lyrics_lang TEXT DEFAULT 'English + Non-English',
bot_lang TEXT DEFAULT 'en',
genius_token TEXT DEFAULT NULL,
spotify_token TEXT DEFAULT NULL
)

27 changes: 27 additions & 0 deletions scripts/py_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# install git and clone repo
sudo apt-get update
sudo apt-get install git
cd ~
git clone https://github.com/allerter/geniust.git

# install python and its requirements
PYTHON_VERSION=$(cat geniust/runtime.txt | sed 's/-//')
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get -y install $PYTHON_VERSION python3-pip python3-dev libpq-dev
pip install virtualenv
virtualenv -p 3.8 geniust_env
source geniust_env/bin/activate
cd geniust
pip install -r requirements.txt
pip install -e .

# get ssl certificates
apt install snapd
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot certonly --standalone
cp /etc/letsencrypt/live/*name*/fullchain.pem ~/geniust/
cp /etc/letsencrypt/live/*name*/privkey.pem ~/geniust/
27 changes: 27 additions & 0 deletions scripts/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export PYTHONUNBUFFERED=
export PYTHONIOENCODING=
export BOT_TOKEN=
export TELEGRAPH_TOKEN=
export GENIUS_TOKEN=
export ANNOTATIONS_CHANNEL_HANDLE=
export DEVELOPERS=
export SERVER_ADDRESS=
export TELETHON_API_ID=
export TELETHON_API_HASH=
export TELETHON_SESSION_STRING=
export DATABASE_URL=
export GENIUS_CLIENT_ID=
export GENIUS_REDIRECT_URI=
export GENIUS_CLIENT_SECRET=
export SERVER_PORT=
export SPOTIFY_CLIENT_ID=
export SPOTIFY_REDIRECT_URI=
export SPOTIFY_CLIENT_SECRET=
export LASTFM_API_KEY=
export RECOMMENDER_TOKEN=
export LYRIC_CARD_CHANNEL=
export IMGBB_TOKEN=
export BTC_ADDRESS=

source geniust_env/bin/activate
python geniust/bot.py
Loading