forked from darkphoenix/tolino-calibre-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tolinoclient.py
executable file
·226 lines (192 loc) · 8.23 KB
/
tolinoclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
# tolino cloud command line client
# call without parameters for a brief help message
# you can use a ".tolinoclientrc" config file
import configparser
import argparse
import json
import sys
import datetime
import logging
from os.path import expanduser
import datetime
from tolinocloud import TolinoCloud
def inventory(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
inv = c.inventory()
c.unregister()
c.logout()
print('{} document{} stored in tolino cloud account {}'.format(len(inv), 's' if len(inv) > 1 else '', args.user))
for i in inv:
print('')
print('id : {}'.format(i['id']))
print('title : {}'.format(i['title']))
print('subtitle : {}'.format(i['subtitle']))
for a in i['author']:
if a:
print('author : {}'.format(a))
print('mimetype : {}'.format(i['mime']))
print('type : {} / {}'.format(i['type'], {
'edata' : 'user storage area',
'ebook' : 'tolino storage area'
}.get(i['type'], i['type'])
))
if 'issued' in i:
print('issued : {}'.format(datetime.datetime.fromtimestamp(i['issued']/1000.0).strftime('%c')))
print('purchased : {}'.format(datetime.datetime.fromtimestamp(i['purchased']/1000.0).strftime('%c')))
print('partner : {} / {}'.format(i['partner'], TolinoCloud.partner_name[i['partner']]))
def devices(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
devs = c.devices()
c.logout()
print('{} device{} connected to tolino cloud account {}'.format(len(devs), 's' if len(devs) > 1 else '', args.user))
for d in devs:
print('')
print('device : {}'.format(d['id']))
print('type : {}'.format(d['type']))
print('name : {}'.format(d['name']))
print('partner : {} / {}'.format(d['partner'], TolinoCloud.partner_name[d['partner']]))
print('registered: {}'.format(datetime.datetime.fromtimestamp(d['registered']/1000.0).strftime('%c')))
print('last use : {}'.format(datetime.datetime.fromtimestamp(d['lastusage']/1000.0).strftime('%c')))
def unregister(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.unregister(args.device_id)
c.logout()
print('unregistered device {} from tolino cloud.'.format(args.device_id))
def upload(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
document_id = c.upload(args.filename, args.name)
c.unregister()
c.logout()
print('uploaded {} to tolino cloud as {}.'.format(args.filename, document_id))
def download(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
fn = c.download(None, args.document_id)
c.unregister()
c.logout()
print('downloaded {} from tolino cloud to {}.'.format(args.document_id, fn))
def delete(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
c.delete(args.document_id)
c.unregister()
c.logout()
print('deleted {} from tolino cloud.'.format(args.document_id))
def meta(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
if args.issued != None:
datetime.datetime.strptime(args.issued, "%d.%m.%Y")
c.metadata(args.document_id, args.title, args.subtitle, args.author, args.publisher, args.isbn, args.edition, args.issued, args.language)
c.unregister()
c.logout()
print('successfully modified book {}'.format(args.document_id))
def cover(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
c.cover(args.document_id, args.image)
c.unregister()
c.logout()
print('successfully modified cover for book {}'.format(args.document_id))
def add_to_collection(args):
global confpath
c = TolinoCloud(args.partner, args.use_device, confpath)
c.login(args.user, args.password)
c.register()
c.add_to_collection(args.document_id, args.collection_name)
c.unregister()
c.logout()
print('successfully modified collections for book {}'.format(args.document_id))
parser = argparse.ArgumentParser(
description='cmd line client to access personal tolino cloud storage space.'
)
parser.add_argument('--config', metavar='FILE', default='~/.tolinoclientrc', help='config file (default: .tolinoclientrc)')
args, remaining_argv = parser.parse_known_args()
confpath = expanduser('~/.tolinoclientrc')
if args.config:
confpath = expanduser(args.config)
c = configparser.ConfigParser(strict=False, interpolation=None)
c.read([expanduser(args.config)])
if c.has_section('Defaults'):
defaults = dict(c.items('Defaults'))
parser.set_defaults(**defaults)
parser.add_argument('--user', type=str, help='username (usually an email address)')
parser.add_argument('--password', type=str, help='password')
parser.add_argument('--partner', type=int, help='shop / partner id (use 0 for list)')
parser.add_argument('--debug', action="store_true", help='log additional debugging info')
parser.add_argument('--use-device', action="store_true", help='use existing device credentials instead of signing in')
subparsers = parser.add_subparsers()
s = subparsers.add_parser('inventory', help='fetch and print inventory')
s.set_defaults(func=inventory)
s = subparsers.add_parser('upload', help='upload a file (must be either .pdf or .epub)')
s.add_argument('filename', metavar='FILE')
s.add_argument('--name', help='specify an alternative name')
s.set_defaults(func=upload)
s = subparsers.add_parser('download', help='download a document')
s.add_argument('document_id')
s.set_defaults(func=download)
s = subparsers.add_parser('delete', help='delete a document (be careful!)')
s.add_argument('document_id')
s.set_defaults(func=delete)
s = subparsers.add_parser('devices', help='list devices registered to cloud account')
s.set_defaults(func=devices)
s = subparsers.add_parser('unregister', help='unregister device from cloud account (be careful!)')
s.add_argument('device_id')
s.set_defaults(func=unregister)
s = subparsers.add_parser('meta', help='set new meta data')
s.add_argument('document_id')
s.add_argument('--title', help='set a new title <string> eg. "Book Title 1"')
s.add_argument('--subtitle', help='set a new subtitle <string> eg. "Subtitle 1"')
s.add_argument('--author', help='set a new author <string> eg. "Hans Muster"')
s.add_argument('--publisher', help='set a new publisher <string> eg. "Carlsen Verlag"')
s.add_argument('--isbn', help='set a new isbn <string> eg."977-3-958-39650-2"')
s.add_argument('--edition', help='set a new edition <int> eg. "3"')
s.add_argument('--issued', help='set a new issued date <int> eg. "01.01.2019" ')
s.add_argument('--language', help='set a new language <string> eg. "en"')
s.set_defaults(func=meta)
s = subparsers.add_parser('cover', help='upload a cover for a specific book')
s.add_argument('document_id')
s.add_argument('image', metavar='IMAGE', help="must be a .png or .jpg")
s.set_defaults(func=cover)
s = subparsers.add_parser('add-to-collection', help='add a book to a collection')
s.add_argument('document_id')
s.add_argument('collection_name')
s.set_defaults(func=add_to_collection)
args = parser.parse_args(remaining_argv)
if args.debug:
logging.basicConfig(level=logging.DEBUG)
if args.partner == 0:
print('List of partner ids available:')
for partner_id in sorted(TolinoCloud.partner_settings.keys()):
print('{} : {}'.format(partner_id, TolinoCloud.partner_name[partner_id]))
sys.exit(1)
if (not args.user) or (not args.password):
print('Login credentials user/password required.')
parser.print_help()
sys.exit(1)
if not hasattr(args, 'func'):
parser.print_help()
sys.exit(1)
user = args.user
password = args.password
partner = args.partner
args.func(args)