Skip to content

Commit

Permalink
Merge pull request #1 from Young-Lord/main-1
Browse files Browse the repository at this point in the history
misc: update `get_safe_path`
  • Loading branch information
JettChenT authored Aug 17, 2023
2 parents 9fc0875 + 1605fbd commit b4c60e7
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import json

import typer
from pathlib import Path
import requests
from tqdm import tqdm

app = typer.Typer()

# Credits: YoungLord https://github.com/Young-Lord/QQ-History-Backup/blob/b7d7f136020d0699c8dd802b3dff65247aeb6698/QQ_History.py#L99
def getSafePath(ans: str) -> str:
ban_words = "\\ / : * ? \" ' < > | $ \r \n".replace(
def get_safe_path(s: str) -> str:
"""
Remove invalid characters to sanitize a path.
:param s: str to sanitize
:returns: sanitized str
"""
ban_chars = "\\ / : * ? \" ' < > | $ \r \n".replace(
' ', '')
ban_strips = "#/~"
while True:
ans_bak = ans
for i in ban_words:
ans = ans.replace(i, "")
for i in ban_strips:
ans = ans.strip(i)
if ans == ans_bak: # 多次匹配
break
return ans
for i in ban_chars:
s = s.replace(i, "")
return s

def export_chathistory(user_id: str):
res = requests.get("http://localhost:48065/wechat/chatlog", params={
Expand All @@ -38,9 +34,9 @@ def export_all(dest: Path):

for user in tqdm(all_users['items']):
usr_chatlog = export_chathistory(user['arg'])
out_path = dest/getSafePath((user['title'] or "")+"-"+user['arg']+'.json')
with open(out_path, 'w') as f:
out_path = dest/get_safe_path((user['title'] or "")+"-"+user['arg']+'.json')
with open(out_path, 'w', encoding='utf-8') as f:
json.dump(usr_chatlog, f)

if __name__ == "__main__":
app()
app()

0 comments on commit b4c60e7

Please sign in to comment.