scripts and useful references to track contributions by users.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get_active_moderator.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2016 OpenStack Foundation
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  14. # implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import datetime
  18. import json
  19. import requests
  20. import time
  21. user_list = 'https://ask.openstack.org/en/api/v1/users/'
  22. params = dict(
  23. sort='reputation',
  24. page=1
  25. )
  26. def get_user_data(karma_level):
  27. """
  28. Loop through the user list to find users that have greater karma than
  29. karma level.
  30. Returns a list of user data dicts.
  31. """
  32. page = 1
  33. session = requests.Session()
  34. response = session.get(user_list, params=params)
  35. user_data = json.loads(response.text)['users']
  36. while user_data[-1]['reputation'] >= karma_level:
  37. page = page + 1
  38. params.update({'page': page})
  39. print "Getting page: %d" % page
  40. response = session.get(user_list, params=params)
  41. user_data.extend(json.loads(response.text)['users'])
  42. time.sleep(3)
  43. # since pages are big chunks, we will have some users that are
  44. # having karma lower than karma_level in the last page. Remove them.
  45. while user_data[-1]['reputation'] < karma_level:
  46. user_data.pop()
  47. return user_data
  48. def get_active_users(user_data, last_active_days=180):
  49. """
  50. Give a list of user dict objects, return the ones that
  51. were active within the number of days specificed by
  52. last_active days.
  53. Prints a list of usernames, reputations and IDs
  54. """
  55. now = datetime.datetime.now()
  56. active_threshold = now - datetime.timedelta(days=last_active_days)
  57. for user in user_data:
  58. last_seen_at = datetime.datetime.fromtimestamp(
  59. int(user['last_seen_at']))
  60. if last_seen_at > active_threshold:
  61. print "{: <20} {: <20}".format(user['username'], str(user['id']))
  62. def main():
  63. user_data = get_user_data(karma_level=200)
  64. get_active_users(user_data, last_active_days=180)
  65. if __name__ == "__main__":
  66. main()