Skip to content

Commit

Permalink
Refactor: [Server] サーバーの起動に失敗する致命的な不具合を修正
Browse files Browse the repository at this point in the history
前回コミットでのリグレッションの修正と、LiveStream.py の app.streams 配下への移動
  • Loading branch information
tsukumijima committed Jul 3, 2023
1 parent 3f8664a commit cdb2ae8
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion server/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from app.config import LoadConfig
from app.constants import CLIENT_DIR, DATABASE_CONFIG, QUALITY, VERSION
from app.models.Channel import Channel
from app.models.LiveStream import LiveStream
from app.models.Program import Program
from app.models.TwitterAccount import TwitterAccount
from app.routers import CapturesRouter
Expand All @@ -30,6 +29,7 @@
from app.routers import UsersRouter
from app.routers import VersionRouter
from app.routers import VideosRouter
from app.streams.LiveStream import LiveStream
from app.utils import Interlaced
from app.utils import Logging
from app.utils.EDCB import EDCBTuner
Expand Down
4 changes: 3 additions & 1 deletion server/app/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import pkgutil
import secrets
import sys
from pathlib import Path
Expand Down Expand Up @@ -60,14 +61,15 @@
}

# データベース (Tortoise ORM) の設定
model_list = [name for _, name, _ in pkgutil.iter_modules(path=['app/models'])]
DATABASE_CONFIG = {
'timezone': 'Asia/Tokyo',
'connections': {
'default': f'sqlite://{str(DATA_DIR / "database.sqlite")}',
},
'apps': {
'models': {
'models': ['app.models', 'aerich.models'],
'models': [f'app.models.{name}' for name in model_list] + ['aerich.models'],
'default_connection': 'default',
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/app/models/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def is_display(self) -> bool:
def viewer_count(self) -> int:
# 現在の視聴者数を取得
## 循環参照を防ぐためにここで遅延インポート
from app.models.LiveStream import LiveStream
from app.streams.LiveStream import LiveStream
return LiveStream.getViewerCount(self.display_channel_id)


Expand Down
14 changes: 10 additions & 4 deletions server/app/models/RecordedProgram.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

import json
from datetime import datetime
from tortoise import fields
from tortoise import models
from typing import TYPE_CHECKING

from app.models.Channel import Channel
from app.models.RecordedVideo import RecordedVideo
from app.models.Series import Series
from app.models.SeriesBroadcastPeriod import SeriesBroadcastPeriod
if TYPE_CHECKING:
from app.models.Channel import Channel
from app.models.RecordedVideo import RecordedVideo
from app.models.Series import Series
from app.models.SeriesBroadcastPeriod import SeriesBroadcastPeriod


class RecordedProgram(models.Model):
Expand Down
8 changes: 7 additions & 1 deletion server/app/models/Series.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

import json
from tortoise import fields
from tortoise import models
from typing import TYPE_CHECKING

from app.models.SeriesBroadcastPeriod import SeriesBroadcastPeriod
if TYPE_CHECKING:
from app.models.SeriesBroadcastPeriod import SeriesBroadcastPeriod


class Series(models.Model):
Expand Down
12 changes: 9 additions & 3 deletions server/app/models/SeriesBroadcastPeriod.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

from tortoise import fields
from tortoise import models
from typing import TYPE_CHECKING

from app.models.Channel import Channel
from app.models.Series import Series
from app.models.RecordedProgram import RecordedProgram
if TYPE_CHECKING:
from app.models.Channel import Channel
from app.models.Series import Series
from app.models.RecordedProgram import RecordedProgram


class SeriesBroadcastPeriod(models.Model):
Expand Down
10 changes: 8 additions & 2 deletions server/app/models/TwitterAccount.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

import asyncio
import json
import tweepy
from requests.cookies import RequestsCookieJar
from tortoise import fields
from tortoise import models
from tweepy_authlib import CookieSessionUserHandler
from typing import TYPE_CHECKING

from app.app import consumer_key, consumer_secret
from app.models.User import User
if TYPE_CHECKING:
from app.models.User import User


class TwitterAccount(models.Model):
Expand Down Expand Up @@ -92,6 +97,7 @@ def getTweepyAuthHandler(self) -> tweepy.OAuth1UserHandler | CookieSessionUserHa

# 通常の OAuth 認証の場合
else:
from app.app import consumer_key, consumer_secret
auth_handler = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, self.access_token, self.access_token_secret,
)
Expand Down
9 changes: 7 additions & 2 deletions server/app/models/User.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

import json
from tortoise import fields
from tortoise import models
from typing import Any
from typing import Any, TYPE_CHECKING

from app.models.TwitterAccount import TwitterAccount
if TYPE_CHECKING:
from app.models.TwitterAccount import TwitterAccount


class User(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion server/app/routers/ChannelsRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from app.config import Config
from app.constants import API_REQUEST_HEADERS, LOGO_DIR, VERSION
from app.models.Channel import Channel
from app.models.LiveStream import LiveStream
from app.routers.UsersRouter import GetCurrentUser
from app.streams.LiveStream import LiveStream
from app.utils import Logging
from app.utils.EDCB import CtrlCmdUtil
from app.utils.EDCB import EDCBUtil
Expand Down
6 changes: 3 additions & 3 deletions server/app/routers/LiveStreamsRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from app import schemas
from app.constants import QUALITY, QUALITY_TYPES
from app.models.Channel import Channel
from app.models.LiveStream import LiveStream
from app.models.LiveStream import LiveStreamClient
from app.models.LiveStream import LiveStreamStatus
from app.streams.LiveStream import LiveStream
from app.streams.LiveStream import LiveStreamClient
from app.streams.LiveStream import LiveStreamStatus
from app.utils import Logging


Expand Down
3 changes: 2 additions & 1 deletion server/app/routers/TwitterRouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from typing import Any, cast, Coroutine

from app import schemas
from app.app import consumer_key, consumer_secret
from app.models.TwitterAccount import TwitterAccount
from app.models.User import User
from app.routers.UsersRouter import GetCurrentUser
Expand Down Expand Up @@ -205,6 +204,7 @@ async def TwitterAuthURLAPI(
## oauth/authorize と異なり、すでにアプリ連携している場合は再承認することなくコールバック URL にリダイレクトされる
## ref: https://developer.twitter.com/ja/docs/authentication/api-reference/authenticate
try:
from app.app import consumer_key, consumer_secret
oauth_handler = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, callback=callback_url)
authorization_url = await asyncio.to_thread(oauth_handler.get_authorization_url, signin_with_twitter=False) # 同期関数なのでスレッド上で実行
except tweepy.TweepyException:
Expand Down Expand Up @@ -291,6 +291,7 @@ async def TwitterAuthCallbackAPI(

# OAuth1UserHandler を初期化
## ref: https://docs.tweepy.org/en/latest/authentication.html#legged-oauth
from app.app import consumer_key, consumer_secret
oauth_handler = tweepy.OAuth1UserHandler(consumer_key, consumer_secret)
oauth_handler.request_token = {
'oauth_token': twitter_account.access_token,
Expand Down
10 changes: 8 additions & 2 deletions server/app/streams/LiveEncodingTask.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@

# Type Hints を指定できるように
# ref: https://stackoverflow.com/a/33533514/17124142
from __future__ import annotations

import aiofiles
import aiohttp
import asyncio
import os
import re
import time
from aiofiles.threadpool.text import AsyncTextIOWrapper
from typing import AsyncIterator, cast, Literal
from typing import AsyncIterator, cast, Literal, TYPE_CHECKING

from app.config import Config
from app.constants import API_REQUEST_HEADERS, LIBRARY_PATH, LOGS_DIR, QUALITY, QUALITY_TYPES
from app.models.Channel import Channel
from app.models.LiveStream import LiveStream
from app.streams.LiveHLSSegmenter import LiveHLSSegmenter
from app.streams.LivePSIDataArchiver import LivePSIDataArchiver
from app.utils import Logging
from app.utils.EDCB import EDCBTuner, PipeStreamReader

if TYPE_CHECKING:
from app.streams.LiveStream import LiveStream


class LiveEncodingTask:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class LiveStreamStatus(TypedDict):
class LiveStreamClient():
""" ライブストリームのクライアントを表すクラス """


def __init__(self, livestream: LiveStream, client_type: Literal['mpegts', 'll-hls']) -> None:
"""
ライブストリーミングクライアントのインスタンスを初期化する
Expand Down

0 comments on commit cdb2ae8

Please sign in to comment.