Created
May 11, 2021 22:40
-
-
Save samgrover/acb966142f1bc7e182ba56ecf9979850 to your computer and use it in GitHub Desktop.
Extract URL From Apple News URL
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 characters
#!/usr/bin/env python | |
# A helper script to convert an Apple News URL on the clipboard into | |
# its corresponding canonical URL and put that on the clipboard. | |
import pasteboard | |
import requests | |
import re | |
pb = pasteboard.Pasteboard() | |
content = pb.get_contents() | |
if content.startswith('https://apple.news/'): | |
resp = requests.get(content) | |
if resp.status_code == 200: | |
m = re.search(r'redirectToUrlAfterTimeout\("(.+)",', resp.text) | |
if m: | |
canonical = m.group(1) | |
print(f"{canonical}") | |
pb.set_contents(canonical) | |
exit(0) | |
print("No Apple News URL found on clipboard") | |
exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment