Last active
November 15, 2021 15:55
-
-
Save epilys/cb5156a59cc66ec7f8cd04771fbcdcaa to your computer and use it in GitHub Desktop.
kodi-cli.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
kodi cli tool v1 | |
Edit user, password, host and port with the info of the kodi host on your | |
network. Screenshots are saved in the folder configures in | |
"Settings/System/Logging/Debug/Screenshot folder" | |
usage: kodi [-h] [-p] [-q] [-y Y] [-t T] [-s] | |
optional arguments: | |
-h, --help show this help message and exit | |
-p pause | |
-q queue (do not auto play) | |
-y Y video or playlist url to play | |
-t T send text | |
-s take a screenshot | |
Copyright (C) 2017 Manos Pitsidianakis | |
This program is free software: you can redistribute it and/or modify it under | |
the terms of the GNU General Public License as published by the Free Software | |
Foundation, either version 3 of the License, or (at your option) any later | |
version. | |
This program is distributed in the hope that it will be useful, but WITHOUT ANY | |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | |
PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License along with | |
this program. If not, see http://www.gnu.org/licenses/. | |
""" | |
import sys | |
import subprocess | |
import json | |
import re | |
import argparse | |
user="" | |
password="" | |
host="" | |
port=80 | |
def kodi_req(req): | |
if not req: | |
return | |
command = """curl -s -X POST --header "Content-Type: application/json" -d '{}' http://{}:{}@{}:{}/jsonrpc""".format(json.dumps(req), user,password,host,port) | |
output = "" | |
try: | |
output = subprocess.check_output(command, shell=True ) | |
except subprocess.CalledProcessError as e: | |
if e.returncode != 52: | |
raise | |
return output | |
def play_youtube(url,queue=False): | |
yt_pattern = re.compile("^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.be)\/.+$") | |
if not yt_pattern.match(url): | |
print("Not a yt url?") | |
sys.exit(1) | |
list_pattern = re.compile("^(?:https?\:\/\/)?(?:(?:www\.)?youtube\.com|youtu\.be)\/playlist\?list=([^#\&\?]*).*$") | |
video_pattern = re.compile("^(?:https?\:\/\/)?(?:(?:www\.)?youtube\.com|youtu\.be)\/(?:watch\?v=)?([^#\&\?]*).*$") | |
if list_pattern.match(url) and list_pattern.match(url).groups(1)[0]: | |
pl_id = list_pattern.match(url).groups(1)[0] | |
if not queue: | |
kodi_req({ 'jsonrpc' : '2.0', | |
'method' : 'Playlist.Clear', | |
'params': {'playlistid' : 1 }, | |
'id' : 1 | |
}) | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Playlist.Add", | |
"params": { "playlistid":1, | |
"item" :{ "file" : "plugin://plugin.video.youtube/?action=play_all&playlist={}".format(pl_id),} | |
}, | |
"id" : 1}) | |
if not queue: | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Player.Open", | |
"params":{"item":{"playlistid":1, "position" : 0}}, | |
"id": 1}) | |
elif video_pattern.match(url) and video_pattern.match(url).groups(1)[0]: | |
vid_id = video_pattern.match(url).groups(1)[0] | |
if not queue: | |
kodi_req({ 'jsonrpc' : '2.0', | |
'method' : 'Playlist.Clear', | |
'params': {'playlistid' : 1 }, | |
'id' : 1 | |
}) | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Playlist.Add", | |
"params": {"playlistid":1, | |
"item" : { "file" : "plugin://plugin.video.youtube/?action=play_video&videoid={}".format(vid_id),} | |
}, | |
"id" : 1}) | |
if not queue: | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Player.Open", | |
"params":{"item":{"playlistid":1, "position" : 0}}, | |
"id": 1}) | |
else: | |
print("no match") | |
sys.exit(1) | |
def play_pause(): | |
out = kodi_req( | |
{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99} | |
) | |
player_id = 1 | |
try: | |
player_id = json.loads(str(out,'utf-8'))['result'][0]['playerid'] | |
except json.JSONDecodeError as e: | |
return | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Player.PlayPause", | |
"params": { "playerid": player_id } | |
}) | |
def send_text(text=""): | |
kodi_req({ | |
"jsonrpc": "2.0", | |
"method": "Input.SendText", | |
"params": { "text": text } | |
}) | |
def screenshot(): | |
kodi_req({ "jsonrpc": "2.0", "id": 1, "method": "Input.ExecuteAction", "params": { "action": "screenshot" }}) | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", help="pause", | |
action="store_true") | |
parser.add_argument("-q", help="queue (do not auto play)", | |
action="store_true") | |
parser.add_argument("-y", help="video or playlist url to play", | |
type=str) | |
parser.add_argument("-t", help="send text", | |
type=str) | |
parser.add_argument("-s", help="take a screenshot", | |
action="store_true") | |
args = parser.parse_args() | |
if (args.y): | |
play_youtube(args.y,args.q) | |
elif args.p: | |
play_pause() | |
elif args.t: | |
send_text(args.t) | |
elif args.s: | |
screenshot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment