-
-
Save rainly/be140263f27819d0066f3d5ae1d4d81e to your computer and use it in GitHub Desktop.
清理微信公众号链接,用法:python3 cleanup_weixin.py -c == pbpaste | python3 cleanup_weixin.py -i | pbcopy
This file contains 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 characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
''' | |
@date: 2024-07-22 | |
@author: Shell.Xu | |
@copyright: 2024, Shell.Xu <[email protected]> | |
@license: BSD-3-clause | |
''' | |
import sys | |
import argparse | |
from urllib.parse import urlsplit, urlunsplit, parse_qs, urlencode | |
import clipboard | |
def cleanup(url): | |
u = list(urlsplit(url)) | |
qs = parse_qs(u[3]) | |
if not u[1].endswith('weixin.qq.com'): | |
return '' | |
qs = {k: qs[k][0] for k in ['mid', 'sn', 'idx', '__biz']} | |
u[3] = urlencode(qs) | |
return urlunsplit(u) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--interactive', '-i', action='store_true') | |
parser.add_argument('--clipboard', '-c', action='store_true') | |
parser.add_argument('rest', nargs='*', type=str) | |
args = parser.parse_args() | |
urls = args.rest | |
if args.interactive: | |
urls = sys.stdin.readlines() | |
elif args.clipboard: | |
urls = clipboard.paste().splitlines() | |
l = [cleanup(url) for url in urls] | |
if args.clipboard: | |
clipboard.copy('\n'.join(l)) | |
else: | |
print('\n'.join(l)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment