Created
January 10, 2023 08:38
-
-
Save JanPetr/0908db1b57848816d3a88c562fec5c68 to your computer and use it in GitHub Desktop.
List all the copies for a specific asset via Pex Discovery API
This file contains 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 | |
# This example shows how to list all the copies for a specific asset via Pex Discovery API. | |
# Documentation for listing copies can be found here: https://docs.api.pex.com/#copies | |
# Use your actual key from page https://drm.pex.com/settings/api-keys | |
token = "FIXME" | |
# Use ID of your specific asset. | |
# It can be found in URL of asset detail page, eg. https://drm.pex.com/asset/5812359003677950988 <- asset ID. | |
assetID = 5812359003677950988 | |
# Specify number of copies per page / single API call. | |
copiesPerPage = 100 | |
# All URL parameters can be found here: https://docs.api.pex.com/#copies | |
urlPattern = "https://api.pex.com/v3/drm/copies?count=%d&asset_id=%d&max_id=%d" | |
# Copies are listed in descending order by ID. | |
# Let's start with the highest possible ID (int64) to get the first ID | |
# and then use the last ID returned by previous API call. | |
lastID = 9223372036854775807 | |
while True: | |
url = urlPattern % (copiesPerPage, assetID, lastID) | |
res = requests.get(url, headers={"Authorization": "Bearer " + token}) | |
if res.status_code != 200: | |
print(res.status_code, res.text) | |
break | |
parsed = res.json() | |
for copy in parsed: | |
# All attributes of a returned copy can be found here: https://docs.api.pex.com/#copies. | |
print("%d\t%s\t%s" % (copy["match_id"], copy["copy_title"], copy["copy_media_url"])) | |
lastID = copy["match_id"] | |
# Last page returns lower number of copies than is specified for a page. | |
# Let's break the loop as all copies were already listed. | |
if len(parsed) < copiesPerPage: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment