This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: Storage of configs and data #2
Open
martvdm
wants to merge
16
commits into
main
Choose a base branch
from
refactors/storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
34c6fda
feat: config.yaml to gitignore
MartAtSiip e5ce0ca
feat: config json -> yaml
MartAtSiip eaf1d50
refactor: requirements.txt add yaml
MartAtSiip 5b22c46
feat: database conn function
MartAtSiip c6036bb
refactor: __api__.py to new config
MartAtSiip ca25d44
refactor: main.py to new config
MartAtSiip aafdced
refactor: api request language default english
MartAtSiip b4d0fcb
feat: add language to config
MartAtSiip 2eb482c
fix: config in request.py
MartAtSiip 9492685
refactor(docs): more clearly comments
MartAtSiip 7b504e7
refactor(docs): update README.md for new config
MartAtSiip bac5903
feat: Generate invite link on start
MartAtSiip 09e70ff
refactor: removed message database loaded
MartAtSiip 2cc080a
refactor: refresh interval of loop configurable
MartAtSiip 19a0920
refactor(docs): added link generator to README.md
MartAtSiip df5cd2c
refactor(docs): dollars to bash
MartAtSiip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,7 +11,7 @@ | |||||
|
||||||
**Clone the repository:** | ||||||
```bash | ||||||
git clone https://github.com/martvdm/TrainAPI.git | ||||||
$ git clone https://github.com/martvdm/TrainAPI.git | ||||||
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. Outdated !
Suggested change
|
||||||
``` | ||||||
|
||||||
**Install packages:** | ||||||
|
@@ -21,47 +21,75 @@ pip install -r requirements.txt | |||||
|
||||||
**Create a config file:** | ||||||
```bash | ||||||
cp config.example.json config.json | ||||||
$ cp config.example.yaml config.yaml | ||||||
``` | ||||||
|
||||||
**Fill in the config.json file:** | ||||||
|
||||||
```json | ||||||
{ | ||||||
"token": "BOT_TOKEN", <-- Replace with your discord-bot token | ||||||
"language": "en, nl", <-- Your language | ||||||
"api": { | ||||||
---> The NS-Primary key is needed to gain acces to the API | ||||||
---> make an account at "https://apiportal.ns.nl/signin?ReturnUrl=%2F" | ||||||
"NS-PRIMARY": "Ocp-Apim-Subscription-Key" <-- Replace with your NS-Primary key | ||||||
}, | ||||||
"app": { | ||||||
"author": "Mart", | ||||||
"invite": "https://discord.com/api/oauth2/authorize?client_id=CLIENT_ID&permissions=8&scope=bot%20applications.commands", <-- Modify the url with your client id | ||||||
"testing": { | ||||||
"server": "TEST-SERVER-ID", <-- Replace with your test server id (optional) | ||||||
"channel": "TEST-CHANNEL-ID" <-- Replace with your test channel id (optional) | ||||||
} | ||||||
}, | ||||||
"database": { | ||||||
"host": "localhost", <-- Replace with your database IP | ||||||
"port": 3036, <-- Replace with your database port | ||||||
"database": "database", <-- Replace with your database name | ||||||
"username": "root", <-- Replace with your database username | ||||||
"password": "" <-- Replace with your database password | ||||||
} | ||||||
} | ||||||
**Fill in the config.yaml file:** | ||||||
> **Please follow the instructions:** | ||||||
> | ||||||
> make an account at https://apiportal.ns.nl/signin?ReturnUrl=%2F (You need an account to gain acces to the api) | ||||||
```yaml | ||||||
app: | ||||||
name: trainAPI # Please fill in the name for your application | ||||||
language: en # Choose between (en & nl) | ||||||
version: 1.0.0 # PLEASE DONT CHANGE THIS | ||||||
maintenance-mode: false # (NOT IMPLEMENTED YET) | ||||||
storage: | ||||||
use-mysql: false # If false the application uses a local file system (JSON) | ||||||
# Customize your app with the following settings | ||||||
customization: | ||||||
theme-color: FFFFFF # This hexcolor-code will be used in embeds | ||||||
#---------------------------------------------------------------------------- | ||||||
# Discord bot settings | ||||||
#---------------------------------------------------------------------------- | ||||||
discord: | ||||||
token: # Your discord bot token | ||||||
maintainers: # Discord id's of maintainers | ||||||
- 287598871373283329 | ||||||
presence: | ||||||
show-last-station: false # If set to true: presence text will change when a user requests information about station. | ||||||
## ^^^ Please note that this will affect the performance | ||||||
default-message: "TrainAPI" | ||||||
default-type: PLAYING #PLAYING, LISTENING, WATCHING, STREAMING | ||||||
|
||||||
#------------------------------------------------------------------------------ | ||||||
# Database | ||||||
# ONLY SUPPORTS MYSQL | ||||||
#------------------------------------------------------------------------------ | ||||||
# Please note that "use-mysql" must be set to true in the "app" section above, else this section will be ignored. | ||||||
#------------------------------------------------------------------------------ | ||||||
database: | ||||||
host: localhost | ||||||
port: 3306 | ||||||
user: root | ||||||
password: root | ||||||
database: trainapi | ||||||
|
||||||
#------------------------------------------------------------------------------ | ||||||
# All api credentials | ||||||
#------------------------------------------------------------------------------ | ||||||
api: | ||||||
refresh-interval: 3 # In minutes | ||||||
# ^^^ 3 minutes IS RECOMMENDED, TO MUCH MAY AFFECT THE PERFORMANCE ALSO YOU CAN BE RATE LIMITED BY NS | ||||||
ns-primary-key: SECRETKEY #PRIMARY KEY | ||||||
# ^^^ This key can be found in the APIPORTAL account dashboard | ||||||
``` | ||||||
|
||||||
**Run the bot:** | ||||||
```bash | ||||||
python3 main.py | ||||||
$ python main.py | ||||||
``` | ||||||
|
||||||
**Invite the bot to your server** | ||||||
**Invite the bot to your server:** | ||||||
|
||||||
> replace the CLIENT_ID with the client id of your bot | ||||||
> | ||||||
> https://discordapp.com/api/oauth2/authorize?client_id=CLIENT_ID&permissions=8&scope=bot%20applications.commands | ||||||
> When the application starts up it will send a log in the console. | ||||||
> This log will contain the generated invite link. | ||||||
|
||||||
Example: | ||||||
```bash | ||||||
$ Config loaded | ||||||
$ Invite link for TrainAPI#5430: | ||||||
$ https://discordapp.com/api/oauth2/authorize?client_id=959101335008063558&permissions=544491302336&scope=applications.commands%20bot | ||||||
^^^ This link redirects to the invite page. | ||||||
``` | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#------------------------------------------------------------------------------ | ||
# AUTHOR: Mart van der Molen | ||
# PLEASE READ THE README.md FILE FOR INSTRUCTIONS | ||
# | ||
# GLOBAL SETTINGS: | ||
#------------------------------------------------------------------------------ | ||
app: | ||
name: trainAPI | ||
language: en # Choose between (en & nl) | ||
version: 1.0.0 # PLEASE DONT CHANGE THIS | ||
maintenance-mode: false | ||
storage: | ||
use-mysql: false # If false the application uses a local file system (JSON) | ||
# Customize your app with the following settings | ||
customization: | ||
theme-color: FFFFFF | ||
#---------------------------------------------------------------------------- | ||
# Discord bot settings | ||
#---------------------------------------------------------------------------- | ||
discord: | ||
token: # Your discord bot token | ||
maintainers: # Discord id's of maintainers | ||
- 287598871373283329 | ||
presence: | ||
show-last-station: false # If set to true: presence text will change when a user requests information about station. | ||
## ^^^ Please note that this will affect the performance | ||
default-message: "TrainAPI" | ||
default-type: PLAYING #PLAYING, LISTENING, WATCHING, STREAMING | ||
|
||
#------------------------------------------------------------------------------ | ||
# Database | ||
# ONLY SUPPORTS MYSQL | ||
#------------------------------------------------------------------------------ | ||
# Please note that "use-mysql" must be set to true in the "app" section above, else this section will be ignored. | ||
#------------------------------------------------------------------------------ | ||
database: | ||
host: localhost | ||
port: 3306 | ||
user: root | ||
password: root | ||
database: trainapi | ||
|
||
#------------------------------------------------------------------------------ | ||
# All api credentials | ||
#------------------------------------------------------------------------------ | ||
api: | ||
refresh-interval: 3 #In minutes | ||
# ^^^ 3 minutes IS RECOMMENDED, TO MUCH MAY AFFECT THE PERFORMANCE ALSO YOU CAN BE RATE LIMITED BY NS | ||
ns-primary-key: SECRETKEY #PRIMARY KEY | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import mysql.connector | ||
import os | ||
|
||
def connect(config): | ||
dbconfig = config['database'] | ||
conn = mysql.connector.connect(user=dbconfig['user'], password=dbconfig['password'], host=dbconfig['host'], port=dbconfig['port'], database=dbconfig['database']) | ||
return conn | ||
|
||
def create_tables(config): | ||
conn = mysql.connector.connect(user=config['database']['username'], password=config['database']['password'], host=config['database']['host'], port=config['database']['port'], database=config['database']['database']) | ||
conn = connect(config) | ||
import database.tables.trips as trips | ||
import database.tables.notifications as notifications | ||
import database.tables.disruptions as disruptions | ||
cursor = conn.cursor() | ||
disruptions.index(cursor) | ||
trips.index(cursor) | ||
notifications.index(cursor) | ||
conn.close() | ||
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
py.