Added way to check for youtube cookies status
This commit is contained in:
parent
b86f13f2d8
commit
c20fdca09b
4 changed files with 87 additions and 1 deletions
|
|
@ -706,3 +706,55 @@ class HttpAPI(common):
|
|||
)
|
||||
except Exception as e:
|
||||
return web.json_response(data={"error": str(e)}, status=web.HTTPInternalServerError.status_code)
|
||||
|
||||
@route("GET", "api/youtube/auth")
|
||||
async def is_authenticated(self, request: Request) -> Response:
|
||||
cookie_file = self.config.ytdl_options.get("cookiefile", None)
|
||||
if not cookie_file:
|
||||
return web.json_response(data={"message": "No cookie file provided."}, status=web.HTTPForbidden.status_code)
|
||||
|
||||
cookie_file = os.path.realpath(cookie_file)
|
||||
if not os.path.exists(cookie_file):
|
||||
return web.json_response(
|
||||
data={"message": f"Cookie file '{cookie_file}' does not exist."}, status=web.HTTPNotFound.status_code
|
||||
)
|
||||
|
||||
try:
|
||||
import http.cookiejar
|
||||
|
||||
cookies = http.cookiejar.MozillaCookieJar(cookie_file, None, None)
|
||||
cookies.load()
|
||||
except Exception as e:
|
||||
LOG.error(f"failed to load cookies from '{cookie_file}'. '{e}'.")
|
||||
LOG.exception(e)
|
||||
return web.json_response(
|
||||
data={"message": "Failed to load cookies"},
|
||||
status=web.HTTPInternalServerError.status_code,
|
||||
)
|
||||
|
||||
url = "https://www.youtube.com/account"
|
||||
|
||||
try:
|
||||
opts = {
|
||||
"proxy": self.config.ytdl_options.get("proxy", None),
|
||||
"headers": {
|
||||
"User-Agent": self.config.ytdl_options.get(
|
||||
"user_agent", request.headers.get("User-Agent", f"YTPTube/{self.config.version}")
|
||||
),
|
||||
},
|
||||
"cookies": cookies,
|
||||
}
|
||||
async with httpx.AsyncClient(**opts) as client:
|
||||
LOG.debug(f"Checking '{url}' redirection.")
|
||||
response = await client.request(method="GET", url=url, follow_redirects=False)
|
||||
return web.json_response(
|
||||
data={"message": "Authenticated." if response.status_code == 200 else "Not authenticated."},
|
||||
status=200 if response.status_code == 200 else 401,
|
||||
)
|
||||
except Exception as e:
|
||||
LOG.error(f"Failed to request '{url}'. '{e}'.")
|
||||
LOG.exception(e)
|
||||
return web.json_response(
|
||||
data={"message": f"Failed to request website. {str(e)}"},
|
||||
status=web.HTTPInternalServerError.status_code,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -310,4 +310,8 @@ class Config:
|
|||
dict: A dictionary with the frontend configuration
|
||||
"""
|
||||
|
||||
return {k: getattr(self, k) for k in self._frontend_vars}
|
||||
data = {k: getattr(self, k) for k in self._frontend_vars}
|
||||
hasCookies = self.ytdl_options.get("cookiefile", None)
|
||||
data["has_cookies"] = hasCookies is not None and os.path.exists(hasCookies)
|
||||
|
||||
return data
|
||||
|
|
|
|||
|
|
@ -12,6 +12,13 @@
|
|||
</div>
|
||||
<div class="navbar-end is-flex">
|
||||
|
||||
<div class="navbar-item" v-if="socket.isConnected && config.app.has_cookies">
|
||||
<button class="button is-dark" @click="checkCookies" v-tooltip="'Check youtube cookies status.'"
|
||||
:disabled="isChecking">
|
||||
<span class="icon has-text-info"><i class="fas fa-cookie"></i></span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="navbar-item" v-if="socket.isConnected">
|
||||
<button class="button is-dark" @click="pauseDownload" v-if="false === config.paused"
|
||||
v-tooltip="'Pause non-active downloads.'">
|
||||
|
|
@ -95,6 +102,8 @@ const Year = new Date().getFullYear()
|
|||
const selectedTheme = useStorage('theme', (() => window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')())
|
||||
const socket = useSocketStore()
|
||||
const config = useConfigStore()
|
||||
const isChecking = ref(false)
|
||||
const toast = useToast()
|
||||
|
||||
const applyPreferredColorScheme = scheme => {
|
||||
for (let s = 0; s < document.styleSheets.length; s++) {
|
||||
|
|
@ -137,6 +146,26 @@ const applyPreferredColorScheme = scheme => {
|
|||
}
|
||||
}
|
||||
|
||||
const checkCookies = async () => {
|
||||
if (true === isChecking.value) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
isChecking.value = true
|
||||
const response = await fetch(config.app.url_host + config.app.url_prefix + 'api/youtube/auth')
|
||||
const data = await response.json()
|
||||
if (response.ok) {
|
||||
toast.success('Succuss. ' + data.message)
|
||||
} else {
|
||||
toast.error('Failed' + data.error)
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error('Failed to check cookies state. ' + e.message)
|
||||
} finally {
|
||||
isChecking.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
applyPreferredColorScheme(selectedTheme.value)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ const CONFIG_KEYS = {
|
|||
version: '',
|
||||
url_host: '',
|
||||
url_prefix: '',
|
||||
has_cookies: false,
|
||||
},
|
||||
presets: [
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue