diff --git a/CHANGES.txt b/CHANGES.txt index e7adcf1..15fa901 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,2 +1,3 @@ +v0.1.2, 26 July 2014 -- Fixed to work with new Quora UI. v0.1.1, 26 July 2014 -- Readme. -v0.1, 26 July 2014 -- Initial release. \ No newline at end of file +v0.1.0, 26 July 2014 -- Initial release. \ No newline at end of file diff --git a/debug.py b/debug.py index f249a73..f9da470 100644 --- a/debug.py +++ b/debug.py @@ -1,11 +1,12 @@ from quora import Quora, Activity quora = Quora() -activity = quora.get_activity('Christopher-J-Su') +quora.get_user_stats('Christopher-J-Su') +# activity = quora.get_activity('Christopher-J-Su') # print activity.answers # print activity.question_follows -print activity.user_follows +# print activity.user_follows # print activity.upvotes # print activity.questions -print quora.get_activity_keys() \ No newline at end of file +# print quora.get_activity_keys() \ No newline at end of file diff --git a/quora/pyquora.py b/quora/pyquora.py index 84d0bc2..4c0a0dd 100644 --- a/quora/pyquora.py +++ b/quora/pyquora.py @@ -53,6 +53,9 @@ def check_activity_type(description): else: # hopefully. return ACTIVITY_ITEM_TYPES.USER_FOLLOW +def is_new_ui(soup): + return soup.find('div', attrs={'class': 'ProfileTabs'}) is not None + #################################################################### # API #################################################################### @@ -61,32 +64,48 @@ class Quora: @staticmethod def get_user_stats(user): soup = BeautifulSoup(requests.get('http://www.quora.com/' + user).text) - user_dict = { 'username': user } - user_dict['name'] = soup.find('h1').find('span', class_='user').string - - if soup.find('div', class_="empty_area br10 light") is not None: - attributes = ['Followers', 'Following', 'Topics', 'Blogs', 'Posts', 'Questions', 'Answers', 'Reviews', 'Edits'] - - for item in soup.findAll('li', class_="tab #"): - label = item.find('strong').string - if label in attributes: - user_dict[label.lower()] = try_cast(item.find('span').string) - else: - attributes_to_href_suffix = { - 'followers': 'followers', - 'following': 'following', - 'topics': 'topics', - 'blogs': 'blogs', - 'posts': 'all_posts', - 'questions': 'questions', - 'answers': 'answers', - 'edits': 'log' + user_dict = {'username': user} + + if is_new_ui(soup): + classes_to_attributes = { + 'ProfileTabsFollowers': 'followers', + 'ProfileTabsFollowing': 'following', + 'ProfileTabsQuestions': 'questions', + 'ProfileTabsAnswers': 'answers', + 'ProfileTabsPosts': 'posts', + 'ProfileTabsReviews': 'reviews', + 'ProfileTabsOperations': 'edits' } - for attribute, suffix in attributes_to_href_suffix.iteritems(): - try: - user_dict[attribute] = get_count_for_user_href(soup, user, suffix) - except: - pass + for item in soup.find('div', attrs={'class': 'ProfileTabs'}).findAll('li'): + for key in classes_to_attributes.keys(): + if key in item.get("class"): + user_dict[classes_to_attributes[key]] = try_cast(item.find('span').string) + else: + user_dict['name'] = soup.find('h1').find('span', class_='user').string + + if soup.find('div', class_="empty_area br10 light") is not None: + attributes = ['Followers', 'Following', 'Topics', 'Blogs', 'Posts', 'Questions', 'Answers', 'Reviews', 'Edits'] + + for item in soup.findAll('li', class_="tab #"): + label = item.find('strong').string + if label in attributes: + user_dict[label.lower()] = try_cast(item.find('span').string) + else: + attributes_to_href_suffix = { + 'followers': 'followers', + 'following': 'following', + 'topics': 'topics', + 'blogs': 'blogs', + 'posts': 'all_posts', + 'questions': 'questions', + 'answers': 'answers', + 'edits': 'log' + } + for attribute, suffix in attributes_to_href_suffix.iteritems(): + try: + user_dict[attribute] = get_count_for_user_href(soup, user, suffix) + except: + pass return user_dict @staticmethod diff --git a/setup.py b/setup.py index af48f97..0524eae 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='quora', - version='0.1.1', + version='0.1.2', description='Fetches and parses data from Quora.', author='Christopher Su', author_email='christophersu9@gmail.com',