Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- """
- Kodi Input Util
- Copyright (C) 2016 tknorris
- 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 urllib2
- import json
- import base64
- import sys
- DEF_HOSTS = 'hosts.txt'
- def get_auth_header(user, password):
- auth = base64.b64encode('%s:%s' % (user, password))
- return {'Authorization': 'Basic %s' % (auth)}
- def send_text(host, user, password, text):
- url = 'http://%s/jsonrpc' % (host)
- data = {'jsonrpc': '2.0', 'method': 'Input.SendText', 'params': {'text': text, 'done': False}, 'id': 1}
- headers = {'Content-Type': 'application/json'}
- headers.update(get_auth_header(user, password))
- req = urllib2.Request(url, data=json.dumps(data), headers=headers)
- res = urllib2.urlopen(req)
- return res.read()
- def get_host(hosts):
- try:
- rows = []
- with open(hosts) as f:
- for line in f:
- user, password, host = line.split('|')
- row = (host.strip(), user.strip(), password.strip())
- rows.append(row)
- pick = 0
- while pick < 1 or pick > len(rows):
- for i, line in enumerate(rows):
- print '%s: %s:%s@%s' % (i + 1, row[1], row[2], row[0])
- try: pick = int(raw_input('\nTarget Host: '))
- except: pick = 0
- host, user, password = row
- except:
- host = raw_input('Host: ')
- user = raw_input('Username: ')
- password = raw_input('Password: ')
- return host, user, password
- def main(argv=None):
- if sys.argv: argv = sys.argv
- hosts = argv[1] if len(argv) > 1 else DEF_HOSTS
- host, user, password = get_host(hosts)
- if len(argv) > 2:
- text = argv[2]
- else:
- text = raw_input('Enter Text: ')
- send_text(host, user, password, text)
- if __name__ == '__main__':
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement