Add example script: "simple-client"
[mygpoclient.git] / simple-client
blob4762ddee403044ce518a16173bd52aa3e605180f
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # my.gpodder.org API Client
4 # Copyright (C) 2009 Thomas Perl
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.
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.
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/>.
20 import sys
21 import getpass
23 import mygpoclient
25 from mygpoclient import simple
27 def usage():
28 print >>sys.stderr, """
29 Usage: python %s {get|put} {username} {device_id} [host]
30 """ % (sys.argv[0],)
31 sys.exit(1)
33 if __name__ == '__main__':
34 # Use the default host if not specified
35 if len(sys.argv) == 4:
36 sys.argv.append(mygpoclient.HOST)
38 # Check for valid arguments
39 if len(sys.argv) != 5:
40 usage()
42 # Split arguments in local variables
43 progname, subcommand, username, device_id, host = sys.argv
45 # Check for valid subcommand
46 if subcommand not in ('get', 'put'):
47 usage()
49 # Read password from the terminal
50 password = getpass.getpass("%s@%s's password: " % (username, host))
52 # Create the client object with username/password/host set
53 client = simple.SimpleClient(username, password, host)
55 if subcommand == 'get':
56 # Get the URL list and print it, one per line
57 print '\n'.join(client.get_subscriptions(device_id))
58 elif subcommand == 'put':
59 # Read the URL list from standard input and upload it
60 print >>sys.stderr, 'Enter podcast URLs, one per line.'
61 urls = sys.stdin.read().splitlines()
62 if client.put_subscriptions(device_id, urls):
63 print >>sys.stderr, 'Upload successful.'
64 else:
65 print >>sys.stderr, 'Could not upload list.'