-
Notifications
You must be signed in to change notification settings - Fork 0
/
gr.py
executable file
·648 lines (524 loc) · 20.4 KB
/
gr.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# some pieces were borrowed from https://github.com/deis/deis/ client
# specifically this: https://raw.githubusercontent.com/deis/deis/master/client/deis.py
# lots of parts were modeled off https://github.com/GrumpyRainbow/goodreads-py
# could not get the rainbow modules ^^ to work so I just stripped what I needed and put them in here.
'''gr
Usage: gr <command> [<args>...]
Auth commands::
authenticate Set up an authentication
access_tokens Get the tokens to resume the session later
user Get id of user who authorized OAuth.
User commands::
status
comment
event
fan
follower
friends get a list of user's friends
group
list
notification
books get the list of the users books
quote
rate
review
shelf get the list of books from a specific shelf
shelves get the list of the shelves the user has (read, to-read, etc)
authors get a list of the authors that the user has read from infile
Subcommands, use ``gr help [subcommand]`` to learn more::
author Author specific details
book Get info about an author by id.
search
series
topic
Options:
-h --help Show this screen.
--version Show version.
--outfile=FILE Output location
--format=FORMAT Output format
--infile=FILE Some input files - can be anything
NOTE: make sure you set up your environment variables or store the account information locally in
'''
import json
import logging
import requests
import sys
import urllib
import webbrowser
import xmltodict
import httplib2
import os
import xml.etree.ElementTree as ET
from docopt import docopt
from docopt import DocoptExit
from posixpath import expanduser
from dicttoxml import dicttoxml
from termcolor import colored
from client.session import GRSession, GRSessionError
from collections import OrderedDict
from pprint import pprint
from client.response import GRResponse, ResponseFormatter
from ConfigParser import RawConfigParser
__version__ = "0.1.0"
__author__ = "Steven Scott"
__license__ = "MIT"
SHORTCUTS = OrderedDict([
('create', 'apps:create'),
('destroy', 'apps:destroy'),
('whoami', 'auth:whoami'),
])
def _dispatch_cmd(method, args):
logger = logging.getLogger(__name__)
"""
Setting up the common parameters
General items are output of file etc
Formatting.
User Information.
Keys
"""
try:
# a mixed response will come back
response = method(args)
handle_output(response, args)
except EnvironmentError as err:
logger.error(err.args[0])
sys.exit(1)
except ResponseError as err:
resp = err.args[0]
logger.error('{} {}'.format(resp.status_code, resp.reason))
try:
msg = resp.json()
if 'detail' in msg:
msg = "Detail:\n{}".format(msg['detail'])
except:
msg = resp.text
logger.info(msg)
sys.exit(1)
def handle_output(response, args):
logger = logging.getLogger(__name__)
"""
Take the response from the function call and see what we need to do with it.
"""
outfile = args.get('--outfile')
outfile_format = args.get('--format')
if not outfile_format:
outfile_format = 'xml'
logger.info('Outfile format: ' + outfile_format)
rf = ResponseFormatter()
retvals = rf.get_formatted_response(response, outfile_format)
if outfile:
logger.info('Opening the outfile file: ' + outfile)
f = open(outfile, 'w')
logger.info('preppring write')
f.write(retvals.encode('utf-8').strip())
f.close()
return
else:
logger.info('no output file: stdout')
print(retvals)
return retvals
def _init_logger():
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(sys.stdout)
# TODO: add a --debug flag
logger.setLevel(logging.DEBUG)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
def trim(docstring):
"""
Function to trim whitespace from docstring
c/o PEP 257 Docstring Conventions
<http://www.python.org/dev/peps/pep-0257/>
"""
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
class ResponseError(Exception):
pass
class GRClient():
'''Gr Client'''
session = None
config = None
auth_section = 'secrets'
host = "https://www.goodreads.com"
env_file = ""
client_id = ""
client_secret = ""
key = "nPwv45LOlOfy0dry1C7Gcw"
secret = "78pLJTFAsdVvaJDteOD8WoEuwTEW8f3ixN7W1gzWhK4"
def __init__(self):
self._logger = logging.getLogger(__name__)
self.setup_config()
def setup_config(self):
self.config = RawConfigParser()
self.env_file = expanduser('~') + '/.goodreads_api.ini';
self.config.readfp(open(self.env_file))
# self.config.read(self.env_file)
if not self.config.has_section(self.auth_section):
self.config.add_section(self.auth_section)
if not self.config.has_option(self.auth_section, 'ACCESS_TOKEN'):
self.config.set(self.auth_section, 'ACCESS_TOKEN', '')
self.config.set(self.auth_section, 'ACCESS_TOKEN_SECRET', '')
else:
self.key = self.config.get(self.auth_section, 'ACCESS_TOKEN')
self.secret = self.config.get(self.auth_section, 'ACCESS_TOKEN_SECRET')
if not self.config.has_option(self.auth_section, 'CLIENT_ID'):
self.config.set(self.auth_section, 'CLIENT_ID', '')
self.config.set(self.auth_section, 'CLIENT_SECRET', '')
else:
self.client_id = self.config.get(self.auth_section, 'CLIENT_ID')
self.client_secret = self.config.get(self.auth_section, 'CLIENT_SECRET')
self.write_config()
def write_config(self):
with open(self.env_file, 'wb') as configfile:
self.config.write(configfile)
def auth(self, args):
"""
Valid commands for auth:
auth:authenticate authenticate
auth:user Get id of user who authorized OAuth.
Use `gr help [command]` to learn more.
"""
return
def auth_authenticate(self, args):
"""
Go through Open Authentication process.
Usage: gr auth:authenticate [options]
Options:
--client_id=<client_id>
provide a client_id
--client_secret=<client_secret>
provide a client_secret
--access_token=<access_token>
a user access token
--access_token_secret=<access_token_secret>
a user access token secret
"""
self._logger.info("Authenticating client")
self.client_id = args.get('--client_id')
self.client_secret = args.get('--client_secret')
access_token = args.get('--access_token')
# was the key loaded from config
if not access_token and self.key:
access_token = self.key
access_token_secret = args.get('--access_token_secret')
if not access_token_secret and self.secret:
access_token_secret = self.secret
if access_token:
self._logger.info("Can resume with access_token: " + access_token + " AND access_token_secret: " + access_token_secret)
self.session = GRSession(self.client_id,
self.client_secret,
access_token,
access_token_secret)
if access_token and access_token_secret:
self._logger.info("Resuming Session")
self.session.oauth_resume()
else: # Access not yet granted, allow via browser
self._logger.info("Getting OAuth")
url = self.session.oauth_start()
webbrowser.open(url)
while raw_input('Have you authorized me? (y/n) ') != 'y':
pass
self.session.oauth_finish()
self.auth_access_tokens(args)
self._logger.info("AccessToken: " + self.session.access_token)
self._logger.info("AccessTokenSecret: " + self.session.access_token_secret)
self.config.set(self.auth_section, 'ACCESS_TOKEN', self.session.access_token)
self.config.set(self.auth_section, 'ACCESS_TOKEN_SECRET', self.session.access_token_secret)
self.write_config()
def auth_access_tokens(self, args):
"""
Return access tokens for storage, so that sessions can be
resumed easily.
Returns: (access_token, access_token_secret)
"""
if not self.session:
self._logger.info(self.session)
raise GRSessionError("No authenticated session.")
return self.session.access_token, self.session.access_token_secret
def auth_user(self, args):
"""
Get id of user who authorized OAuth.
"""
self._logger.info(self.session)
self._logger.info("in user")
self.auth_authenticate(args)
if not self.session:
raise GRSessionError("No authenticated session.")
data_dict = self.session.get(self.host + '/api/auth_user', {'format':'xml'})
# Parse response
user_id = data_dict['user']['@id']
name = data_dict['user']['name']
# todo make this prettier
self._logger.info(data_dict)
return user_id, name
def author(self, args):
"""
Valid commands for author:
author:info Get info about an author by id.
author:books Paginate an author's books.
Use `gr help [command]` to learn more.
"""
return
def author_info(self, args):
"""
Show the authors information page
Usage: gr author:info [options]
Options:
-a --author_id=<author_id> Author ID
--outfile=FILE Output location
--format=FORMAT Output format
"""
self._logger.info(args)
author_id = args.get('--author_id')
if not author_id:
raise Exception("--author_id needed ")
payload = {'id': author_id, 'key': self.client_id }
self._logger.info(payload)
r = requests.get(self.host + "/author/show.xml", params=payload)
return r
def author_books(self, args):
"""
Get the list of books from this author
"""
return
def user(self, args):
"""
Valid commands for author:
user:info Get info about an author by id.
user:books Paginate a users book list
user:friends Get a list of the user's friends
user:shelves Get the user's shelves
user:shelf Get the list of books from a specific shelf
user:authors Get the authors for this user's read books - OFFLINE OP
Use `gr help [command]` to learn more.
"""
return
def user_books(self, args):
"""
Show the users list of books
Usage: gr user:books [options]
Options:
-u --user_id=<user_id> GR user id
--outfile=FILE Output location
--format=FORMAT Output format
"""
self.auth_authenticate(args)
if not self.session:
raise GRSessionError("No authenticated session.")
user_id = args.get('--user_id')
if not user_id:
raise Exception("--user_id needed ")
payload = {'id': user_id, 'key': self.client_id, 'format': 'xml' }
self._logger.info(payload)
return requests.get(self.host + "/owned_books/user", params=payload)
def user_friends(self, args):
"""
Show the users list of books
Usage: gr user:friends [options]
Options:
-u --user_id=<user_id> GR user id
--outfile=FILE Output location
--format=FORMAT Output format
"""
self._logger.info(args)
return
self.auth_authenticate(args)
if not self.session:
raise GRSessionError("No authenticated session.")
# print(args)
user_id = args.get('--user_id')
if not user_id:
raise Exception("--user_id needed ")
payload = {'id': user_id, 'key': self.client_id, 'format': 'xml' }
return self.session.get(self.host + "/friend/user/" + user_id, payload)
def user_shelves(self, args):
"""
Show the users list of shelves
Usage: gr user:shelves [options]
Options:
-u --user_id=<user_id> GR user id
--outfile=FILE Output location
--format=FORMAT Output format
"""
self.auth_authenticate(args)
if not self.session:
raise GRSessionError("No authenticated session.")
# print(args)
user_id = args.get('--user_id')
if not user_id:
raise Exception("--user_id needed ")
payload = {'id': user_id, 'key': self.client_id, 'format': 'xml' }
return self.session.get(self.host + "/shelf/list/" + user_id, payload)
def user_shelf(self, args):
"""
Show the users list of books on a shelf
Usage: gr user:shelf [options]
Options:
-u --user_id=<user_id> GR user id
-s --shelf=<shelf> [default: 'read']
--sort=<sort> title, author, cover, rating, year_pub, date_pub, date_pub_edition,
date_started, date_read, date_updated, date_added, recommender,
avg_rating, num_ratings, review, read_count, votes, random
--search=<query> query to search through a users list of books
--order=<order> a, d (optional)
--page=<n> 1-N (optional)
--per_page=<n> 1-200 (optional)
--outfile=FILE Output location
--format=FORMAT Output format
"""
self.auth_authenticate(args)
if not self.session:
raise GRSessionError("No authenticated session.")
user_id = args.get('--user_id')
if not user_id:
raise Exception("--user_id needed ")
shelf = args.get('--shelf')
sort = args.get('--sort')
search = args.get('--search')
order = args.get('--order')
page = args.get('--page')
per_page = args.get('--per_page')
payload = {'id': user_id, 'key': self.client_id, 'format': 'xml',
'v': 2, 'shelf': shelf, 'order': order,
'sort': sort, 'page': page, 'per_page': per_page,
'search': search
}
return self.session.get(self.host + "/review/list/" + user_id, payload)
def user_authors(self, args):
"""
Show or parse the users list of read authors.
OFFLINE operation - must have run user:shelf to get theset values
This must be run after the user:shelf command has created the downloaded list of books
Output can be either JSON CSV or XML
Usage: gr user:authors [options]
Options:
-u --user_id=<user_id> GR user id
-s --shelf=<shelf> [default: 'read']
--infile=FILE Input file - might be list of reviews already downloaded
--outfile=FILE Output location
--format=FORMAT Output format
"""
self._logger.info('users:authors - starting ')
infile = args.get('--infile')
if not infile:
raise Exception('This is an offline operation - profide a file --infile downloaded with user:shelf ')
if not os.path.isfile(infile):
raise Exception('Invalid file ' + infile)
tree = ET.parse(infile)
root = tree.getroot()
authroot = {}
for author in root.iter('author'):
aid = int(author.find('id').text)
authroot[aid] = {
'name': author.find('name').text.strip('\n'),
'image_url': author.find('image_url').text.strip('\n'),
'small_image_url': author.find('small_image_url').text.strip('\n'),
'link': author.find('link').text.strip('\n')
}
return GRResponse(json.dumps(authroot), 'json')
class GRRequestError(Exception):
""" Custom request exception """
def __init__(self, error_msg, url):
self.error_msg = error_msg
self.url = url
def __str__(self):
return self.error_msg + "\n" + self.url
class GRRequest:
""" Handles the goodreads requests and response parsing """
def __init__(self, path, additional_query_info, client_instance):
""" """
self.query_dict = dict(client_instance.query_dict.items() + additional_query_info.items())
self.host = client_instance.host
self.path = path
# Will there be parameters?
if len(self.query_dict) > 0:
self.path += '?'
def request(self, return_raw=False):
""" """
h = httplib2.Http('.cache')
url_extension = self.path + urllib.urlencode(self.query_dict)
response, content = h.request(self.host + url_extension, "GET")
# Check success
if response['status'] != '200':
raise GRRequestError(response['status'], url_extension)
return
# Some responses aren't xml structured (see get_book_id)
if return_raw:
return content
# Parse response into dictionary
data_dict = xmltodict.parse(content)
if data_dict.has_key('error'):
raise GRRequestError(data_dict['error'], url_extension)
return data_dict['GRResponse']
def parse_args(cmd):
"""
Parses command-line args applying shortcuts and looking for help flags.
"""
print("CMD: " + cmd)
if cmd == 'help':
cmd = sys.argv[-1]
help_flag = True
else:
cmd = sys.argv[1]
help_flag = False
# swap cmd with shortcut
if cmd in SHORTCUTS:
cmd = SHORTCUTS[cmd]
# change the cmdline arg itself for docopt
if not help_flag:
sys.argv[1] = cmd
else:
sys.argv[2] = cmd
# convert : to _ for matching method names and docstrings
if ':' in cmd:
cmd = '_'.join(cmd.split(':'))
return cmd, help_flag
def main():
'''Main entry point for the gr CLI.'''
_init_logger()
cli = GRClient()
args = docopt(__doc__, version=__version__, options_first=True)
cmd = args['<command>']
cmd, help_flag = parse_args(cmd)
# print help if it was asked for
if help_flag:
if cmd != 'help' and cmd in dir(cli):
print(trim(getattr(cli, cmd).__doc__))
return
docopt(__doc__, argv=['--help'])
# unless cmd needs to use sys.argv directly
if hasattr(cli, cmd):
method = getattr(cli, cmd)
else:
raise DocoptExit('Found no matching command, try `gr help`')
# re-parse docopt with the relevant docstring
docstring = trim(getattr(cli, cmd).__doc__)
if 'Usage: ' in docstring:
args.update(docopt(docstring))
# dispatch the CLI command
_dispatch_cmd(method, args)
if __name__ == '__main__':
main()