Module: Fifadat

Defined in:
lib/fifadat/convert/tool.rb

Class Method Summary collapse

Class Method Details

.main(args = ARGV) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fifadat/convert/tool.rb', line 5

def self.main( args=ARGV )


opts = {
  season:  nil,
  file:    nil,
  force:   false,
  offline: false,
  reports: true,

  cache_dir:    '/sports/cache.fifadat',
  convert_dir:  '/sports/cache.api.fifa',
}


parser = OptionParser.new do |parser|
parser.banner = "Usage: #{$PROGRAM_NAME} [options] NAME"
   ## add options here

   parser.on( "--season=SEASON",
                 "season" ) do |season|
     opts[:season] = season
   end


  parser.on( "--cache", "--cached", "--offline",
               "always (and only) use cached data in >#{opts[:cache_dir]}< - default is (#{opts[:offline]})" ) do |offline|
    opts[:offline] = offline
  end

  parser.on( "--force",
               "always (force) download fresh copy & overwrite - default is (#{opts[:force]})" ) do |force|
    opts[:force] = force
  end

  parser.on( "--[no-]reports",
               "incl. one-by-one (detailed) match reports - default is (#{opts[:reports]})" ) do |reports|
    opts[:reports] = reports
  end



  parser.on( "-f FILE", "--file FILE",
                "read leagues (and seasons) via .csv file") do |file|
    opts[:file] = file
  end



end
parser.parse!( args )


puts "OPTS:"
pp opts
puts "ARGV:"
pp args


cache_dir    = opts[:cache_dir]
convert_dir  = opts[:convert_dir]



if opts[:file]
   recs = read_csv( opts[:file] )
else

  ## otherwise

  ##   build rec(ord)s from scratch (from command-line args)

  if args.size == 0
    puts " NAME argument (plus season opt) required; use:"
    Fifa::CODES.each do |code, h|
        idCompetition = h[:idCompetition]
        comp = Fifa::COMPETITIONS[idCompetition]
        seasons = comp.values.map { |c| c[:season] }
        puts "  #{code}   (#{seasons.size}) #{seasons.join(' ')}"
    end
    exit 1
  end


  ##

  ## note - all args other than first ignored for now; issue warn - why? why not?

  rec = {}
  rec['league']    =  args[0].downcase
  rec['seasons']   =  Season.parse(opts[:season]).to_s
  recs = [rec]
end

pp recs


   ################

   ## step 0) validate slugs & seasons

   recs.each do |rec|
      slug   =  rec['league']
      seasons = Season.parse_line( rec['seasons'] )
      seasons.each do |season|
        pp  Fifa._idSeason_by!( name: slug, season: season )
      end
    end


  if opts[:offline] == false
    ## step 1a) prepare

   recs.each do |rec|
      slug   =  rec['league']
      seasons = Season.parse_line( rec['seasons'] )
      seasons.each do |season|

        prepare( name:    slug,
                 season:  season,
                 outdir:  cache_dir,
                 force: opts[:force] )    ## note - autoadd slug (name) e..g ./eng !!


=begin
        ###
        ## add debug
        page = String.new
        page << "= #{slug} #{season}\n"
        page << "#  generated on #{Time.now}\n"
        page <<  "\n"

        buf = pp_debug( slug: slug, season: season,
                    indir: cache_dir )

        page << buf
        puts page

        outpath =  "#{convert_dir}/#{season.to_path}_#{slug}-debug.txt"

        write_text( outpath, page )
        puts "  written to >#{outpath}<"
=end

      end
    end

    if opts[:reports]
      ## step 1b) prepare details

      recs.each do |rec|
        slug   =  rec['league']
        seasons = Season.parse_line( rec['seasons'] )
        seasons.each do |season|
          ## match (reports) one-by-one

          prepare_reports( name:    slug,
                         season:  season,
                         outdir:  cache_dir,    ## note - autoadd slug (name) e..g ./eng !!

                         force: opts[:force] )
        end
      end
    end # if reports

  end  ## if online (offline==false)





   ## step 2) convert

   recs.each do |rec|
      slug   =  rec['league']
      seasons = Season.parse_line( rec['seasons'] )
      seasons.each do |season|

        convert( slug: slug, season: season,
                               indir: cache_dir,
                              outdir: convert_dir )


        ##  convert all match reports one-by-one

        convert_reports( slug: slug, season: season,
                                     indir: cache_dir,
                                     outdir: convert_dir )
      end
   end

puts "bye"

end