Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add error notification #3

Merged
merged 9 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: poetry install
- name: static code analysis
run: |
poetry run black --check
poetry run black --check ipfs_video_kodi test
- run: poetry run pytest
- name: build package
run: |
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ OUTPUT_PATH=build/plugin.video.ipfs

clean:
rm -rf build
rm -rf dist

build/plugin_video_ipfs.zip: build
rm -f build/plugin_video_ipfs.zip
cd build && zip -r plugin_video_ipfs.zip plugin.video.ipfs

build: $(SOURCES) fanart.jpg icon.png addon.xml resources/settings.xml
VERSION=$(poetry version --short)
sed -i 's/id="plugin.video.ipfs" version="[^"]*"/id="plugin.video.ipfs" version="'${VERSION}'"/' addon.xml
poetry build
mkdir -p $(OUTPUT_PATH)/ipfs
tar -xzf dist/ipfs-video-kodi-*.tar.gz -C dist --wildcards '*/ipfs_video_kodi'
Expand Down
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon id="plugin.video.ipfs" version="0.0.6" name="IPFS" provider-name="ipfs.video">
<addon id="plugin.video.ipfs" version="" name="IPFS" provider-name="ipfs.video">
<requires>
<import addon="xbmc.python" version="3.0.0" />
<import addon="script.module.requests" version="2.22.0" />
Expand Down
15 changes: 12 additions & 3 deletions ipfs_video_kodi/ipfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
def via(gateway):
return IPFS(gateway)


def lower_keys(dictList):
return [{k.lower(): v for k, v in entry.items()} for entry in dictList]


class IPFS:
def __init__(self, gateway):
assert len(gateway) > 0
Expand All @@ -20,13 +22,20 @@ def get_links(self, path, params):
r = requests.get(url, params=params, timeout=20)
r.raise_for_status()
rjson = r.json()
return lower_keys(
link_list = lower_keys(
filter(
lambda link: len(link["Name"]) > 0 and "/" in link["Hash"],
rjson["Links"],
lambda link: len(link["Name"]) > 0
and "/" in (link.get("Cid") or link["Hash"]),
rjson.get("links") or rjson["Links"],
)
)

# Backwards compatibility
for i in link_list:
if "cid" in i:
i["hash"] = i["cid"]
return link_list

def list(self, hash):
"""Get the directory content of the given hash"""
assert type(hash) == str
Expand Down
46 changes: 26 additions & 20 deletions ipfs_video_kodi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,32 @@ def list_node(cid):
# for this type of content.
xbmcplugin.setContent(_handle, "videos")
# Get the list of videos in the category.
links = _ipfs.list(cid)

for link in links:
is_folder = len(_ipfs.list(link["hash"]["/"])) > 0

list_item = xbmcgui.ListItem(label=link["name"])
# Set additional info for the list item.
# 'mediatype' is needed for skin to display info for this ListItem correctly.
list_item.setInfo("video", {"title": link["name"], "mediatype": "video"})
# TODO set thumbnails
# list_item.setArt({'thumb': video['thumb'], 'icon': video['thumb'], 'fanart': video['thumb']})

list_item.setProperty("IsPlayable", ("false" if is_folder else "true"))

url = self_url(action=("list" if is_folder else "play"), cid=link["hash"]["/"])
# Add our item to the Kodi virtual folder listing.
xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)
# Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.endOfDirectory(_handle)
try:
links = _ipfs.list(cid)
for link in links:
is_folder = len(_ipfs.list(link["hash"]["/"])) > 0

list_item = xbmcgui.ListItem(label=link["name"])
# Set additional info for the list item.
# 'mediatype' is needed for skin to display info for this ListItem correctly.
list_item.setInfo("video", {"title": link["name"], "mediatype": "video"})
# TODO set thumbnails
# list_item.setArt({'thumb': video['thumb'], 'icon': video['thumb'], 'fanart': video['thumb']})

list_item.setProperty("IsPlayable", ("false" if is_folder else "true"))

url = self_url(
action=("list" if is_folder else "play"), cid=link["hash"]["/"]
)
# Add our item to the Kodi virtual folder listing.
xbmcplugin.addDirectoryItem(_handle, url, list_item, is_folder)
# Add a sort method for the virtual folder items (alphabetically, ignore articles)
xbmcplugin.addSortMethod(_handle, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.endOfDirectory(_handle)
except Exception as e:
dialog = xbmcgui.Dialog()
dialog.ok("Failed to fetch information", str(e))
raise


def play_node(cid):
Expand Down
41 changes: 40 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ipfs-video-kodi"
version = "0.0.6"
version = "0.0.7"
description = "Kodi plugin to view IPFS video files"
authors = ["Bram Neijt <bram@neijt.nl>"]
license = "GPLv3"
Expand All @@ -12,6 +12,7 @@ requests = "^2.26.0"
[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
black = {version = "^21.12b0", allow-prereleases = true}
mypy = "^0.930"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
4 changes: 2 additions & 2 deletions resources/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<settings>
<category label="5">
<setting label="32000" type="text" id="rootCid" default="QmYHDhsgUgdKSAimguGC92MzQ8VNFHZw3yp6kAHwiXCFLm" />
<setting label="32001" type="text" id="ipfsGateway" default="https://ipfs.io" />
<setting label="32001" type="labelenum" id="ipfsGateway" default="https://ipfs.io" values="http://127.0.0.1:8080|https://ipfs.io|https://dweb.link|https://gateway.pinata.cloud" />
</category>
</settings>
</settings>
16 changes: 15 additions & 1 deletion test/test_ipfs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import unittest
import pytest

import ipfs_video_kodi.ipfs as ipfs

test_gateway = ipfs.via("https://ipfs.io")
Expand All @@ -14,3 +15,16 @@ def test_list_directory_should_work():
)
b = test_gateway.list("QmYHDhsgUgdKSAimguGC92MzQ8VNFHZw3yp6kAHwiXCFLm")
assert len(b) == 3


@pytest.mark.skip(reason="uses remote systems")
def test_remote_apis():
for gateway_url in [
"http://127.0.0.1:8080",
"https://ipfs.io",
"https://dweb.link",
"https://gateway.pinata.cloud",
]:
remote_gw = ipfs.via(gateway_url)
b = remote_gw.list("QmYHDhsgUgdKSAimguGC92MzQ8VNFHZw3yp6kAHwiXCFLm")
assert len(b) == 3, f"Should be able to get listing from {gateway_url}"