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.

229 lines
9.0 KiB

  1. from flask import Flask
  2. from flask.ext.sqlalchemy import SQLAlchemy
  3. from flask.ext.openid import OpenID
  4. from flask.ext.cache import Cache
  5. app = Flask(__name__)
  6. app.config.from_object('config')
  7. db = SQLAlchemy(app)
  8. oid = OpenID(app)
  9. cache = Cache(app, config={'CACHE_TYPE': app.config['CACHE_TYPE']})
  10. from app import views
  11. '''
  12. from flask import Flask, render_template
  13. from flask.ext.mongoengine import MongoEngine
  14. from flask.ext.openid import OpenID
  15. from flask.ext.cache import Cache
  16. import utils
  17. import ts3
  18. app = Flask(__name__)
  19. app.config.from_object('config')
  20. #Setup mongo database
  21. db = MongoEngine(app)
  22. #Setup OpenID and Caching
  23. oid = OpenID(app)
  24. cache = Cache(app, config={'CACHE_TYPE': app.config['CACHE_TYPE']})
  25. from app import views
  26. @app.route('/')
  27. def inx():
  28. return render_template('main.html')
  29. ##### INTO UTILS LATER #####
  30. RADIANT_TEAM = 2
  31. DIRE_TEAM = 3
  32. RADIANT_COLOR = 'b'
  33. DIRE_COLOR = 'r'
  34. def get_hero_data():
  35. xhr = urllib2.build_opener().open(urllib2.Request("https://api.steampowered.com/IEconDOTA2_570/GETHeroes/v0001/?key="+DOTA2_API_KEY+"&language=en_us"))
  36. data = json.load(xhr)
  37. return data
  38. @app.context_processor
  39. def utility_processor():
  40. @cache.memoize(60*5)
  41. def ts3_viewer():
  42. try:
  43. server = ts3.TS3Server(app.config['TS3_HOST'], app.config['TS3_PORT'])
  44. server.login(app.config['TS3_USERNAME'], app.config['TS3_PASSWORD'])
  45. server.use(1)
  46. serverinfo = server.send_command('serverinfo').data
  47. channellist = server.send_command('channellist', opts=("limits", "flags", "voice", "icon")).data
  48. clientlist = server.send_command('clientlist', opts=("away", "voice", "info", "icon", "groups", "country")).data
  49. servergrouplist = server.send_command('servergrouplist').data
  50. channelgrouplist = server.send_command('channelgrouplist').data
  51. soup = BeautifulSoup()
  52. div_tag = soup.new_tag('div')
  53. div_tag['class'] ='devmx-webviewer'
  54. soup.append(div_tag)
  55. def construct_channels(parent_tag, cid):
  56. num_clients = 0
  57. for channel in channellist:
  58. if int(channel['pid']) == int(cid):
  59. # Construct the channel
  60. channel_tag = soup.new_tag('div')
  61. channel_tag['class'] = 'tswv-channel'
  62. # Channel image
  63. image_tag = soup.new_tag('span')
  64. image_tag['class'] = 'tswv-image tswv-image-right'
  65. if int(channel['channel_flag_password']) == 1:
  66. image_tag['class'] += ' tswv-channel-password-right'
  67. if int(channel['channel_flag_default']) == 1:
  68. image_tag['class'] += ' tswv-channel-home'
  69. if int(channel['channel_needed_talk_power']) > 0:
  70. image_tag['class'] += ' tswv-channel-moderated'
  71. if int(channel['channel_icon_id']) != 0:
  72. raise NotImplementedError
  73. image_tag.append(' ')
  74. channel_tag.append(image_tag)
  75. # Status image
  76. status_tag = soup.new_tag('span')
  77. status_tag['class'] = 'tswv-image'
  78. if int(channel['channel_flag_password']) == 1:
  79. status_tag['class'] += ' tswv-channel-password'
  80. elif int(channel['total_clients']) == int(channel['channel_maxclients']):
  81. status_tag['class'] += ' tswv-channel-full'
  82. else:
  83. status_tag['class'] += ' tswv-channel-normal'
  84. status_tag.append(' ')
  85. channel_tag.append(status_tag)
  86. # Label
  87. label_tag = soup.new_tag('span')
  88. label_tag['class'] = 'tswv-label'
  89. label_tag.append(channel['channel_name'])
  90. channel_tag.append(label_tag)
  91. # Clients
  92. channel_tag, channel_clients = construct_clients(channel_tag, channel['cid'])
  93. # Recurse through sub-channels, collecting total number of clients as we go
  94. channel_tag, sub_clients = construct_channels(channel_tag, channel['cid'])
  95. channel_clients += sub_clients
  96. # Only show non-empty channels
  97. if channel_clients > 0:
  98. parent_tag.append(channel_tag)
  99. num_clients += channel_clients
  100. return parent_tag, num_clients
  101. def construct_clients(parent_tag, cid):
  102. num_clients = 0
  103. for client in clientlist:
  104. if int(client['cid']) == int(cid):
  105. # Skip ServerQuery clients
  106. if int(client['client_type']) == 1: continue
  107. num_clients += 1
  108. client_tag = soup.new_tag('div')
  109. client_tag['class'] = 'tswv-client'
  110. # Status image
  111. status_tag = soup.new_tag('span')
  112. status_tag['class'] = 'tswv-image'
  113. if int(client['client_type']) == 1:
  114. status_tag['class'] += ' tswv-client-query'
  115. elif int(client['client_away']) == 1:
  116. status_tag['class'] += " tswv-client-away"
  117. elif int(client['client_input_muted']) == 1:
  118. status_tag['class'] += " tswv-client-input-muted"
  119. elif int(client['client_output_muted']) == 1:
  120. status_tag['class'] += " tswv-client-output-muted"
  121. elif int(client['client_input_hardware']) == 0:
  122. status_tag['class'] += " tswv-client-input-muted-hardware"
  123. elif int(client['client_output_hardware']) == 0:
  124. status_tag['class'] += " tswv-client-output-muted-hardware"
  125. elif (int(client['client_flag_talking']) == 1) and (int(client['client_is_channel_commander']) == 1):
  126. status_tag['class'] += " tswv-client-channel-commander-talking"
  127. elif int(client['client_is_channel_commander']) == 1:
  128. status_tag['class'] += " tswv-client-channel-commander"
  129. elif int(client['client_flag_talking']) == 1:
  130. status_tag['class'] += " tswv-client-talking"
  131. else:
  132. status_tag['class'] += " tswv-client-normal"
  133. status_tag.append(' ')
  134. client_tag.append(status_tag)
  135. # Country image
  136. country_tag = soup.new_tag('span')
  137. country_tag['class'] = 'tswv-image tswv-image-right'
  138. country_tag['title'] = ' '.join([word.capitalize() for word in utils.ISO3166_MAPPING[client['client_country']].split(' ')])
  139. country_tag['style'] = 'background: url("%s") center center no-repeat;' % url_for('static', filename='img/ts3_viewer/countries/%s.png' % client['client_country'].lower())
  140. country_tag.append(' ')
  141. client_tag.append(country_tag)
  142. # Server group images
  143. sgids = [int(sg) for sg in client['client_servergroups'].split(',')]
  144. servergroups = [servergroup for servergroup in servergrouplist if int(servergroup['sgid']) in sgids]
  145. servergroups.sort(key=operator.itemgetter('sortid'))
  146. for servergroup in servergroups:
  147. if not servergroup['iconid']: continue
  148. img_fname = 'img/ts3_viewer/%s.png' % servergroup['iconid']
  149. if not os.path.exists(os.path.join(app.static_folder, img_fname)):
  150. continue
  151. image_tag = soup.new_tag('span')
  152. image_tag['class'] = 'tswv-image tswv-image-right'
  153. image_tag['title'] = servergroup['name']
  154. image_tag['style'] = 'background-image: url("%s")' % url_for('static', filename=img_fname)
  155. image_tag.append(' ')
  156. client_tag.append(image_tag)
  157. # Check if client is in a moderated channel
  158. channel = [channel for channel in channellist if int(channel['cid']) == int(client['cid'])][0]
  159. if int(channel['channel_needed_talk_power']) > 0:
  160. status_tag = soup.new_tag('span')
  161. status_tag['class'] = 'tswv-image tswv-image-right'
  162. if int(client['client_is_talker']) == 0:
  163. status_tag['class'] += ' tswv-client-input-muted'
  164. else:
  165. status_tag['class'] += ' tswv-client-talkpower-granted'
  166. status_tag.append(' ')
  167. client_tag.append(status_tag)
  168. # Label
  169. label_tag = soup.new_tag('span')
  170. label_tag['class'] = 'tswv-label'
  171. label_tag.append(client['client_nickname'])
  172. client_tag.append(label_tag)
  173. parent_tag.append(client_tag)
  174. return parent_tag, num_clients
  175. div_tag, num_clients = construct_channels(div_tag, 0)
  176. return soup.prettify()
  177. except Exception as inst:
  178. return "error: %s" % inst
  179. def shorten_text(text, num_words=10):
  180. text = utils.fix_bad_unicode(text)
  181. space_iter = re.finditer('\s+', text)
  182. output = u''
  183. while num_words > 0:
  184. match = space_iter.next()
  185. if not match: break
  186. output = text[:match.end()]
  187. num_words -= 1
  188. else:
  189. output += '...'
  190. return output
  191. def num_unique_clients(teamspeak_data):
  192. unique_clients = set()
  193. for data in teamspeak_data:
  194. unique_clients.update(data.clients)
  195. return len(unique_clients)
  196. def num_unique_clients_by_country(teamspeak_data):
  197. unique_clients = {}
  198. for data in teamspeak_data:
  199. for client_id, client_data in data.clients.iteritems():
  200. unique_clients[client_id] = (client_data['country'] or 'Unknown').lower()
  201. country = {}
  202. for client_id, country_code in unique_clients.iteritems():
  203. country[country_code] = country.get(country_code, 0) + 1
  204. return country
  205. def country_abbreviation_mapping():
  206. mapping = {}
  207. for key, name in utils.ISO3166_MAPPING.iteritems():
  208. mapping[key.lower()] = ' '.join([word.capitalize() for word in name.split(' ')])
  209. return mapping
  210. return dict(timestamp_to_js_date=utils.timestamp_to_js_date, ts3_viewer=ts3_viewer, shorten_text=shorten_text, getTeamspeakWindow=doob.getTeamspeakWindow,
  211. num_unique_clients=num_unique_clients,
  212. num_unique_clients_by_country=num_unique_clients_by_country,
  213. country_abbreviation_mapping=country_abbreviation_mapping)
  214. '''