Last active
November 5, 2020 17:50
-
-
Save PttCodingMan/d128f64ad5e79f7ddb840c020bafa2ec to your computer and use it in GitHub Desktop.
Revisions
-
PttCodingMan revised this gist
Jul 10, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ def get_aid_from_url(url: str) -> (str, str): # from get_aid_from_url in PyPtt -
PttCodingMan renamed this gist
Jul 10, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
PttCodingMan created this gist
Jul 10, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ def get_aid_from_url(self, url: str) -> (str, str): # from get_aid_from_url in PyPtt # 檢查是否符合 PTT BBS 文章網址格式 pattern = re.compile('https://www.ptt.cc/bbs/[-.\w]+/M.[\d]+.A[.\w]*.html') r = pattern.search(url) if r is None: raise ValueError('url must be www.ptt.cc article url') # 演算法參考 https://www.ptt.cc/man/C_Chat/DE98/DFF5/DB61/M.1419434423.A.DF0.html # aid 字元表 aid_table = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_' board = url[23:] board = board[:board.find('/')] temp = url[url.rfind('/') + 1:].split('.') # print(temp) id_0 = int(temp[1]) # dec aid_0 = '' for _ in range(6): index = id_0 % 64 aid_0 = f'{aid_table[index]}{aid_0}' id_0 = int(id_0 / 64) if temp[3] != 'html': id_1 = int(temp[3], 16) # hex aid_1 = '' for _ in range(2): index = id_1 % 64 aid_1 = f'{aid_table[index]}{aid_1}' id_1 = int(id_1 / 64) else: aid_1 = '00' aid = f'{aid_0}{aid_1}' return board, aid