Skip to content

Commit

Permalink
fix bug where bilibili links are not processed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaohong6 committed Oct 4, 2022
1 parent 9669ab3 commit 90b2332
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 12 additions & 6 deletions models/video.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import re
from datetime import datetime
from enum import Enum
from typing import Union, List
Expand Down Expand Up @@ -83,13 +84,18 @@ def get_nc_info(vid: str) -> Video:
return Video(VideoSite.NICO_NICO, vid, url, views, date, thumb)


def get_bv(vid: str) -> str:
search_bv = re.search("BV[0-9a-zA-Z]+", vid, re.IGNORECASE)
if search_bv is not None:
return search_bv.group(0)
search_av = re.search("av[0-9]+", vid, re.IGNORECASE)
if search_av is not None:
return av_to_bv(search_av.group(0))
return vid


def get_bb_info(vid: str) -> Video:
if vid.find("bilibili") != -1:
vid = vid[vid.rfind("/") + 1:]
if vid.find("?") != -1:
vid = vid[:vid.find("?")]
if "av" in vid:
vid = av_to_bv(vid)
vid = get_bv(vid)
url = f"https://api.bilibili.com/x/web-interface/view?bvid={vid}"
response = json.loads(http_get(url, use_proxy=False).text)
epoch_time = int(response['data']['pubdate'])
Expand Down
15 changes: 15 additions & 0 deletions tests/test_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from unittest import TestCase

from models.video import get_bv


class TestVide(TestCase):
def test_get_bv(self):
self.assertEqual("BV1Ex411w7d2",
get_bv("https://bilibili.com/video/BV1Ex411w7d2/?spm_id_from"))

self.assertEqual("BV1Ex411w7d2",
get_bv("https://bilibili.com/video/BV1Ex411w7d2"))

self.assertEqual("BV1Ex411w7d2",
get_bv("BV1Ex411w7d2"))

0 comments on commit 90b2332

Please sign in to comment.