Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
MikuInvidious
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bing Chilling
MikuInvidious
Commits
298b148c
Commit
298b148c
authored
1 year ago
by
John Xina
Browse files
Options
Downloads
Patches
Plain Diff
add automatic cookies refresh
parent
ed2bb7f7
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
config.toml.sample
+3
-0
3 additions, 0 deletions
config.toml.sample
refresher.py
+70
-0
70 additions, 0 deletions
refresher.py
shared.py
+18
-4
18 additions, 4 deletions
shared.py
with
91 additions
and
4 deletions
config.toml.sample
+
3
−
0
View file @
298b148c
...
...
@@ -37,6 +37,9 @@ bili_jct = ''
buvid3 = ''
dedeuserid = ''
# Refresh token, can be obtained from the main page by examing `window.localStorage.ac_time_value'.
ac_time_value = ''
[proxy]
# If your server is located in China, videos and audo-only videos may likely break if not using proxy.
video = true
...
...
This diff is collapsed.
Click to expand it.
refresher.py
0 → 100644
+
70
−
0
View file @
298b148c
# Copyright (C) 2023 MikuInvidious Team
#
# MikuInvidious is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
# MikuInvidious is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MikuInvidious. If not, see <http://www.gnu.org/licenses/>.
import
toml
from
bilibili_api
import
sync
,
Credential
def
discard_generated_data
(
fn
):
with
open
(
fn
,
'
r
'
)
as
file
:
lines
=
file
.
readlines
()
index
=
None
for
i
,
line
in
enumerate
(
lines
):
if
'
## GENERATED DATA, DO NOT WRITE ANYTHING BELOW!!! ##
\n
'
in
line
:
index
=
i
break
if
index
is
not
None
:
with
open
(
fn
,
'
w
'
)
as
file
:
file
.
writelines
(
lines
[:
index
])
return
True
else
:
return
False
def
renew_cookies
(
cred
):
if
sync
(
cred
.
check_refresh
()):
sync
(
cred
.
refresh
())
write_cookies
(
cred
)
print
(
'
Cookies refreshed.
'
)
return
True
return
False
def
write_cookies
(
cred
):
buf
=
''
if
discard_generated_data
(
'
config.toml
'
)
else
'
\n\n
'
buf
+=
'
## GENERATED DATA, DO NOT WRITE ANYTHING BELOW!!! ##
\n
'
buf
+=
'
[updatedcred]
\n
'
for
k
,
v
in
cred
.
get_cookies
().
items
():
buf
+=
f
'
{
k
.
lower
()
}
=
\'
{
v
}
\'\n
'
with
open
(
'
config.toml
'
,
'
a
'
)
as
f
:
f
.
write
(
buf
)
if
__name__
==
'
__main__
'
:
print
(
'
Trying to refresh the cookies...
'
)
appconf
=
toml
.
load
(
'
config.toml
'
)
credstore
=
appconf
[
'
updatedcred
'
]
if
'
updatedcred
'
in
appconf
else
\
appconf
[
'
credential
'
]
appcred
=
Credential
(
sessdata
=
credstore
[
'
sessdata
'
],
bili_jct
=
credstore
[
'
bili_jct
'
],
buvid3
=
credstore
[
'
buvid3
'
],
dedeuserid
=
credstore
[
'
dedeuserid
'
],
ac_time_value
=
credstore
[
'
ac_time_value
'
])
if
renew_cookies
(
appcred
):
print
(
'
Successfully refreshed the cookies.
'
)
else
:
print
(
'
Cookies are already up-to-date.
'
)
This diff is collapsed.
Click to expand it.
shared.py
+
18
−
4
View file @
298b148c
...
...
@@ -19,6 +19,8 @@ from aioflask import request, render_template, Flask
from
flask_caching
import
Cache
from
bilibili_api
import
Credential
from
refresher
import
renew_cookies
try
:
appconf
=
toml
.
load
(
'
config.toml
'
)
except
FileNotFoundError
:
...
...
@@ -36,10 +38,22 @@ appcache = Cache(app, config={'CACHE_TYPE': 'RedisCache'})
# Initilize credentials for bilibili API.
if
appconf
[
'
credential
'
][
'
use_cred
'
]:
appcred
=
Credential
(
sessdata
=
appconf
[
'
credential
'
][
'
sessdata
'
],
bili_jct
=
appconf
[
'
credential
'
][
'
bili_jct
'
],
buvid3
=
appconf
[
'
credential
'
][
'
buvid3
'
],
dedeuserid
=
appconf
[
'
credential
'
][
'
dedeuserid
'
])
credstore
=
appconf
[
'
updatedcred
'
]
if
'
updatedcred
'
in
appconf
else
\
appconf
[
'
credential
'
]
appcred
=
Credential
(
sessdata
=
credstore
[
'
sessdata
'
],
bili_jct
=
credstore
[
'
bili_jct
'
],
buvid3
=
credstore
[
'
buvid3
'
],
dedeuserid
=
credstore
[
'
dedeuserid
'
],
ac_time_value
=
credstore
[
'
ac_time_value
'
])
if
renew_cookies
(
appcred
):
appconf
=
toml
.
load
(
'
config.toml
'
)
credstore
=
appconf
[
'
updatedcred
'
]
appcred
=
Credential
(
sessdata
=
credstore
[
'
sessdata
'
],
bili_jct
=
credstore
[
'
bili_jct
'
],
buvid3
=
credstore
[
'
buvid3
'
],
dedeuserid
=
credstore
[
'
dedeuserid
'
],
ac_time_value
=
credstore
[
'
ac_time_value
'
])
else
:
appcred
=
None
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment