Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Always-prog committed May 17, 2023
1 parent 83673c1 commit 8111782
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 36 deletions.
3 changes: 0 additions & 3 deletions command/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ def validate(self):
@abstractmethod
def run(self):
pass



3 changes: 1 addition & 2 deletions database/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from settings import SQLALCHEMY_DATABASE_URL

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from settings import SQLALCHEMY_DATABASE_URL

engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Expand Down
3 changes: 1 addition & 2 deletions database/tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Text, DateTime, Numeric
from sqlalchemy.orm import relationship

from . import Base, engine


Expand Down Expand Up @@ -145,5 +146,3 @@ class Token(Base):


Base.metadata.create_all(engine)


5 changes: 3 additions & 2 deletions mal/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from mal.types import TITLE_FIELDS
from os import environ

import requests

from mal.types import TITLE_FIELDS

DEFAULT_TITLE_FIELDS: TITLE_FIELDS = ['id', 'title', 'num_episodes', 'average_episode_duration', 'related_anime',
'start_date']

Expand Down Expand Up @@ -34,4 +36,3 @@ def get_anime_details(self, id: int, fields: TITLE_FIELDS = None):
fields = fields or DEFAULT_TITLE_FIELDS
endpoint = f'/v2/anime/{id}'
return self._client_request('get', endpoint, params={'fields': ','.join(fields)}).json()

2 changes: 1 addition & 1 deletion mal/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Literal, List, Dict
from typing import Literal, List

