Skip to content

Commit

Permalink
misc: update get_safe_path
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Lord authored Aug 16, 2023
1 parent 9fc0875 commit 54b3ae5
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
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:
def get_safe_path(s: str) -> str:
"""
Remove invalid characters & prefixes to sanitize a path.
:param s: str to sanitize
:returns: sanitized str
"""
ban_words = "\\ / : * ? \" ' < > | $ \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_words:
s = s.replace(i, "")
s = s.strip(i)
return s

def export_chathistory(user_id: str):
res = requests.get("http://localhost:48065/wechat/chatlog", params={
Expand All @@ -38,9 +36,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 54b3ae5

Please sign in to comment.