Make it async
This commit is contained in:
parent
60a0b8e582
commit
6bcb87c62a
2 changed files with 17 additions and 11 deletions
|
|
@ -2,9 +2,17 @@ import psycopg
|
||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from os import makedirs, path
|
from os import makedirs, path
|
||||||
|
from asyncio import run
|
||||||
|
|
||||||
from . import database
|
from . import database
|
||||||
|
|
||||||
|
async def connect_and_export(connection_string: str, username: str):
|
||||||
|
async with await psycopg.AsyncConnection.connect(connection_string, row_factory = psycopg.rows.namedtuple_row) as conn:
|
||||||
|
async with conn.cursor() as cur:
|
||||||
|
data_export = await database.export_user(cur, username)
|
||||||
|
|
||||||
|
print(data_export.to_dict(), end='')
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = ArgumentParser(
|
parser = ArgumentParser(
|
||||||
prog='invidious-exporter',
|
prog='invidious-exporter',
|
||||||
|
|
@ -15,8 +23,6 @@ def main():
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
with psycopg.connect(args.database, row_factory = psycopg.rows.namedtuple_row) as conn:
|
run(connect_and_export(args.database, args.username))
|
||||||
with conn.cursor() as cur:
|
|
||||||
data_export = database.export_user(cur, args.username)
|
|
||||||
|
|
||||||
print(data_export.to_dict(), end='')
|
|
||||||
|
|
@ -4,7 +4,7 @@ from .objects import Channel, Video, Playlist, PlaylistPrivacy, InvidiousJsonDat
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def get_playlists(cur: Cursor, user_email: str) -> list[Playlist]:
|
async def get_playlists(cur: Cursor, user_email: str) -> list[Playlist]:
|
||||||
sql = """
|
sql = """
|
||||||
SELECT
|
SELECT
|
||||||
p.title,
|
p.title,
|
||||||
|
|
@ -21,7 +21,7 @@ def get_playlists(cur: Cursor, user_email: str) -> list[Playlist]:
|
||||||
p.index, p.title, p.description, p.privacy;
|
p.index, p.title, p.description, p.privacy;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cur.execute(sql, (user_email,))
|
await cur.execute(sql, (user_email,))
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Playlist(
|
Playlist(
|
||||||
|
|
@ -30,18 +30,18 @@ def get_playlists(cur: Cursor, user_email: str) -> list[Playlist]:
|
||||||
privacy=PlaylistPrivacy[row.privacy],
|
privacy=PlaylistPrivacy[row.privacy],
|
||||||
video_ids=row.video_ids or []
|
video_ids=row.video_ids or []
|
||||||
)
|
)
|
||||||
for row in cur.fetchall()
|
for row in await cur.fetchall()
|
||||||
]
|
]
|
||||||
|
|
||||||
def export_user(cur: Cursor, user_email: str) -> InvidiousJsonDataExport:
|
async def export_user(cur: Cursor, user_email: str) -> InvidiousJsonDataExport:
|
||||||
cur.execute("SELECT subscriptions, watched, preferences FROM users WHERE email = %s", (user_email,))
|
await cur.execute("SELECT subscriptions, watched, preferences FROM users WHERE email = %s", (user_email,))
|
||||||
|
|
||||||
data = cur.fetchone()
|
data = await cur.fetchone()
|
||||||
subscribed_channels_ids = data.subscriptions
|
subscribed_channels_ids = data.subscriptions
|
||||||
watched_videos_ids = data.watched
|
watched_videos_ids = data.watched
|
||||||
user_preferences = json.loads(data.preferences)
|
user_preferences = json.loads(data.preferences)
|
||||||
|
|
||||||
playlists = get_playlists(cur, user_email)
|
playlists = await get_playlists(cur, user_email)
|
||||||
|
|
||||||
data_export = InvidiousJsonDataExport(
|
data_export = InvidiousJsonDataExport(
|
||||||
subscriptions = [Channel(id=c_id) for c_id in subscribed_channels_ids],
|
subscriptions = [Channel(id=c_id) for c_id in subscribed_channels_ids],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue