Download app.py from alige/Dk: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/spaces/alige/Dk/resolve/main/app.py
- Command line
-
hf download hf://spaces/alige/Dk/app.py
-
curl -L -o app.py https://huggingface.co/spaces/alige/Dk/resolve/main/app.py
4.66 kB
| from flask import Flask, render_template, request, redirect, url_for, send_file, flash | |
| from datetime import datetime | |
| import pandas as pd | |
| import io | |
| from yt_dlp import YoutubeDL | |
| app = Flask(__name__) | |
| app.secret_key = "change_this_to_something_random" | |
| # 最多获取多少条视频用于 Python 端日期筛选 | |
| # 设为 None 则不限制(会扫描频道全部视频,速度极慢) | |
| MAX_FETCH = 60 | |
| def fetch_videos_by_date(profile_url, date_from=None, date_to=None): | |
| ydl_opts = { | |
| "ignoreerrors": True, | |
| "quiet": True, | |
| "skip_download": True, | |
| "playlistend": MAX_FETCH, | |
| } | |
| with YoutubeDL(ydl_opts) as ydl: | |
| info = ydl.extract_info(profile_url, download=False) | |
| entries = [] | |
| if not info: | |
| return [] | |
| if "entries" in info and info["entries"]: | |
| for e in info["entries"]: | |
| if e: | |
| entries.append(e) | |
| else: | |
| entries = [info] | |
| normalized = [] | |
| for e in entries: | |
| video_id = e.get("id") or e.get("display_id") or e.get("url") | |
| title = e.get("title", "") | |
| webpage_url = e.get("webpage_url") or e.get("url") | |
| upload_date = e.get("upload_date") | |
| timestamp = e.get("timestamp") | |
| if timestamp: | |
| dt = datetime.utcfromtimestamp(timestamp) | |
| date_str = dt.strftime("%Y-%m-%d") | |
| elif upload_date: | |
| try: | |
| date_str = datetime.strptime(str(upload_date), "%Y%m%d").strftime("%Y-%m-%d") | |
| except Exception: | |
| date_str = str(upload_date) | |
| else: | |
| date_str = "" | |
| view_count = e.get("view_count") if e.get("view_count") is not None else e.get("views") | |
| like_count = e.get("like_count") if e.get("like_count") is not None else e.get("likes") | |
| normalized.append({ | |
| "id": video_id, | |
| "title": title, | |
| "url": webpage_url, | |
| "date": date_str, | |
| "views": view_count, | |
| "likes": like_count, | |
| }) | |
| # Secondary date filter in Python for accuracy | |
| def in_range(d): | |
| if not d: | |
| return False | |
| try: | |
| dt = datetime.strptime(d, "%Y-%m-%d") | |
| except ValueError: | |
| return False | |
| if date_from and dt < datetime.strptime(date_from, "%Y-%m-%d"): | |
| return False | |
| if date_to and dt > datetime.strptime(date_to, "%Y-%m-%d"): | |
| return False | |
| return True | |
| filtered = [e for e in normalized if in_range(e["date"])] | |
| def sort_key(x): | |
| try: | |
| return datetime.strptime(x["date"], "%Y-%m-%d") | |
| except Exception: | |
| return datetime.min | |
| filtered.sort(key=sort_key, reverse=True) | |
| return filtered | |
| def index(): | |
| if request.method == "POST": | |
| profile_url = request.form.get("profile_url", "").strip() | |
| date_from = request.form.get("date_from", "").strip() or None | |
| date_to = request.form.get("date_to", "").strip() or None | |
| if not profile_url: | |
| flash("请先输入 TikTok 或 YouTube 的主页链接。") | |
| return redirect(url_for("index")) | |
| try: | |
| entries = fetch_videos_by_date(profile_url, date_from, date_to) | |
| except Exception as ex: | |
| flash(f"抓取失败:{ex}") | |
| return redirect(url_for("index")) | |
| if not entries: | |
| flash("该时间段内没有找到视频,请确认链接或日期范围是否有效。") | |
| return redirect(url_for("index")) | |
| rows = [] | |
| for e in entries: | |
| rows.append({ | |
| "视频ID": e["id"], | |
| "发布日期": e["date"], | |
| "标题": e["title"], | |
| "视频链接": e["url"], | |
| "播放量": e["views"], | |
| "点赞数": e["likes"], | |
| }) | |
| df = pd.DataFrame(rows) | |
| # Build filename from date range | |
| f_date_from = date_from.replace("-", "") if date_from else "begin" | |
| f_date_to = date_to.replace("-", "") if date_to else "end" | |
| filename = f"videos_{f_date_from}_{f_date_to}.xlsx" | |
| buffer = io.BytesIO() | |
| with pd.ExcelWriter(buffer, engine="openpyxl") as writer: | |
| df.to_excel(writer, index=False) | |
| buffer.seek(0) | |
| return send_file( | |
| buffer, | |
| as_attachment=True, | |
| download_name=filename, | |
| mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", | |
| ) | |
| return render_template("index.html") | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) |