TITLE_FIELD = Literal[
'id', 'title', 'main_picture', 'start_date', 'end_date', 'synopsis',
Expand Down
5 changes: 3 additions & 2 deletions mal/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Tuple

from mal.client import MALClient
from mal.types import TITLE_ID, FRANCHISE_NAME, MAL_TITLE_ID, TITLE_NAME, FRANCHISE_CODE, MAL_TITLE_ID
from mal.types import TITLE_ID, FRANCHISE_NAME, FRANCHISE_CODE, MAL_TITLE_ID
from shikimori.functions import get_anime_by_id


def get_id(anime: dict):
return anime.get('id', None) or anime['node']['id']

Expand All @@ -28,4 +29,4 @@ def get_franchise_from_title(id: MAL_TITLE_ID) -> Tuple[FRANCHISE_NAME or None,
code = anime['franchise']
if code is None:
return None, None
return ''.join(x.capitalize()+' ' or '_' for x in code.split('_'))[:-1], code
return ''.join(x.capitalize() + ' ' or '_' for x in code.split('_'))[:-1], code
9 changes: 3 additions & 6 deletions schemas/episodes/commands.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from sqlalchemy import desc

from database.tables import Episode, Season
from sqlalchemy.orm import Session

from database.tables import Episode, Season
from schemas.episodes.dumps import dump_database, write_episodes_to_txt
from settings import EPISODES_TXT_FILE_PATH, DUMP_SCRIPT_PATH
from .exceptions import EpisodeAlreadyExists
from .utils import generate_episode_name
from .exceptions import EpisodeAlreadyExists, EpisodeNotFound
from schemas.episodes.dumps import dump_database, write_episodes_to_txt
from schemas.seasons.exceptions import SeasonNotFound


def find_season_episode_by_order(db: Session, season_id, episode_order):
Expand Down
8 changes: 5 additions & 3 deletions schemas/episodes/dumps.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import subprocess
from threading import Thread

from sqlalchemy import func
from sqlalchemy.orm import Session

from database.tables import Season, Episode
from threading import Thread
from os import system


def dump_all(db: Session, script: str, filepath: str):
def execute(db: Session, script: str, filepath: str):
write_episodes_to_txt(db, filepath)
dump_database(script)

thread = Thread(target=execute, args=(db, script, filepath,))
thread.start()

Expand Down Expand Up @@ -40,7 +41,8 @@ def new_title(title_name):
file_content.append(f'\n\n\n - [{title_name}]\n')

last_title = None
for season in db.query(Season).join(Episode).group_by(Season.season_name).order_by(func.min(Episode.watched_datetime)):
for season in db.query(Season).join(Episode).group_by(Season.season_name).order_by(
func.min(Episode.watched_datetime)):
season.episodes.sort(key=lambda ep: ep.episode_order)
if last_title != season.title_name:
new_title(season.title_name)
Expand Down
4 changes: 1 addition & 3 deletions schemas/episodes/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@


def generate_episode_name(title_name, season_name, episode_order):
return f'{title_name}-{season_name} - ({episode_order})'
return f'{title_name}-{season_name} - ({episode_order})'
3 changes: 2 additions & 1 deletion schemas/records/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def create_record_from_source(db: Session, user: User,
if not episode_db:
raise EpisodeNotFound(
f'Episode not found: {season_db.title.title_name} -> {season_db.title.title_name} -> {episode_order}')
return create_record(db, user, episode_db, watch_datetime, watched_from, watched_time, translate_type, comment, site)
return create_record(db, user, episode_db, watch_datetime, watched_from, watched_time, translate_type, comment,
site)


def create_record(db: Session,
Expand Down
2 changes: 0 additions & 2 deletions schemas/seasons/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


class SeasonAlreadyExists(Exception):
def __init__(self, message):
super().__init__(message)
Expand Down
3 changes: 1 addition & 2 deletions schemas/titles/commands.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from sqlalchemy.orm import Session

from database.tables import Title
from mal.types import FRANCHISE_CODE, TITLE_NAME
from mal.utils import get_franchise_from_title
from mal.types import TITLE_NAME
from mal.utils import get_title_by_id
from shikimori.functions import get_source_title
from .exceptions import TitleNotFound
Expand Down
5 changes: 3 additions & 2 deletions schemas/users/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
from werkzeug.security import generate_password_hash, check_password_hash

from database.tables import User
from superset.users import create_user_in_superset, get_user_from_superset
from schemas.users.exceptions import UserAlreadyExists
from superset.users import create_user_in_superset, get_user_from_superset


def register_new_user(db: Session, username: str, email: str, password: str, first_name: str or None,
last_name: str or None):
if db.query(User).filter(or_(User.username==username, User.email==email)).first() or get_user_from_superset(username):
if db.query(User).filter(or_(User.username == username, User.email == email)).first() or get_user_from_superset(
username):
raise UserAlreadyExists(f'User with username {username} or email {email} already exists')
gen_first_name, gen_last_name = generate_user_name()
first_name, last_name = gen_first_name if not first_name else first_name, \
Expand Down
3 changes: 2 additions & 1 deletion shikimori/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from os import environ
from os.path import exists

import requests

from mal.types import MAL_TITLE_ID
Expand Down Expand Up @@ -76,7 +77,7 @@ def refresh_tokens(self):
headers = {
"User-Agent": 'timeEater'
}
response = requests.post(self._endpoint('/oauth/token'),headers=headers, files=files)
response = requests.post(self._endpoint('/oauth/token'), headers=headers, files=files)
if response.status_code != 200:
raise Exception('Something is going wrong with refreshing token %s' % response.text)

Expand Down
2 changes: 1 addition & 1 deletion shikimori/functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Animes at Shikimori has the same MAL ID.
"""
from mal.types import MAL_TITLE_ID, MAL_TITLE_ID
from mal.types import MAL_TITLE_ID
from shikimori.client import ShikimoriClient


Expand Down
4 changes: 2 additions & 2 deletions shikimori/tokens.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"access_token": "noop",
"refresh_token": "noop"
"access_token": "noop",
"refresh_token": "noop"
}
2 changes: 1 addition & 1 deletion superset/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def get_user_from_superset(username: str):
f"""
SELECT * from ab_user WHERE username='{username}';
"""
).all()
).all()

0 comments on commit 8111782

Please sign in to comment.