I followed Kodi simple video plugin tutorial which works as expected with JSON string embedded into main.py file.
The tutorial refers that JSON string can be obtained by other means from other media (file, internet).
My attempt to read utf-8 JSON file into the string was unsuccessful so far.
Initially I tried the following approach
import json
import codecs
fname = 'c:/temp/iptv/video_data.json'
with open(fname, encoding='utf-8') as f:
data = f.read()
VIDEO = json.loads(data)
What produced the following error in log file
2020-05-09 11:11:29.327 T:19428 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.TypeError'>
Error Contents: 'encoding' is an invalid keyword argument for this function
Traceback (most recent call last):
File "C:\Users\Alex Fox\AppData\Roaming\Kodi\addons\plugin.video.example\main.py", line 27, in <module>
with open(fname, encoding='utf-8') as f:
TypeError: 'encoding' is an invalid keyword argument for this function
-->End of Python script error report<--
I investigated the problem and found that Kodi 18.4 uses python27.dll and I assume that this library is somehow accessed from Kodi.
I substituted the code above with following code snippet into main.py file
import json
import codecs
fname = 'c:/temp/iptv/video_data.json'
with open(fname, 'rb') as f:
bytes = f.read()
data = bytes.decode('utf-8')
VIDEOS = json.loads(data)
On plugin's run it produces following error record in log file
2020-05-09 16:54:17.024 T:5700 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.UnicodeEncodeError'>
Error Contents: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
Traceback (most recent call last):
File "C:\Users\Alex Fox\AppData\Roaming\Kodi\addons\plugin.video.example\main.py", line 239, in <module>
router(sys.argv[2][1:])
File "C:\Users\Alex Fox\AppData\Roaming\Kodi\addons\plugin.video.example\main.py", line 233, in router
list_categories()
File "C:\Users\Alex Fox\AppData\Roaming\Kodi\addons\plugin.video.animatron\main.py", line 137, in list_categories
url = get_url(action='listing', category=category)
File "C:\Users\Alex Fox\AppData\Roaming\Kodi\addons\plugin.video.animatron\main.py", line 66, in get_url
return '{0}?{1}'.format(_url, urlencode(kwargs))
File "C:\bin\Portable\Kodi\system\python\Lib\urllib.py", line 1343, in urlencode
v = quote_plus(str(v))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
-->End of Python script error report<--
While at the same time following code outside KODI (tested with Python 3.8.2)
import json
import codecs
fname = 'c:/temp/iptv/animation.json'
with open(fname, 'rb') as f:
bytes = f.read()
str = bytes.decode('utf-8')
VIDEOS = json.loads(str)
print(json.dumps(VIDEOS, indent=4))
for key in VIDEOS.keys():
print(key)
outputs JSON dump and VIDEO dictionary keys properly.
What is a proper way to read utf-8 encoded JSON file/internet into a string in Kodi?