Skip to content

Instantly share code, notes, and snippets.

@shell909090
Last active August 26, 2024 03:32
Show Gist options
  • Save shell909090/25b53e02d4083aaf3f3eec2c2f42a766 to your computer and use it in GitHub Desktop.
Save shell909090/25b53e02d4083aaf3f3eec2c2f42a766 to your computer and use it in GitHub Desktop.
清理微信公众号链接,用法:python3 cleanup_weixin.py -c == pbpaste | python3 cleanup_weixin.py -i | pbcopy
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
@date: 2024-07-22
@author: Shell.Xu
@copyright: 2024, Shell.Xu <shell909090@gmail.com>
@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