Created
October 24, 2022 08:26
-
-
Save SwapnanilDhol/e02b276a108c5f1816ccc129a0b8569a to your computer and use it in GitHub Desktop.
Apple Media Tools Shorten Link
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
import requests | |
from bs4 import BeautifulSoup | |
longURL = input('Enter the long media URL: ') | |
homePageURL = "https://tools.applemediaservices.com/" | |
homePageRequest = requests.get(homePageURL) | |
soup = BeautifulSoup(homePageRequest.content, "lxml") | |
meta = soup.find("meta", attrs={'name': 'csrf-token'}) | |
csrfToken = meta['content'] | |
medusaCookie = homePageRequest.cookies.get_dict()['_pineapple_medusa_session'] | |
shortenURL = homePageURL + "api/short-link/shorten" | |
headers = { | |
'Cookie': '_pineapple_medusa_session=' + medusaCookie, | |
'X-CSRF-Token': csrfToken, | |
'X-Requested-With': 'XMLHttpRequest', | |
'Content-Type': 'application/json;charset=utf-8' | |
} | |
shortenBody = { | |
'url': longURL | |
} | |
shortenRequest = requests.post(shortenURL, headers = headers, json = shortenBody) | |
if shortenRequest.ok: | |
print(shortenRequest.json()['link']) | |
else: | |
print ('Sorry, the API failed to shorten the URL.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment