Advertisement
Guest User

Untitled

a guest
Nov 10th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'rubyXL'
  2. workbook = RubyXL::Workbook.new
  3. Sheet = workbook[0]
  4. Sheet.add_cell(0, 0, "Ars")
  5. Sheet.add_cell(0, 1, "Man")
  6. Sheet.add_cell(0, 2, "Ars-Man")
  7. Sheet.add_cell(0, 3, "Year")
  8. Sheet.add_cell(0, 4, "Winning")
  9.  
  10. class Counter
  11.     def initialize
  12.         @line_num = 0
  13.         @block_num = 0
  14.         @ars_is_host = false
  15.     end
  16.  
  17.     def reset_line
  18.         @line_num = 0
  19.     end
  20.  
  21.     def line
  22.         @line_num
  23.     end
  24.  
  25.     def next_line
  26.         @line_num += 1
  27.     end
  28.  
  29.     def block
  30.         @block_num
  31.     end
  32.  
  33.     def next_block
  34.         @block_num += 1
  35.     end
  36.  
  37.     def hosted_by_ars res
  38.         @ars_is_host = res     
  39.     end
  40.  
  41.     def ars_is_host?
  42.         @ars_is_host
  43.     end
  44. end
  45.  
  46. def parse_zero line, counter
  47.     counter.next_block
  48.     counter.reset_line
  49.    
  50. end
  51.  
  52.  
  53. def parse_first line, counter
  54.     day, s1, month, s2, year = line.match(/([0-9]+)( )([A-z]+)( )([0-9]{4})/).captures
  55.     date = [day, month, year].join(' ')
  56.     ::Sheet.add_cell(counter.block, 3, date)
  57.    
  58. end
  59.  
  60. def parse_second line, counter
  61.     if "Arsenal v Manchester United".match(line)
  62.         counter.hosted_by_ars true
  63.     else
  64.         counter.hosted_by_ars false
  65.     end
  66.    
  67. end
  68.  
  69. def parse_fourth line, counter
  70.     if counter.ars_is_host?
  71.         s1, ars, b, man = line.match(/(.*">)([0-9]+)(-)([0-9]+)/).captures
  72.         ::Sheet.add_cell(counter.block, 0, ars)
  73.         ::Sheet.add_cell(counter.block, 1, man)
  74.     else
  75.         s1, man, b, ars = line.match(/(.*">)([0-9]+)(-)([0-9]+)/).captures
  76.         ::Sheet.add_cell(counter.block, 0, ars)
  77.         ::Sheet.add_cell(counter.block, 1, man)
  78.     end
  79.     ars_man = ars.to_i-man.to_i
  80.     if ars_man > 0
  81.         ::Sheet.add_cell(counter.block, 4, 1)
  82.     elsif ars_man < 0
  83.         ::Sheet.add_cell(counter.block, 4, -1)
  84.     else
  85.         ::Sheet.add_cell(counter.block, 4, 0)
  86.     end
  87.     ::Sheet.add_cell counter.block, 2, ars_man
  88.    
  89. end
  90.  
  91.  
  92.  
  93. counter = Counter.new
  94.  
  95. File.open("untitled.txt").each do |line|
  96.     if line.to_s.include? "<tr>"
  97.         parse_zero line, counter
  98.     else
  99.         case counter.line
  100.         when 1
  101.             parse_first line, counter
  102.         when 2
  103.             parse_second line, counter
  104.         when 4
  105.             parse_fourth line, counter
  106.         end
  107.     end
  108.     counter.next_line
  109. end
  110.  
  111. workbook.write("./Ars3.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement