Skip to content

Instantly share code, notes, and snippets.

@ProphetLamb
Created August 30, 2022 07:45
Show Gist options
  • Save ProphetLamb/98c86e851bb9d8c5847ea4525c866c22 to your computer and use it in GitHub Desktop.
Save ProphetLamb/98c86e851bb9d8c5847ea4525c866c22 to your computer and use it in GitHub Desktop.
Create a temporary youtuble playlist from NewPipe exported database
#!/usr/bin/env python3
import typing as t
import click
import lxml.html as xaml
import pandas as pd
import requests as req
import sqlite3
def extract_csv(db_file: str) -> pd.DataFrame:
conn=sqlite3.connect(db_file)
cmds="""
select
url,
title,
stream_type,
duration,
uploader,
streams.thumbnail_Url,
playlists.name as playlist_name,
playlists.thumbnail_url as playlist_thumbnail_url
from streams
inner join playlist_stream_join on playlist_stream_join.stream_id=streams.uid
inner join playlists on playlists.uid == playlist_stream_join.playlist_id
"""
cur=conn.cursor()
cur.execute(cmds)
rows=cur.fetchall()
return pd.DataFrame(rows, columns=['url','title','stream_type','duration','uploader','stream_thumbnail_url','playlist_name','playlist_thumbnail_url'])
def gen_playlist(urls: t.List[str]) -> req.Response:
data=dict({('listItem{}'.format(i+1), url) for i, url in enumerate(urls)})
return req.post('https://playlist.ws/', headers={'ContentType': 'application/x-www-form-urlencoded'}, data=data)
def rsp_to_link(rsp: req.Response) -> t.Tuple[str, str]:
dom=xaml.fromstring(rsp.text)
a_yt=dom.xpath('/html/body/div/div/div/div/div/div/div[4]/span[2]/a')[0]
a_shrink=dom.xpath('/html/body/div/div/div/div/div/div/div[3]/span[2]/a')[0]
return a_yt.text, a_shrink.text
@click.group()
def main():
pass
@main.command()
@click.argument('db-file', type=click.Path(exists=True, dir_okay=False))
@click.argument('csv-file', type=click.Path(dir_okay=False))
def extract(db_file, csv_file):
df=extract_csv(db_file)
df.to_csv(csv_file, index=False)
print('playlist_name:', list(df.groupby('playlist_name').groups.keys()))
@main.command()
@click.argument('csv-file', type=click.Path(exists=True, dir_okay=False))
@click.argument('playlist-name', type=str)
def upload(csv_file, playlist_name: str):
df=pd.read_csv(csv_file)
playlist = df[df['playlist_name'] == playlist_name]
rsp = gen_playlist(playlist["url"])
(yt, shrink) = rsp_to_link(rsp)
print('youtube:', yt)
print('shrink:', shrink)
print('powered by playlist.ws')
if __name__ == '__main__':
main()
#!/usr/bin/env bash
newpipe-playlist.py extract ./newpipe.db ./playlists.csv
# playlist_name: ['Favourites', 'Best Music']
newpipe-playlist.py upload ./playlists.csv 'Favourites'
# youtube: youtuble.com/...
# shrink: tiny.url/...
# powered by playlist.ws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment