Debian Codegraph @

bin/mygpo-simple-client

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