Debian Codegraph @

bin/mygpo-bpsync

raw
1 #!/usr/bin/python
2 # BashPodder <-> gpodder.net sync client
3 # Copyright (c) 2010-04-02 Thomas Perl <[email protected]>
4 # Licensed under the same terms as the "mygpoclient" library
5 # See: http://thp.io/2010/mygpoclient/
6
7 from __future__ import print_function
8 import mygpoclient
9 from mygpoclient import simple
10
11 import os
12 import sys
13
14
15 class BPSync(object):
16 def __init__(self, filename, root_url=mygpoclient.ROOT_URL, device='bp'):
17 self.filename = filename
18 self.device = device
19 username = os.environ['MYGPO_USERNAME']
20 password = os.environ['MYGPO_PASSWORD']
21 root_url = os.environ.get('MYGPO_HOSTNAME', root_url)
22 self.client = simple.SimpleClient(username, password, root_url)
23
24 def put(self):
25 lines = open(self.filename).read().strip().splitlines()
26 podcasts = [line.strip() for line in lines if line.strip()]
27 self.client.put_subscriptions(self.device, podcasts)
28
29 def get(self):
30 podcasts = self.client.get_subscriptions(self.device)
31 open(self.filename, 'w').write('\n'.join(podcasts + ['']))
32
33
34 def usage(s=None):
35 print("""
36 Usage: %s (put|get) [device-id]
37
38 e.g %s put ........ Upload bp.conf as device "bp"
39 %s get ........ Download subscriptions for device "bp"
40 %s get mydev .. Download subscriptions for device "mydev"
41
42 The following environment variables need to be set:
43
44 MYGPO_USERNAME .. Username for the web service
45 MYGPO_PASSWORD .. Password for the web service
46
47 The following environment variables are optional:
48
49 BPSYNC_BP_CONF .. Path to your bp.conf file
50 MYGPO_HOSTNAME .. Host or URL of the web service to use
51 """ % ((os.path.basename(sys.argv[0]),) * 4), file=sys.stderr)
52 if s is not None:
53 print(s, file=sys.stderr)
54 sys.exit(1)
55
56
57 if __name__ == '__main__':
58 if len(sys.argv) not in (2, 3):
59 usage('Wrong parameter count')
60
61 if 'MYGPO_USERNAME' not in os.environ or \
62 'MYGPO_PASSWORD' not in os.environ:
63 usage('MYGPO_USERNAME or MYGPO_PASSWORD not set!')
64
65 filename = os.environ.get('BPSYNC_BP_CONF', 'bp.conf')
66
67 if not os.path.exists(filename):
68 usage('File not found: ' + filename)
69
70 command = sys.argv[1]
71
72 if len(sys.argv) == 3:
73 client = BPSync(filename, device=sys.argv[2])
74 else:
75 client = BPSync(filename)
76
77 if command == 'put':
78 client.put()
79 elif command == 'get':
80 client.get()
81 else:
82 usage('Unknown command')