Skip to content

Instantly share code, notes, and snippets.

@ranfysvalle02
Created February 14, 2024 00:26
Show Gist options
  • Save ranfysvalle02/c9329f089378e3bf4d1289a78b3be694 to your computer and use it in GitHub Desktop.
Save ranfysvalle02/c9329f089378e3bf4d1289a78b3be694 to your computer and use it in GitHub Desktop.
YouTube into Mendable
import requests
import scrapetube
import json
def get_video_urls_from_channel_or_playlist(id, type):
if type == 'channel':
videos = scrapetube.get_channel(id)
elif type == 'playlist':
videos = scrapetube.get_playlist(id)
else:
raise ValueError('Invalid type. Please choose "channel" or "playlist".')
video_urls = ['https://www.youtube.com/watch?v=' + video['videoId'] for video in videos]
return video_urls
def ingest_urls(api_key, urls):
url = 'https://api.mendable.ai/v1/ingestData'
headers = {'Content-Type': 'application/json'}
for video_url in urls:
data = {
'api_key': api_key,
'url': video_url,
'type': 'youtube'
}
response = requests.post(url, headers=headers, data=json.dumps(data))
if response.status_code != 200:
print(f'Failed to ingest {video_url}. Status code: {response.status_code}')
# Example usage
urls = get_video_urls_from_channel_or_playlist('UCCezIgC97PvUuR4_gbFUs5g', 'channel')
ingest_urls('YOUR_API_KEY', urls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment