fork download
  1. # -*- coding: utf-8 -*-
  2.  
  3. from HTMLParser import HTMLParser
  4. from htmlentitydefs import name2codepoint
  5. import codecs, re, pprint
  6.  
  7. def get_bar(lhs, rhs, bar_len, mid_str_fmt):
  8. dis_chr = u'\u25a0'
  9. emp_chr = u' '
  10. ls = emp_chr * (bar_len - lhs) + dis_chr * int(lhs)
  11. rs = dis_chr * rhs + emp_chr * (bar_len - rhs)
  12.  
  13. return u'{0:>{1}}'.format(ls, bar_len) + \
  14. mid_str_fmt.format(lhs, rhs) + \
  15. u'{0:<{1}}'.format(rs, bar_len)
  16.  
  17. class MyHTMLParser(HTMLParser):
  18. def __init__(self):
  19. HTMLParser.__init__(self)
  20. self.match_commentary = ''
  21.  
  22. def handle_data(self, data):
  23. if 'commentaryUpdater' in data:
  24. temp = data[data.find('commentaryUpdater.load([[')+25:]
  25. temp = temp[temp.find('[['):temp.find(']]')+1]
  26. self.match_commentary = re.sub('\[,+', '[', temp)
  27.  
  28.  
  29. match_id = raw_input("Enter match id: ")
  30. with codecs.open(match_id, 'r') as f:
  31. s = f.read()
  32.  
  33. parser = MyHTMLParser()
  34. parser.feed(s)
  35.  
  36. fmt = '%-6s|%-15s|%-62s|%s'
  37.  
  38. match_commentary_list = [c[:-2] for c in eval(parser.match_commentary)
  39. if c[0][0].isdigit()]
  40.  
  41. for comment in match_commentary_list:
  42. if len(comment[2]) > 60:
  43. comment[2] = comment[2][:60]
  44. print fmt % tuple(comment)
  45.  
  46.  
  47. attempt_count = [0,0]
  48. yellow_count = [0,0]
  49. red_count = [0,0]
  50. goal_count = [0,0]
  51. penalty_count = [0,0]
  52. foul_count = [0,0]
  53. offside_count = [0,0]
  54. corner_count = [0,0]
  55. ontarget_count = [0,0]
  56. offtarget_count = [0,0]
  57. blocked_count = [0,0]
  58. owngoal_count = [0,0]
  59. sub_count = [0,0]
  60. hitpost_count = [0,0]
  61.  
  62. first_attempt = ('','')
  63. first_yellow = ('','')
  64. first_red = ('','')
  65. first_goal = ('','')
  66. first_penalty = ('','')
  67. first_foul = ('','')
  68. first_offside = ('','')
  69. first_corner = ('','')
  70. first_ontarget = ('','')
  71. first_offtarget = ('','')
  72. first_blocked = ('','')
  73. first_owngoal = ('','')
  74. first_sub = ('','')
  75. first_hitpost = ('','')
  76.  
  77. team = ()
  78.  
  79. for comment in match_commentary_list:
  80. if 'end 2' in comment[1]:
  81. team = re.findall(r'ends, (.+) [0-9]+, (.+) [0-9]+\.', comment[2])[0]
  82. elif 'red' in comment[1] or 'secondyellow' in comment[1]:
  83. first_red = comment[-1],comment[0]
  84. if comment[-1]=='home': red_count[0] += 1
  85. else: red_count[1] += 1
  86. elif 'yellow' in comment[1]:
  87. first_yellow = comment[-1],comment[0]
  88. if comment[-1]=='home': yellow_count[0] += 1
  89. else: yellow_count[1] += 1
  90. elif 'penalty lost' in comment[1]:
  91. first_foul = comment[-1],comment[0]
  92. if comment[-1]=='home': foul_count[0] += 1
  93. else: foul_count[1] += 1
  94. if comment[-1]=='home':
  95. penalty_count[1] += 1
  96. first_penalty = 'away',comment[0]
  97. else:
  98. penalty_count[0] += 1
  99. first_penalty = 'home',comment[0]
  100. elif 'free kick lost' in comment[1]:
  101. first_foul = comment[-1],comment[0]
  102. if comment[-1]=='home': foul_count[0] += 1
  103. else: foul_count[1] += 1
  104. elif 'corner' in comment[1]:
  105. first_corner = comment[-1],comment[0]
  106. if comment[-1]=='home': corner_count[0] += 1
  107. else: corner_count[1] += 1
  108. elif 'offside' in comment[1]:
  109. first_offside = comment[-1],comment[0]
  110. if comment[-1]=='home': offside_count[0] += 1
  111. else: offside_count[1] += 1
  112. elif 'own goal' in comment[1]:
  113. first_owngoal = comment[-1],comment[0]
  114. if comment[-1]=='home':
  115. first_goal = 'away',comment[0]
  116. goal_count[1] += 1
  117. owngoal_count[0] += 1
  118. else:
  119. first_goal = 'home',comment[0]
  120. goal_count[0] += 1
  121. owngoal_count[1] += 1
  122. elif 'goal' in comment[1]:
  123. first_goal = first_attempt = comment[-1],comment[0]
  124. if comment[-1]=='home':
  125. goal_count[0] += 1
  126. ontarget_count[0] += 1
  127. else:
  128. goal_count[1] += 1
  129. ontarget_count[1] += 1
  130. elif 'saved' in comment[1]:
  131. first_ontarget = first_attempt = comment[-1],comment[0]
  132. if comment[-1]=='home': ontarget_count[0] += 1
  133. else: ontarget_count[1] += 1
  134. elif 'miss' in comment[1]:
  135. first_offtarget = first_attempt = comment[-1],comment[0]
  136. if comment[-1]=='home': offtarget_count[0] += 1
  137. else: offtarget_count[1] += 1
  138. elif 'post' in comment[1]:
  139. first_hitpost = first_attempt = comment[-1],comment[0]
  140. if comment[-1]=='home': hitpost_count[0] += 1
  141. else: hitpost_count[1] += 1
  142. elif 'blocked' in comment[1]:
  143. first_blocked = first_attempt = comment[-1],comment[0]
  144. if comment[-1]=='home': blocked_count[0] += 1
  145. else: blocked_count[1] += 1
  146. elif 'substitution' in comment[1]:
  147. first_sub = comment[-1],comment[0]
  148. if comment[-1]=='home': sub_count[0] += 1
  149. else: sub_count[1] += 1
  150. else:
  151. if 'free kick won' not in comment[1]:
  152. print 'uncategorized comment:', comment[1], comment[-1], comment[0]
  153. attempt_count[0] = ontarget_count[0] + offtarget_count[0] +\
  154. hitpost_count[0] + blocked_count[0]
  155. attempt_count[1] = ontarget_count[1] + offtarget_count[1] +\
  156. hitpost_count[1] + blocked_count[1]
  157.  
  158.  
  159.  
  160. print '\ngoals: home %d, away %d' % tuple(goal_count)
  161. print 'first goal: %s %s' % first_goal
  162. print '\nown goals: home %d, away %d' % tuple(owngoal_count)
  163. print 'first own goal: %s %s' % first_owngoal
  164.  
  165. print '\npenalties: home %d, away %d' % tuple(penalty_count)
  166. print 'first penalty: %s %s' % first_penalty
  167.  
  168. print '\ntotal sub: home %d, away %d' % tuple(sub_count)
  169. print 'first sub: %s %s' % first_sub
  170.  
  171. bar_len = max(len(team[0]), attempt_count[0], yellow_count[0], red_count[0],\
  172. offside_count[0], foul_count[0], corner_count[0]) + 1
  173. print '\n\n{0:>{4}} {1:>2} - {2:<2} {3}\n'.format(team[0], goal_count[0],\
  174. goal_count[1], team[1], 13 + bar_len)
  175. mid_str_fmt = u' {0:>2} {1:<2} '
  176. print ' Shots %s' % (get_bar(attempt_count[0], attempt_count[1],\
  177. bar_len, mid_str_fmt))
  178. print ' On target %s' % (get_bar(ontarget_count[0], ontarget_count[1],\
  179. bar_len, mid_str_fmt))
  180. print ' Off target %s' % (get_bar(offtarget_count[0], offtarget_count[1],\
  181. bar_len, mid_str_fmt))
  182. print 'Hit woodwork %s' % (get_bar(hitpost_count[0], hitpost_count[1],\
  183. bar_len, mid_str_fmt))
  184. print ' Blocked %s' % (get_bar(blocked_count[0], blocked_count[1],\
  185. bar_len, mid_str_fmt))
  186. print ''
  187. print 'Yellow cards %s' % (get_bar(yellow_count[0], yellow_count[1],\
  188. bar_len, mid_str_fmt))
  189. print ' Red cards %s' % (get_bar(red_count[0], red_count[1],\
  190. bar_len, mid_str_fmt))
  191. print ''
  192. print ' Offsides %s' % (get_bar(offside_count[0], offside_count[1],\
  193. bar_len, mid_str_fmt))
  194. print ' Fouls %s' % (get_bar(foul_count[0], foul_count[1],\
  195. bar_len, mid_str_fmt))
  196. print ' Corners %s' % (get_bar(corner_count[0], corner_count[1],\
  197. bar_len, mid_str_fmt))
Runtime error #stdin #stdout #stderr 0.03s 8416KB
stdin
Standard input is empty
stdout
Enter match id: 
stderr
Traceback (most recent call last):
  File "prog.py", line 29, in <module>
    match_id = raw_input("Enter match id: ")
EOFError: EOF when reading a line