Created
September 16, 2022 15:49
-
-
Save kirubakaran/9a725c2398658092159eddc494c45cc3 to your computer and use it in GitHub Desktop.
Export csv of your YouTube subscriptions and import into histre.com
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 csv | |
import datetime | |
import getpass | |
import requests | |
urls = {} | |
urls["host"] = "https://histre.com/" | |
urls["api"] = f"{urls['host']}api/v1/" | |
urls["auth"] = f"{urls['api']}auth_token/" | |
urls["collection"] = f"{urls['api']}collections/" | |
urls["note"] = f"{urls['api']}note/?append=true" | |
def authenticate(username=None, password=None): | |
if username is None: | |
username = input("Enter your histre username or email: ") | |
if password is None: | |
password = getpass.getpass("Enter your histre password: ") | |
json = { | |
"username": username, | |
"password": password, | |
} | |
r = requests.post(urls["auth"], json=json) | |
if r.status_code != 200: | |
print(r.text) | |
raise Exception("Authentication failed") | |
response = r.json() | |
auth_token = response["data"]["access"] | |
return auth_token | |
def auth_header(auth_token): | |
return {"Authorization": f"Bearer {auth_token}"} | |
def create_collection(title, description, headers): | |
json = {"title": title, "description": description} | |
r = requests.post(urls["collection"], json=json, headers=headers) | |
if r.status_code >= 400: | |
print(r.text) | |
raise Exception("Collection creation failed") | |
response = r.json() | |
book_id = response["data"]["book_id"] | |
return book_id | |
def import_youtube_subscriptions(): | |
"""Import YouTube subscriptions (exported as csv) into histre""" | |
auth_token = authenticate() | |
headers = auth_header(auth_token) | |
# create collection | |
title = "YouTube Subscriptions" | |
utc_dt = datetime.datetime.now() | |
desc = f"Uploaded at {utc_dt.astimezone().strftime('%c')}" | |
book_id = create_collection(title, desc, headers) | |
book_url = f"{urls['host']}collections/{book_id}/" | |
print(f"Created collection {book_url}") | |
# save notes to collection | |
notes = [] | |
with open("youtube-subscriptions.csv") as csvfile: | |
reader = csv.reader(csvfile, delimiter=",") | |
next(reader, None) # skip headers | |
for sub in reader: | |
if len(sub) < 3: | |
continue | |
url = sub[1] | |
title = sub[2] | |
json = { | |
"url": url, | |
"title": title, | |
"book_ids": [book_id], | |
} | |
notes.append(json) | |
r = requests.post(urls["note"], json=notes, headers=headers) | |
if r.status_code >= 400: | |
print(r.text) | |
raise Exception("Note creation failed") | |
print("Successfully uploaded all subscriptions!") | |
return | |
if __name__ == "__main__": | |
import_youtube_subscriptions() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment