DotaNoobs main site.
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.

73 lines
2.8 KiB

  1. import requests
  2. import re
  3. from time import strptime, strftime, gmtime
  4. from calendar import timegm
  5. from app import app, cache
  6. from teamspeak import create_teamspeak_viewer, getTeamspeakWindow, ISO3166_MAPPING
  7. def get_steam_userinfo(steam_id):
  8. options = {
  9. 'key': app.config['DOTA2_API_KEY'],
  10. 'steamids': steam_id
  11. }
  12. data = requests.get('http://api.steampowered.com/ISteamUser/' \
  13. 'GetPlayerSummaries/v0001/', params=options).json()
  14. return data['response']['players']['player'][0] or {}
  15. # For Templates
  16. @app.template_filter('shorten')
  17. def shorten_filter(s, num_words=20):
  18. space_iter = re.finditer('\s+', s)
  19. output = u''
  20. while num_words > 0:
  21. match = space_iter.next()
  22. if not match: break
  23. output = s[:match.end()]
  24. num_words -= 1
  25. else:
  26. output += '...'
  27. return output
  28. @app.context_processor
  29. def utility_processor():
  30. @cache.memoize(60*5)
  31. def ts3_viewer():
  32. html = create_teamspeak_viewer()[0]
  33. return html
  34. @cache.memoize(60*5)
  35. def ts3_current_clients():
  36. num = create_teamspeak_viewer()[1]
  37. return num
  38. def get_teamspeak_window():
  39. data_list = getTeamspeakWindow()
  40. return data_list
  41. def ts3_active_clients(teamspeak_data):
  42. unique_clients = set()
  43. for data in teamspeak_data:
  44. unique_clients.update(data.clients)
  45. return len(unique_clients)
  46. def num_unique_clients_by_country(teamspeak_data):
  47. unique_clients = {}
  48. for data in teamspeak_data:
  49. for client_id, client_data in data.clients.iteritems():
  50. unique_clients[client_id] = (client_data['country'] or 'Unknown').lower()
  51. country = {}
  52. for client_id, country_code in unique_clients.iteritems():
  53. country[country_code] = country.get(country_code, 0) + 1
  54. return country
  55. def ts3_countries_active(teamspeak_data):
  56. data = num_unique_clients_by_country(teamspeak_data)
  57. return len(data)
  58. def country_abbreviation_mapping():
  59. mapping = {}
  60. for key, name in ISO3166_MAPPING.iteritems():
  61. mapping[key.lower()] = ' '.join([word.capitalize() for word in name.split(' ')])
  62. return mapping
  63. def timestamp_to_js_date(timestamp):
  64. return strftime('%B %d, %Y %H:%M:%S UTC', gmtime(timestamp))
  65. def js_date_to_timestamp(date):
  66. return timegm(strptime(date, '%s, %d %b %Y %H:%M:%S %Z'))
  67. return dict(ts3_viewer=ts3_viewer, ts3_current_clients=ts3_current_clients, get_teamspeak_window=get_teamspeak_window, \
  68. ts3_active_clients=ts3_active_clients, timestamp_to_js_date=timestamp_to_js_date, js_date_to_timestamp=js_date_to_timestamp, \
  69. num_unique_clients_by_country=num_unique_clients_by_country, country_abbreviation_mapping=country_abbreviation_mapping, \
  70. ts3_countries_active=ts3_countries_active)