lucjon / Py-StackExchange

A Python binding for the StackExchange API
http://stackapps.com/questions/198/py-stackexchange-an-api-wrapper-for-python
BSD 3-Clause "New" or "Revised" License
229 stars 76 forks source link

How to fetch up_vote_count, down_vote_count and view_count from user? #65

Closed andregasser closed 7 years ago

andregasser commented 7 years ago

First things first: Thank you very much for your efforts in building this Python package. It saves me lots of hours for sure. Keep up the good work.

Nevertheless, I have a small issue when fetching data from the Stackoverflow site. I try to load a specific user object and would like to get reputation as well as up vote count, down vote count and view count. Fetching reputation works well, but when I try to access the other attributes, such as up_vote_count, I fail.

Here is my code so far:

    so = stackexchange.Site(stackexchange.StackOverflow)
    so_user_obj = so.user(so_user_id)
    so_user_obj.badges.fetch()
    so_user_obj.questions.fetch()
    so_user_obj.answers.fetch()
    user_dict['reputation'] = so_user_obj.reputation
    user_dict['questions_asked'] = len(so_user_obj.questions)
    user_dict['answers_posted'] = len(so_user_obj.answers)
    user_dict['badges_earned'] = len(so_user_obj.badges)
    user_dict['up_votes_received'] = so_user_obj.up_vote_count
    user_dict['down_votes_received'] = so_user_obj.down_vote_count
    user_dict['profile_views'] = so_user_obj.view_count

When running the code, I can fetch reputation successfully, but get this error for the other attribute:

    Traceback (most recent call last):
      File "./stacktrack.py", line 405, in <module>
        user_data = so_get_user_obj_as_dict(so_user_obj)
      File "./stacktrack.py", line 193, in so_get_user_obj_as_dict
        user_dict['up_votes_received'] = so_user_obj.up_vote_count
    AttributeError: 'User' object has no attribute 'up_vote_count'

I actually thought, that up_vote_count, down_vote_count and view_count are part of the response, because of this code in models.py (~ line 342):

    class User(JSONModel):
        """Describes a user on a StackExchange site."""
        transfer = ('display_name', 'profile_image', 'age', 'website_url',
            'location', 'about_me', 'view_count', 'up_vote_count',
            'down_vote_count', 'account_id', 'profile_image',
            ('creation_date', UNIXTimestamp),
            ('last_access_date', UNIXTimestamp),
            ('reputation', FormattedReputation),
            ...

What am I doing wrong here?

I use Python 3.4.3 with py-stackexchange 2.2.7 (installed via pip3).

Any help highly appreciated!

Thank you André

andregasser commented 7 years ago

This is also described on StackApps: https://stackapps.com/a/7649/49901 (sorry, for cross-posting, but didn't know where you look for / answer issues with the wrapper. ;-)

andregasser commented 7 years ago

Hi, I was finally able to solve it myself. See this link: https://stackapps.com/a/7650/49901