Skip to content

Instantly share code, notes, and snippets.

@SwapnanilDhol
Created October 24, 2022 08:26
Show Gist options
  • Save SwapnanilDhol/e02b276a108c5f1816ccc129a0b8569a to your computer and use it in GitHub Desktop.
Save SwapnanilDhol/e02b276a108c5f1816ccc129a0b8569a to your computer and use it in GitHub Desktop.
Apple Media Tools Shorten Link
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