Created
May 7, 2021 00:53
-
-
Save mtbdeano/f6114270aa3fa58dfeaf3a1cc60e6c22 to your computer and use it in GitHub Desktop.
Migrate omny / midroll to airwave / megaphone
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
''' | |
Data export from midroll's omny system for episodes and ad positions in a CSV (utf-8 encoded) | |
(Megaphone Developer API)[https://developers.megaphone.fm/] | |
''' | |
import csv | |
import requests | |
from collections import defaultdict | |
import time | |
NETWORK = "from megaphone" | |
PODCAST - "from medaphone" | |
MEGAPHONE = f"https://cms.megaphone.fm/api/networks/{NETWORK}/podcasts/{PODCAST}/" | |
headers = { | |
'Authorization': 'Token token="some secret megaphone auth token you own"', | |
'Content-type': 'application/json', | |
'Accept': 'application/json' | |
} | |
def extractAdPos(s): | |
''' omny export prints time as 0:14:49 or maybe 25:45.8 ''' | |
segs = s.split(':') | |
if len(segs) == 2: | |
m = int(segs[0]) | |
s = float(segs[1]) | |
else: | |
m = int(segs[1]) | |
s = int(segs[2]) | |
return m*60 + s | |
def getEpisodes(filename): | |
episodes = defaultdict(lambda: {"insertionPoints": []}) | |
with open(filename) as inf: | |
reader = csv.DictReader(inf) | |
for row in reader: | |
episodes[row['OmnyClipTitle']]["id"] = row["RssEpisodeGuid"] | |
episodes[row['OmnyClipTitle']]["title"] = row["OmnyClipTitle"] | |
adpos = row['AdMarkerPosition'] | |
secs = extractAdPos(adpos) | |
if secs > 0: | |
# print(f'{row["OmnyClipTitle"]}: pos {secs}') | |
episodes[row['OmnyClipTitle']]["insertionPoints"].append(secs) | |
return episodes | |
def getEpisodeList(): | |
resp = requests.get(f'{MEGAPHONE}episodes?', headers=headers) | |
return resp.json() | |
def addInsertionPoints(anid, pts): | |
''' two preroll, two postroll, plus whatever is in `insertionPoints` which is in seconds ''' | |
upd = { | |
"insertionPoints": pts, | |
"preCount": 2, | |
"postCount": 2 | |
} | |
#print(f"curl {MEGAPHONE}episodes/{anid}: data {upd}") | |
resp = requests.put(f'{MEGAPHONE}episodes/{anid}', headers=headers, json=upd) | |
if resp.status_code >= 400: | |
print(f"errored {anid}") | |
episodes = getEpisodes("randk.csv") | |
mega_episodes = getEpisodeList() | |
for mega_ep in mega_episodes: | |
title = mega_ep.get("title") | |
if title: | |
csvdata = episodes.get(title) | |
if csvdata: | |
newips = csvdata.get("insertionPoints") | |
existingips = mega_ep.get("insertionPoints", []) | |
if len(newips) == 0 or len(existingips) > 0: | |
print(f'Skipping {title}') | |
else: | |
print(csvdata) | |
addInsertionPoints(mega_ep["id"], csvdata["insertionPoints"]) | |
time.sleep(5) # just in case rate limiting |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a cheap and cheerful script to migrate your podcast ad positions from midroll / omny to airwave / megaphone