def friends(self, user_id): if not (self.access_token and self.access_token_secret): self._authorize() page = 1 while True: self.logger.info('On page %s of the friends of user %s' % (page, user_id)) session = OAuth1Session( consumer_key=self.key, consumer_secret=self.secret, access_token=self.access_token, access_token_secret=self.access_token_secret, ) endpoint = 'https://www.goodreads.com/friend/user/%s' % (user_id) params = {'format': 'xml', 'key': self.key, 'page': page} r = session.get(endpoint, params=params) if r.status_code != 200: raise GoodReadsError(r.text) soup = BeautifulSoup(r.content, 'xml') end = int(soup.GoodreadsResponse.friends['end']) total = int(soup.GoodreadsResponse.friends['total']) users = soup.GoodreadsResponse.friends for user_tag in users.find_all('user'): user = User(int(user_tag.id.text)) user.name = user_tag.find('name').text user.link = user_tag.link.text user.friends_count = int(user_tag.friends_count.text) user.reviews_count = int(user_tag.reviews_count.text) try: user.created_at = date_parser.parse(user_tag.created_at.text) except: pass yield user if end >= total: break page += 1
def user(self, user_id): endpoint = 'https://www.goodreads.com/user/show/%s.xml' % (user_id) params = {'key': self.key} r = requests.get(endpoint, params=params) if r.status_code != 200: raise GoodReadsError(r.text) soup = BeautifulSoup(r.content, 'xml') user_tag = soup.user user = User(int(user_tag.id.text)) user.name = user_tag.find('name').text user.link = user_tag.link.text user.friends_count = int(user_tag.friends_count.text) user.reviews_count = int(user_tag.reviews_count.text) return user