Skip to content

Instantly share code, notes, and snippets.

@samgrover
Created May 11, 2021 22:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samgrover/acb966142f1bc7e182ba56ecf9979850 to your computer and use it in GitHub Desktop.
Save samgrover/acb966142f1bc7e182ba56ecf9979850 to your computer and use it in GitHub Desktop.
Extract URL From Apple News URL
#!/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