admin commited on
Commit
186440e
·
1 Parent(s): da81c0e
Files changed (4) hide show
  1. bili.py +18 -12
  2. bvid2acid.py +1 -0
  3. tiktok.py +22 -17
  4. utils.py +17 -0
bili.py CHANGED
@@ -1,25 +1,30 @@
1
- import os
2
  import re
3
- import shutil
4
  import requests
5
  import gradio as gr
6
- from utils import timestamp
 
7
  from config import TMP_DIR, HEADER, TIMEOUT, API_BILI
8
 
9
 
10
  def download_file(url, video_id, cache_dir=TMP_DIR):
11
- if os.path.exists(cache_dir):
12
- shutil.rmtree(cache_dir)
13
-
14
- os.makedirs(cache_dir)
15
  local_file = f"{cache_dir}/{video_id}.mp4"
16
  response = requests.get(url, stream=True)
17
  if response.status_code == 200:
18
- with open(local_file, "wb") as file:
 
 
 
 
 
 
 
 
19
  for chunk in response.iter_content(chunk_size=8192):
20
- file.write(chunk)
 
 
21
 
22
- print(f"[{timestamp()}] File was downloaded to {local_file}")
23
  return local_file
24
 
25
 
@@ -46,14 +51,15 @@ def get_real_url(short_url):
46
  ).url.split("/?")[0]
47
 
48
 
 
49
  def infer(video_url: str, p: int):
50
  title = cover = desc = dur = video = author = avatar = None
51
  if not video_url:
52
  title = "Empty video link!"
53
  return title, cover, video, desc, dur, avatar, author
54
 
55
- video_url = extract_fst_url(video_url)
56
  try:
 
57
  if "b23.tv" in video_url:
58
  video_url = get_real_url(video_url)
59
 
@@ -75,7 +81,7 @@ def infer(video_url: str, p: int):
75
  avatar = author_data["user_img"]
76
 
77
  else:
78
- title = f"Failed to call API, error code: {retcode}"
79
 
80
  except Exception as e:
81
  title = f"Failed to parse video: {e}"
 
 
1
  import re
 
2
  import requests
3
  import gradio as gr
4
+ from tqdm import tqdm
5
+ from utils import timestamp, clean_dir
6
  from config import TMP_DIR, HEADER, TIMEOUT, API_BILI
7
 
8
 
9
  def download_file(url, video_id, cache_dir=TMP_DIR):
10
+ clean_dir(cache_dir)
 
 
 
11
  local_file = f"{cache_dir}/{video_id}.mp4"
12
  response = requests.get(url, stream=True)
13
  if response.status_code == 200:
14
+ total_size = int(response.headers.get("Content-Length", 0)) + 1
15
+ time_stamp = timestamp()
16
+ progress_bar = tqdm(
17
+ total=total_size,
18
+ unit="B",
19
+ unit_scale=True,
20
+ desc=f"[{time_stamp}] {local_file}",
21
+ )
22
+ with open(local_file, "wb") as f:
23
  for chunk in response.iter_content(chunk_size=8192):
24
+ if chunk:
25
+ f.write(chunk)
26
+ progress_bar.update(len(chunk))
27
 
 
28
  return local_file
29
 
30
 
 
51
  ).url.split("/?")[0]
52
 
53
 
54
+ # outer func
55
  def infer(video_url: str, p: int):
56
  title = cover = desc = dur = video = author = avatar = None
57
  if not video_url:
58
  title = "Empty video link!"
59
  return title, cover, video, desc, dur, avatar, author
60
 
 
61
  try:
62
+ video_url = extract_fst_url(video_url)
63
  if "b23.tv" in video_url:
64
  video_url = get_real_url(video_url)
65
 
 
81
  avatar = author_data["user_img"]
82
 
83
  else:
84
+ raise ConnectionError(f"Failed to call API, error code: {retcode}")
85
 
86
  except Exception as e:
87
  title = f"Failed to parse video: {e}"
bvid2acid.py CHANGED
@@ -2,6 +2,7 @@ import requests
2
  import gradio as gr
3
 
4
 
 
5
  def infer(bvid: str):
6
  try:
7
  response = requests.get(
 
2
  import gradio as gr
3
 
4
 
5
+ # outer func
6
  def infer(bvid: str):
7
  try:
8
  response = requests.get(
tiktok.py CHANGED
@@ -1,25 +1,30 @@
1
- import os
2
  import re
3
- import shutil
4
  import requests
5
  import gradio as gr
6
- from utils import timestamp
 
7
  from config import API_TIKTOK, TIMEOUT, TMP_DIR
8
 
9
 
10
- def download_file(url, video_id, cache_dir=TMP_DIR):
11
- if os.path.exists(cache_dir):
12
- shutil.rmtree(cache_dir)
13
-
14
- os.makedirs(cache_dir)
15
  local_file = f"{cache_dir}/{video_id}.mp4"
16
  response = requests.get(url, stream=True)
17
  if response.status_code == 200:
18
- with open(local_file, "wb") as file:
 
 
 
 
 
 
 
 
19
  for chunk in response.iter_content(chunk_size=8192):
20
- file.write(chunk)
 
 
21
 
22
- print(f"[{timestamp()}] File was downloaded to {local_file}")
23
  return local_file
24
 
25
 
@@ -32,18 +37,18 @@ def extract_fst_url(text):
32
  return None
33
 
34
 
 
35
  def infer(video_url):
36
  video = parse_time = desc = avatar = author = sign = None
37
  if not video_url:
38
  desc = "The video sharing link is empty!"
39
  return video, desc, parse_time, avatar, author, sign
40
 
41
- video_url = extract_fst_url(video_url)
42
- if not video_url:
43
- desc = "Please enter a valid video sharing link!"
44
- return video, desc, parse_time, avatar, author, sign
45
-
46
  try:
 
 
 
 
47
  response = requests.get(API_TIKTOK, params={"url": video_url}, timeout=TIMEOUT)
48
  response_json = response.json()
49
  retcode = response_json["code"]
@@ -60,7 +65,7 @@ def infer(video_url):
60
  sign = additional_data["signature"]
61
 
62
  else:
63
- desc = f"Interface call failed, error code: HTTP {retcode}"
64
 
65
  except Exception as e:
66
  desc = f"Video parsing failed: {e}"
 
 
1
  import re
 
2
  import requests
3
  import gradio as gr
4
+ from tqdm import tqdm
5
+ from utils import timestamp, clean_dir
6
  from config import API_TIKTOK, TIMEOUT, TMP_DIR
7
 
8
 
9
+ def download_file(url, video_id, cache_dir=f"{TMP_DIR}/tiktok"):
10
+ clean_dir(cache_dir)
 
 
 
11
  local_file = f"{cache_dir}/{video_id}.mp4"
12
  response = requests.get(url, stream=True)
13
  if response.status_code == 200:
14
+ total_size = int(response.headers.get("Content-Length", 0)) + 1
15
+ time_stamp = timestamp()
16
+ progress_bar = tqdm(
17
+ total=total_size,
18
+ unit="B",
19
+ unit_scale=True,
20
+ desc=f"[{time_stamp}] {local_file}",
21
+ )
22
+ with open(local_file, "wb") as f:
23
  for chunk in response.iter_content(chunk_size=8192):
24
+ if chunk:
25
+ f.write(chunk)
26
+ progress_bar.update(len(chunk))
27
 
 
28
  return local_file
29
 
30
 
 
37
  return None
38
 
39
 
40
+ # outer func
41
  def infer(video_url):
42
  video = parse_time = desc = avatar = author = sign = None
43
  if not video_url:
44
  desc = "The video sharing link is empty!"
45
  return video, desc, parse_time, avatar, author, sign
46
 
 
 
 
 
 
47
  try:
48
+ video_url = extract_fst_url(video_url)
49
+ if not video_url:
50
+ raise ValueError("Please enter a valid video sharing link!")
51
+
52
  response = requests.get(API_TIKTOK, params={"url": video_url}, timeout=TIMEOUT)
53
  response_json = response.json()
54
  retcode = response_json["code"]
 
65
  sign = additional_data["signature"]
66
 
67
  else:
68
+ raise ConnectionError(f"Interface call failed, error code: HTTP {retcode}")
69
 
70
  except Exception as e:
71
  desc = f"Video parsing failed: {e}"
utils.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from datetime import datetime
2
  from zoneinfo import ZoneInfo
3
  from tzlocal import get_localzone
@@ -10,3 +12,18 @@ def timestamp(naive_time: datetime = None, target_tz=ZoneInfo("Asia/Shanghai")):
10
  local_tz = get_localzone()
11
  aware_local = naive_time.replace(tzinfo=local_tz)
12
  return aware_local.astimezone(target_tz).strftime("%Y-%m-%d %H:%M:%S")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
  from datetime import datetime
4
  from zoneinfo import ZoneInfo
5
  from tzlocal import get_localzone
 
12
  local_tz = get_localzone()
13
  aware_local = naive_time.replace(tzinfo=local_tz)
14
  return aware_local.astimezone(target_tz).strftime("%Y-%m-%d %H:%M:%S")
15
+
16
+
17
+ def mk_dir(dirpath: str):
18
+ if not os.path.exists(dirpath):
19
+ os.makedirs(dirpath)
20
+
21
+
22
+ def rm_dir(dirpath: str):
23
+ if os.path.exists(dirpath):
24
+ shutil.rmtree(dirpath)
25
+
26
+
27
+ def clean_dir(dirpath: str):
28
+ rm_dir(dirpath)
29
+ os.makedirs(dirpath)