Skip to content

Instantly share code, notes, and snippets.

@austinsonger
Last active November 25, 2023 01:09
Show Gist options
  • Save austinsonger/1a18ceb9cc5d90807ddf08c478731107 to your computer and use it in GitHub Desktop.
Save austinsonger/1a18ceb9cc5d90807ddf08c478731107 to your computer and use it in GitHub Desktop.
Create draft Podcast Episodes in Transistor from CSV

This Python script automates the process of creating draft podcast episodes in Transistor from a CSV file. The script reads episode data (titles and summaries) from a CSV file and uses the Transistor API to create each episode as a draft in your Transistor account.

How it Works

  • CSV File Parsing: The script begins by reading a CSV file specified by csv_file_path. This file should contain the episode titles and summaries. It's expected that the first row of the CSV file is a header row, which the script skips.
  • Creating Episode Objects: For each row in the CSV file, the script extracts the title and summary, ensuring that both are present. It then creates an episode object containing this data, along with the show ID (show_id).
  • Interacting with the Transistor API: The script sends a POST request to the Transistor API for each episode object. The API endpoint (api_url) is set to "https://api.transistor.fm/v1/episodes".
  • API Authentication: The script uses an API key (api_key) for authentication. This key should be obtained from your Transistor account and replaced in the script.
  • Error Handling and Feedback: After each API request, the script checks the response. If the episode is created successfully (HTTP status code 201), a success message is printed. Otherwise, an error message with the status code and response details is printed.

Configuration and Usage

  • Before running the script, ensure that you replace api_key, show_id, and csv_file_path with your actual API key, show ID, and the path to your CSV file, respectively.
  • The CSV file should have two columns: one for the episode title and the other for the summary. Ensure that this format is maintained for the script to work correctly.
import csv
import requests
# Function to parse the CSV file and create draft episodes
def create_draft_episodes_from_csv(csv_file_path, show_id):
episodes = []
with open(csv_file_path, mode='r', encoding='utf-8') as file:
csv_reader = csv.reader(file)
next(csv_reader) # Skip the header row
for row in csv_reader:
if len(row) < 2: # Ensure the row has at least title and summary
continue
title, summary = row[0], row[1]
episode = {
"episode[show_id]": show_id,
"episode[title]": title.strip(),
"episode[summary]": summary.strip()
}
episodes.append(episode)
return episodes
# Transistor.fm API endpoint and authorization
api_url = "https://api.transistor.fm/v1/episodes"
api_key = "[API_KEY]" # Replace with your actual API key
show_id = "[SHOW_ID]" # "#####" Replace with your actual Show ID
# Headers for the API request
headers = {
"x-api-key": api_key
}
# Function to create a draft episode in Transistor
def create_draft_episode_in_transistor(episode):
response = requests.post(api_url, data=episode, headers=headers)
return response
# Main function to process the CSV and create episodes in Transistor
def process_episodes(csv_file_path):
episodes = create_draft_episodes_from_csv(csv_file_path, show_id)
for episode in episodes:
response = create_draft_episode_in_transistor(episode)
if response.status_code == 201:
print(f"Draft episode '{episode['episode[title]']}' created successfully.")
else:
print(f"Failed to create episode '{episode['episode[title]']}'. Status Code: {response.status_code}, Response: {response.json()}")
# Example usage (replace with your actual values)
csv_file_path = 'path_to_your_csv_file' # Replace with the path to your CSV file
process_episodes(csv_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment