Initial commit.

This commit is contained in:
Sunoru 2024-07-14 15:51:00 -04:00
commit e2f1a31d0e
Signed by: sunoru
GPG key ID: 37AFC6011869B4DF
3 changed files with 58 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
__pycache__/
*.pyc
*.swp
build/

7
README.md Normal file
View file

@ -0,0 +1,7 @@
# weibo-lottery
一个简单的脚本,用来把一条微博的所有转发保存到本地的 CSV 文件。
## License
[The MIT License](https://sunoru.mit-license.org/)

47
main.py Normal file
View file

@ -0,0 +1,47 @@
import csv
import time
import requests
import fire
RepostAPI = "https://m.weibo.cn/api/statuses/repostTimeline"
class Main():
def _get_repost_page(self, weiboId, page, session=requests):
params = {"id": weiboId, "page": page}
response = session.get(RepostAPI, params=params)
return response.json()
def _get_reposts(self, cookie, weiboId, wait):
max_page = None
i = 0
with requests.Session() as session:
session.headers.update({"Cookie": cookie})
while max_page is None or i < max_page:
i += 1
print(f"Fetching page {i}...")
page = self._get_repost_page(weiboId, i, session)
if max_page is None:
max_page = page["data"]["max"]
for repost in page["data"]["data"]:
yield repost
time.sleep(wait)
def _to_row(self, repost):
user = repost["user"]["screen_name"]
time = repost["created_at"]
text = repost["raw_text"]
url = f'https://www.weibo.com/{repost["user"]["id"]}/{repost["bid"]}'
return [user, time, text, url]
def fetch_reposts(self, output, wait=1):
weiboId = input("Weibo ID: ")
cookie = input("Cookie: ")
with open(output, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["User", "Time", "Text", "URL"])
for repost in self._get_reposts(cookie, weiboId, wait):
writer.writerow(self._to_row(repost))
print(f"Reposts saved to {output}")
fire.Fire(Main)