Advertisement
Guest User

kodi_input.py

a guest
Nov 12th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/python
  2. """
  3.    Kodi Input Util
  4.    Copyright (C) 2016 tknorris
  5.  
  6.    This program is free software: you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation, either version 3 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. import urllib2
  20. import json
  21. import base64
  22. import sys
  23.  
  24. DEF_HOSTS = 'hosts.txt'
  25.  
  26. def get_auth_header(user, password):
  27.     auth = base64.b64encode('%s:%s' % (user, password))
  28.     return {'Authorization': 'Basic %s' % (auth)}
  29.    
  30. def send_text(host, user, password, text):
  31.     url = 'http://%s/jsonrpc' % (host)
  32.     data = {'jsonrpc': '2.0', 'method': 'Input.SendText', 'params': {'text': text, 'done': False}, 'id': 1}
  33.     headers = {'Content-Type': 'application/json'}
  34.     headers.update(get_auth_header(user, password))
  35.     req = urllib2.Request(url, data=json.dumps(data), headers=headers)
  36.     res = urllib2.urlopen(req)
  37.     return res.read()
  38.  
  39. def get_host(hosts):
  40.     try:
  41.         rows = []
  42.         with open(hosts) as f:
  43.             for line in f:
  44.                 user, password, host = line.split('|')
  45.                 row = (host.strip(), user.strip(), password.strip())
  46.                 rows.append(row)
  47.  
  48.         pick = 0
  49.         while pick < 1 or pick > len(rows):
  50.             for i, line in enumerate(rows):
  51.                 print '%s: %s:%s@%s' % (i + 1, row[1], row[2], row[0])
  52.             try: pick = int(raw_input('\nTarget Host: '))
  53.             except: pick = 0
  54.         host, user, password = row
  55.     except:
  56.         host = raw_input('Host: ')
  57.         user = raw_input('Username: ')
  58.         password = raw_input('Password: ')
  59.        
  60.     return host, user, password
  61.  
  62. def main(argv=None):
  63.     if sys.argv: argv = sys.argv
  64.     hosts = argv[1] if len(argv) > 1 else DEF_HOSTS
  65.     host, user, password = get_host(hosts)
  66.    
  67.     if len(argv) > 2:
  68.         text = argv[2]
  69.     else:
  70.         text = raw_input('Enter Text: ')
  71.     send_text(host, user, password, text)
  72.    
  73. if __name__ == '__main__':
  74.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement