Published on

Shorten Apple Media Links with Python

In this tutorial, I’d like to share how you can write a python script to use the hidden Apple Media Tools API to shorten Apple Media Links such as apps, podcasts, and more using a simple python script. The process is easy, but it requires a clear understanding of the requirements for the shorten link API.

Libraries

For this script, we’d need 2 libraries: requests and BeautifulSoup. Start by importing them

import requests
from bs4 import BeautifulSoup

Fetching Cookies and CSRF Token

To facilitate the request to the shorten link API, we need two items coming in from the home page: The _pineapple_medusa_session cookie (I have no clue as to why it’s named that), and the CSRF token. According to synopsys.com:

A CSRF token is a secure random token (e.g., synchronizer token or challenge token) that is used to prevent CSRF attacks. The token needs to be unique per user session and should be of large random value to make it difficult to guess. A CSRF secure application assigns a unique CSRF token for every user session.

To fetch these items, we will perform a GET request on the home page of Apple Media Tools. We are using BeautifulSoup to parse the HTML to access the meta tags to fetch the csrf-token.

homePageURL = "https://tools.applemediaservices.com/"
homePageRequest = requests.get(homePageURL)
soup = BeautifulSoup(homePageRequest.content, "lxml")

Once the request is complete, fetch the csrf-token using

meta = soup.find("meta", attrs={'name': 'csrf-token'})
csrfToken = meta['content']

To access the _pineapple_medusa_session cookie, we will access the dictionary of cookies for this request that is easily available to us using

medusaCookie = homePageRequest.cookies.get_dict()['_pineapple_medusa_session']

Before we begin creating the request for shortening the link, we need to first ask for the link from the user. Use the input() method to fetch text from the python console.

longURL = input('Enter the long media URL: ')

Creating the request

The shorten link API request requires 4 parameters in the header and a body with one parameter encoded as JSON. Start by creating the header object:

headers = {
    'Cookie': '_pineapple_medusa_session=' + medusaCookie,
    'X-CSRF-Token': csrfToken,
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/json;charset=utf-8'
}

Then, create the body with the longURL that we had requested from the user:

shortenBody = {
    'url': longURL
}

The API request will be made to a subpath of the initial URL that we had used to fetch the cookies and token. Create a new variable for the shorten link API endpoint.

shortenURL = homePageURL + "api/short-link/shorten"

Performing the request

Perform a POST request with the headers and the body objects created in the previous steps. Ensure that the body object is passed as a JSON and not just as data.

shortenRequest = requests.post(shortenURL, headers = headers, json = shortenBody)

If the request succeeded, the shortened link will be available in the response JSON’s link property.

if shortenRequest.ok:
    print(shortenRequest.json()['link'])
else:
    print ('Sorry, the API failed to shorten the URL.')

Conclusion

In this tutorial we learnt how the Apple Media Tools’ shorten API service works and we were able to recreate that request flow in python. Now, you can simply run python3 pathToFile.py in your terminal, type in the long URL, and immediately get the short URL without having to open your web browser.