-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
138 lines (113 loc) · 4.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from fastapi import FastAPI, Request
from pydantic import BaseModel
from oauth2client.client import OAuth2WebServerFlow
from fastapi.responses import RedirectResponse
from googleapiclient.discovery import build
import json
import httplib2
from dotenv import load_dotenv
import os
from backend.search import create_newplaylist, channel_playlist_ID, get_video_id_all_playlist
from starlette.middleware.cors import CORSMiddleware # 追加
app = FastAPI()
# エンドポイントを環境変数から読み込む
load_dotenv('.env.dev')
FORNTEND_ENDPOINT = os.getenv('VUE_FRONTEND_URL')
BACKEND_ENDPOINT = os.getenv('VITE_APP_BACKEND_URL')
# CORSを回避するために追加(今回の肝)
app.add_middleware(
CORSMiddleware,
allow_origins=[FORNTEND_ENDPOINT],
allow_credentials=True, # 追記により追加
allow_methods=["*"], # 追記により追加
allow_headers=["*"] # 追記により追加
)
#playlistの通信
class PlaylistID(BaseModel):
playlistID: str
# クライアント情報を読み込む
CLIENT_SECRETS_FILE = "./secret/youtube_secret.json"
with open(CLIENT_SECRETS_FILE, 'r') as json_file:
SECRETS_DATA = json.load(json_file)
# OAuth2.0情報を読み込む
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
REDIRECT_URI = BACKEND_ENDPOINT + "/api/callback"
# flowオブジェクトを記載
flow = OAuth2WebServerFlow(
client_id=SECRETS_DATA["installed"]["client_id"],
client_secret=SECRETS_DATA["installed"]["client_secret"],
scope=YOUTUBE_READ_WRITE_SCOPE,
redirect_uri=REDIRECT_URI
)
class PlaylistID(BaseModel):
playlistID: str
# プレイリストIDから曲のリストをゲットする
@app.post("/api/playlistid")
async def get_playlistid(data: PlaylistID):
return get_video_id_all_playlist(data.playlistID)
# グーグル認証画面を作る
@app.get("/api/googleauth")
async def access_googleauth():
auth_uri = flow.step1_get_authorize_url()
return auth_uri
# 認証して、対応するチャンネルIDをデータベースに新規登録する
@app.get("/api/callback")
async def handle_auth_callback(request: Request, code: str, scope: str):
global youtube
# 認証コードを取得
credentials = flow.step2_exchange(code)
# YouTube Data APIクライアントを作成する
youtube = build(YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http())
)
# トークンを取得
access_token = credentials.access_token
refresh_token = credentials.refresh_token
# チャンネル情報を取得
youtube.channels().list(part="snippet", mine=True).execute()
response = youtube.channels().list(part="snippet", mine=True).execute()
# YouTubeアカウントが登録されていない
if response['pageInfo']['totalResults'] < 1:
return RedirectResponse(url=FORNTEND_ENDPOINT + "/login?error=YouTubeチャンネルを作成してません")
# チャンネルIDとチャンネル名・アイコンURLを取得
channel_id = response["items"][0]["id"]
channel_name = response["items"][0]["snippet"]["title"]
icon_url = response["items"][0]["snippet"]["thumbnails"]["high"]["url"]
# token.pickleファイルを生成する
import pickle
with open('./token.pickle', 'wb') as token_file:
pickle.dump(credentials, token_file)
# プレイリストを作成する
playlistID = {
'happy': None,
'sad': None,
'chill': None,
'fight': None,
'like': None,
'etc': None
}
# プレイリストが既に存在するか確認する
playlist_data = channel_playlist_ID(channel_id)
print(playlist_data)
for key in playlistID.keys():
songle_title = 'Surbe専用 - ' + key
if songle_title in playlist_data.keys():
# 既にある時はそれを代入する
playlistID[key] = playlist_data[songle_title]
else:
# プレイリストが存在しない場合は新規作成する
playlistID[key] = create_newplaylist('Surbe専用 - ' + key)
# リダイレクト
query_params = {"id": channel_id, "name": channel_name, "icon": icon_url, "token": access_token}
query_params.update(playlistID)
redirect_url = FORNTEND_ENDPOINT + "/login?" + "&".join([f"{key}={value}" for key, value in query_params.items()])
return RedirectResponse(url=redirect_url)
# 動画タイトルを検索する
"""
@app.post("/api/youtube/search")
async def search(data: Search):
return emotion(search, emotion)
"""