diff --git a/TODO b/TODO index 20d13cb..e1eb15b 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ ## MISSING -- Viewing User Profiles - Viewing Comment Replies - Length Of Video in search_results.html ## FIX diff --git a/main.py b/main.py index 4177cd7..f909308 100644 --- a/main.py +++ b/main.py @@ -47,33 +47,56 @@ class VideoWrapper: self.no_quality_selected = not self.video -# Format: -# key: domain -# entry: [ instance_name, last_time_updated ] -cached_instance_names = {} +# Helper Class for using caches +class Cache: + def __init__(self): + self.dict = {} + + def get(self, arg, func): + if arg in self.dict: + last_time_updated = (self.dict[arg])[1] + time_diff = datetime.now() - last_time_updated + + if time_diff.days > 0: + self.dict[arg] = [ + func(arg), + datetime.now() + ] + else: + self.dict[arg] = [ + func(arg), + datetime.now() + ] + + return (self.dict[arg])[0] + + +cached_instance_names = Cache() +cached_account_infos = Cache() +cached_video_channel_infos = Cache() # cache the instance names so we don't have to send a request to the domain every time someone # loads any site def get_instance_name(domain): - if domain in cached_instance_names: - last_time_updated = (cached_instance_names[domain])[1] - time_diff = datetime.now() - last_time_updated - - # only check once every day - if time_diff.days != 0: - cached_instance_names[domain] = [ - peertube.get_instance_name(domain), - datetime.now() - ] - else: - cached_instance_names[domain] = [ - peertube.get_instance_name(domain), - datetime.now() - ] - - return (cached_instance_names[domain])[0] + return cached_instance_names.get(domain, peertube.get_instance_name) +# simple wrapper that is used inside the cached_account_infos +def get_account(info): + info = info.split("@") + return peertube.account(info[1], info[0]) + +def get_account_info(name): + return cached_account_infos.get(name, get_account) + + +# simple wrapper that is used inside the cached_video_channel_infos +def get_video_channel(info): + info = info.split("@") + return peertube.video_channel(info[1], info[0]) + +def get_video_channel_info(name): + return cached_video_channel_infos.get(name, get_video_channel) @@ -142,5 +165,101 @@ async def video(domain, id): embed=embed, ) + +def build_channel_or_account_name(domain, name): + if '@' in name: + return name + return name + "@" + domain + +# --- Accounts --- + +@app.route("//accounts/") +async def accounts(domain, name): + return redirect("/" + domain + "/accounts/" + name + "/video-channels") + +@app.route("//accounts//video-channels") +async def account__video_channels(domain, name): + return await render_template( + "accounts/video_channels.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + account = get_account_info(build_channel_or_account_name(domain, name)), + video_channels = peertube.account_video_channels(domain, name) + ) + +@app.route("//accounts//videos") +async def account__videos(domain, name): + return await render_template( + "accounts/videos.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + account = get_account_info(build_channel_or_account_name(domain, name)), + videos = peertube.account_videos(domain, name) + ) + +@app.route("//accounts//about") +async def account__about(domain, name): + return await render_template( + "accounts/about.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + account = get_account_info(build_channel_or_account_name(domain, name)), + about = peertube.account(domain, name) + ) + +# --- Video-Channels --- + +@app.route("//video-channels/") +async def video_channels(domain, name): + return redirect("/" + domain + "/video-channels/" + name + "/videos") + +@app.route("//video-channels//videos") +async def video_channels__videos(domain, name): + return await render_template( + "video_channels/videos.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)), + videos = peertube.video_channel_videos(domain, name) + ) + +@app.route("//video-channels//video-playlists") +async def video_channels__video_playlists(domain, name): + return await render_template( + "video_channels/video_playlists.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)), + video_playlists = peertube.video_channel_video_playlists(domain, name) + ) + +@app.route("//video-channels//about") +async def video_channels__about(domain, name): + return await render_template( + "video_channels/about.html", + domain=domain, + commit=commit, + instance_name=get_instance_name(domain), + + name = name, + video_channel = get_video_channel_info(build_channel_or_account_name(domain, name)), + about = peertube.video_channel(domain, name) + ) + if __name__ == "__main__": app.run() diff --git a/peertube.py b/peertube.py index d39a263..0f56cbe 100644 --- a/peertube.py +++ b/peertube.py @@ -26,16 +26,48 @@ def search(domain, term, start = 0, count = 10): def get_comments(domain, id): url = "https://" + domain + "/api/v1/videos/" + id + "/comment-threads" - comments_object = json.loads(requests.get(url).text) - return comments_object + return json.loads(requests.get(url).text) +# --- Accounts --- + +def account_video_channels(domain, name): + url = "https://" + domain + "/api/v1/accounts/" + name + "/video-channels" + return json.loads(requests.get(url).text) + +def account_videos(domain, name): + url = "https://" + domain + "/api/v1/accounts/" + name + "/videos" + return json.loads(requests.get(url).text) + +def account(domain, name): + url = "https://" + domain + "/api/v1/accounts/" + name + return json.loads(requests.get(url).text) + +# --- Video Channels --- + +def video_channel_videos(domain, name): + url = "https://" + domain + "/api/v1/video-channels/" + name + "/videos" + return json.loads(requests.get(url).text) + +def video_channel_video_playlists(domain, name): + url = "https://" + domain + "/api/v1/video-channels/" + name + "/video-playlists" + return json.loads(requests.get(url).text) + +def video_channel(domain, name): + url = "https://" + domain + "/api/v1/video-channels/" + name + return json.loads(requests.get(url).text) if __name__ == "__main__": #name = get_instance_name("videos.lukesmith.xyz") #print(name) - com = get_comments("videos.lukesmith.xyz", "d1bfb082-b203-43dc-9676-63d28fe65db5") - print(json.dumps(com, indent=2)) + + #com = get_comments("videos.lukesmith.xyz", "d1bfb082-b203-43dc-9676-63d28fe65db5") + #print(json.dumps(com, indent=2)) + #vid = video("diode.zone", "c4f0d71b-bd8b-4641-87b0-6d9edd4fa9ce") #print(json.dumps(vid, indent=2)) - #_, results = search("diode.zone", "test") - #print(json.dumps(results, indent=2)) + + _, results = search("diode.zone", "test") + print(json.dumps(results, indent=2)) + + #video_channels = account_video_channels("peer.tube", "mouse@peertube.dsmouse.net") + #print(json.dumps(video_channels, indent=2)) diff --git a/templates/accounts/about.html b/templates/accounts/about.html new file mode 100644 index 0000000..a46e22a --- /dev/null +++ b/templates/accounts/about.html @@ -0,0 +1,9 @@ +{% extends "accounts/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +Coming Soon! + +{% endblock %} diff --git a/templates/accounts/base.html b/templates/accounts/base.html new file mode 100644 index 0000000..3224983 --- /dev/null +++ b/templates/accounts/base.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block content %} + + + + + +
+ + + {{ account.displayName }} {{ account.name }} +
+ {{ account.followersCount }} Followers +
+ +
+
+Video Channels + | +Videos + | +About +
+
+ +{% block sub_content %}{% endblock %} + +{% endblock %} diff --git a/templates/accounts/video_channels.html b/templates/accounts/video_channels.html new file mode 100644 index 0000000..f870692 --- /dev/null +++ b/templates/accounts/video_channels.html @@ -0,0 +1,28 @@ +{% extends "accounts/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +{{ video_channels.total }} Channels +
+
+ + +{% for channel in video_channels.data %} + + + + +{% endfor %} +
+ + + + {{ channel.displayName }} + +
+ {{ channel.followersCount }} Followers +
+ +{% endblock %} diff --git a/templates/accounts/videos.html b/templates/accounts/videos.html new file mode 100644 index 0000000..40a10d5 --- /dev/null +++ b/templates/accounts/videos.html @@ -0,0 +1,31 @@ +{% extends "accounts/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +{{ videos.total }} Videos +
+
+ + +{% for video in videos.data %} + + + + +{% endfor %} +
+ + + + + {{ video.name }} +
+ {{ video.views }} Views +
+ {{ video.channel.displayName }} + +
+ +{% endblock %} diff --git a/templates/search_results.html b/templates/search_results.html index e98b3bb..142e20c 100644 --- a/templates/search_results.html +++ b/templates/search_results.html @@ -14,11 +14,17 @@ - {{ result.name }} + {{ result.name }}
{{ result.views }} Views
- {{ result.channel.displayName }} + + {{ result.channel.displayName }} + +
+ + {{ result.account.name }}@{{ result.account.host }} + {% endfor %} diff --git a/templates/video.html b/templates/video.html index bb85b50..008d1d4 100644 --- a/templates/video.html +++ b/templates/video.html @@ -5,7 +5,13 @@ {% block content %}

{{ video.name }}

By: -{{ video.channel.displayName }} ({{ video.channel.name }}@{{ video.channel.host }}) + + {{ video.channel.displayName }} + + + ({{ video.channel.ownerAccount.name }}@{{ video.channel.ownerAccount.host }}) + +
{% if video.no_quality_selected and not embed %} diff --git a/templates/video_channels/about.html b/templates/video_channels/about.html new file mode 100644 index 0000000..fdaaad1 --- /dev/null +++ b/templates/video_channels/about.html @@ -0,0 +1,9 @@ +{% extends "video_channels/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +Coming Soon! + +{% endblock %} diff --git a/templates/video_channels/base.html b/templates/video_channels/base.html new file mode 100644 index 0000000..0267614 --- /dev/null +++ b/templates/video_channels/base.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block content %} + + + + + +
+ + + {{ video_channel.displayName }} {{ video_channel.name }} +
+ {{ video_channel.followersCount }} Followers +
+ Created By + {{ video_channel.ownerAccount.name }} + +
+ +
+
+Videos + | +Video Playlists + | +About +
+
+ +{% block sub_content %}{% endblock %} + +{% endblock %} diff --git a/templates/video_channels/video_playlists.html b/templates/video_channels/video_playlists.html new file mode 100644 index 0000000..1f4625b --- /dev/null +++ b/templates/video_channels/video_playlists.html @@ -0,0 +1,27 @@ +{% extends "video_channels/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +{{ video_playlists.total }} Playlists +
+
+ + +{% for playlist in video_playlists.data %} + + + + + +{% endfor %} +
+ + + {{ playlist.displayName }} +
+ {{ playlist.videosLength }} Videos +
+ +{% endblock %} diff --git a/templates/video_channels/videos.html b/templates/video_channels/videos.html new file mode 100644 index 0000000..8ddd337 --- /dev/null +++ b/templates/video_channels/videos.html @@ -0,0 +1,32 @@ +{% extends "video_channels/base.html" %} + +{% block title %}{{ domain }}{% endblock %} + +{% block sub_content %} + +{{ videos.total }} Videos +
+
+ + +{% for video in videos.data %} + + + + + +{% endfor %} +
+ + + + + {{ video.name }} +
+ {{ video.views }} Views +
+ {{ video.channel.displayName }} + +
+ +{% endblock %}