Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from flask_jwt_extended import create_access_token 

2import json 

3 

4from settings import GBOOKS_TOKEN_URI, GOOGLE_OAUTH_FILE, GBOOKS_SCOPES, GBOOKS_REDIRECT_URL 

5from googleapiclient.discovery import build 

6from google_auth_oauthlib.flow import Flow 

7from google.auth.transport.requests import Request 

8import google.oauth2.credentials 

9 

10 

11def credentials_to_dict(credentials): 

12 return {'token': credentials.token, 

13 'refresh_token': credentials.refresh_token, 

14 'token_uri': credentials.token_uri, 

15 'client_id': credentials.client_id, 

16 'client_secret': credentials.client_secret, 

17 'scopes': credentials.scopes} 

18 

19 

20class GBooks: 

21 @staticmethod 

22 def oauth_url(user): 

23 access_token = create_access_token(identity=user) 

24 flow = Flow.from_client_secrets_file(GOOGLE_OAUTH_FILE, GBOOKS_SCOPES) 

25 flow.redirect_uri = GBOOKS_REDIRECT_URL 

26 url, _ = flow.authorization_url( 

27 access_type='offline', 

28 include_granted_scopes='true', 

29 state=access_token 

30 ) 

31 return url 

32 

33 @staticmethod 

34 def get_token(code, state): 

35 flow = Flow.from_client_secrets_file( 

36 GOOGLE_OAUTH_FILE, GBOOKS_SCOPES, state=state) 

37 flow.redirect_uri = GBOOKS_REDIRECT_URL 

38 # Use the authorization server's response to fetch the OAuth 2.0 tokens. 

39 flow.fetch_token(code=code) 

40 

41 credentials = flow.credentials 

42 return credentials_to_dict(credentials) 

43 

44 @staticmethod 

45 def get_data(token, refresh_token): 

46 rating = { 

47 "ONE": 1, 

48 "TWO": 2, 

49 "THREE": 3, 

50 "FOUR": 4, 

51 "FIVE": 5 

52 } 

53 # Load credentials from the session. 

54 with open(GOOGLE_OAUTH_FILE) as json_file: 

55 data = json.load(json_file) 

56 credentials = google.oauth2.credentials.Credentials( 

57 token=token, 

58 refresh_token=refresh_token, 

59 token_uri=data['web']['token_uri'], 

60 client_id=data['web']['client_id'], 

61 client_secret=data['web']['client_secret'], 

62 scopes=GBOOKS_SCOPES 

63 ) 

64 try: 

65 credentials.refresh(Request()) 

66 except Exception: 

67 return "revoked" 

68 books = build('books', 'v1', credentials=credentials) 

69 bookshelves = books.mylibrary().bookshelves().list().execute() 

70 excludedShelves = ['To read', 'Books for you', 

71 'Browsing history', 'Recently viewed'] 

72 data = [] 

73 # return bookshelves 

74 shelf_id = [] 

75 for bs in bookshelves['items']: 

76 if bs["title"] not in excludedShelves and "volumeCount" in bs.keys() and bs['volumeCount'] != 0: 

77 if bs['title'] == "Reviewed": 

78 shelf_id.insert(0, bs['id']) 

79 else: 

80 shelf_id.append(bs['id']) 

81 for id in shelf_id: 

82 for user_bk in books.mylibrary().bookshelves().volumes().list(shelf=str(id), projection='FULL').execute()["items"]: 

83 # return bk # ! change shelf to bs['id'] 

84 

85 # ? if get description take same link as "publisher" 

86 """ bk = books.volumes().get(volumeId=str('0i_BjgEACAAJ')).execute() 

87 return bk """ 

88 if ("industryIdentifiers" in user_bk['volumeInfo'].keys()): 

89 for isbn in user_bk["volumeInfo"]["industryIdentifiers"]: 

90 if isbn['type'] == "ISBN_13": 

91 element = {} 

92 if "userInfo" in user_bk.keys(): 

93 if "review" in user_bk['userInfo'].keys() and "rating" in user_bk['userInfo']['review'].keys() and user_bk['userInfo']['review']['rating'] != "NOT_RATED": 

94 element['user_rating'] = rating[user_bk['userInfo'] 

95 ['review']['rating']] 

96 else: 

97 element['user_rating'] = None 

98 if "isPurchased" in user_bk['userInfo'].keys(): 

99 element['purchase'] = user_bk['userInfo']["isPurchased"] 

100 else: 

101 element['purchase'] = None 

102 bk = books.volumes().get( 

103 volumeId=str(user_bk['id'])).execute() 

104 element["isbn"] = isbn['identifier'] 

105 element["title"] = bk['volumeInfo']['title'] 

106 element["author"] = bk['volumeInfo']['authors'][0] if (isinstance( 

107 bk['volumeInfo']['authors'], list) and len(bk['volumeInfo']['authors']) > 0) else None 

108 element["year_of_publication"] = bk['volumeInfo']['publishedDate'][:4] 

109 element["publisher"] = bk['volumeInfo']['publisher'] 

110 if "imageLinks" in bk["volumeInfo"].keys(): 

111 if "small" in bk['volumeInfo']['imageLinks'].keys() and "medium" in bk['volumeInfo']['imageLinks'].keys() and "large" in bk['volumeInfo']['imageLinks'].keys(): 

112 element["image_url_s"] = bk['volumeInfo']['imageLinks']['small'] 

113 element["image_url_m"] = bk['volumeInfo']['imageLinks']['medium'] 

114 element["image_url_l"] = bk['volumeInfo']['imageLinks']['large'] 

115 else: 

116 try: 

117 img = bk['volumeInfo']['imageLinks'] 

118 element["image_url_s"] = next( 

119 iter(img.values())) 

120 element["image_url_m"] = next( 

121 iter(img.values())) 

122 element["image_url_l"] = next( 

123 iter(img.values())) 

124 except: 

125 element["image_url_s"] = None 

126 element["image_url_m"] = None 

127 element["image_url_l"] = None 

128 else: 

129 element["image_url_s"] = None 

130 element["image_url_m"] = None 

131 element["image_url_l"] = None 

132 if "averageRating" in bk['volumeInfo'].keys(): 

133 element["rating"] = bk['volumeInfo']['averageRating'] 

134 element["rating_count"] = bk['volumeInfo']['ratingsCount'] 

135 else: 

136 element["rating"] = None 

137 element["rating_count"] = None 

138 data.append(element) 

139 return data