Module: Fbhub

Defined in:
lib/fbtxt-pp/tool-fbhub.rb

Constant Summary collapse

LEAGUE_CODES =
{
    'de'  => 'de.1',
    'eng' => 'eng.1',
    'es'  => 'es.1',
    'it'  => 'it.1',
    'fr'  => 'fr.1',
    'at'  => 'at.1',
    'mx'  => 'mx.1',
}

Class Method Summary collapse

Class Method Details

.main(args = ARGV) ⇒ Object



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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fbtxt-pp/tool-fbhub.rb', line 23

def self.main( args=ARGV )

opts = {
  push:     false,
  ffwd:     false,

  full:     true,   ## add full details page - true|false
  test:     true,  ## true,   ## sets push & ffwd to false
  test_dir:  './o',
  convert_dir:  '/sports/cache.api.fifa',
  file:     nil,
}


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

    parser.on( "-p", "--[no-]push",
               "fast forward sync and commit & push changes to git repo - default is (#{opts[:push]})" ) do |push|
      opts[:push] = push
      if opts[:push]   ## note: autoset ffwd too if push == true
        opts[:ffwd] = true
        opts[:test] = false
      end
    end
    ## todo/check - add a --ffwd flag too - why? why not?

    parser.on( "-t", "--test",
                "test run; writing output to #{opts[:test_dir]} - default is #{opts[:test]}" ) do |test|
      opts[:test] = true
      opts[:push] = false
      opts[:ffwd] = false
    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


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

               datasets = recs.map do |rec|
                               ## auto-convert season to season obj - why? why not?
                               ##   use Season.parse_line
                                 [ rec['league'],
                                   Season.parse_line( rec['seasons'])]
                          end
           else
             puts "!! error: --file FILE option for now required; sorry"
             exit 1
           end


pp datasets


####
#  get github repos for (league) slugs/codes

root_dir =  if opts[:test]
               opts[:test_dir]
            else
               Fbup::GitHubSync.root   # e.g. "/sports"
            end

puts "  (output) root_dir: >#{root_dir}<"


repos = Fbup::GitHubSync.find_repos( datasets )
puts "  #{repos.size} repo(s):"
pp repos

sync  =  Fbup::GitHubSync.new( repos )
puts "  sync:"
pp sync

sync.git_fast_forward_if_clean    if opts[:ffwd]



datasets.each do |slug, seasons|
  puts "==> gen #{slug} - #{seasons.size} seasons(s)..."

  config = CONFIGS[ slug ]
  if config.nil?
     puts "!! no pp config found for slug >#{slug}<; keys/codes include:"
     pp CONFIGS.keys
     exit 1
  end

  seasons.each do |season|
     ## get repo config for flags and more
      repo  = Fbup::GitHubSync::REPOS[ slug ]
      flags = repo['flags'] || {}
      classic_flag = flags['classic'] || false

      pp repo


      basename = nil
      if classic_flag
         league_config = Fbup::LeagueConfig.find_by( code:   LEAGUE_CODES[slug]||slug,
                                                      season: season )
         if league_config.nil?
            puts "!! ERROR - basename league config required for classic format; no config found for #{league_query} #{season}; sorry"
            exit 1
         end
         basename  = league_config['basename']
      else
         ## change base name to league key
         ##   todo - fix - make gsub smarter
         ##    change at.cup to at_cup - why? why not?
         basename = (LEAGUE_CODES[slug]||slug).gsub( '.', '' )
      end


      repo_path = "#{repo['owner']}/#{repo['name']}"
      repo_path << "/#{repo['path']}"    if repo['path']  ## note: do NOT forget to add optional extra path!!!


      outpath = "#{root_dir}/#{repo_path}"
      outpath +=  if classic_flag
                     "/#{season.to_path}/#{basename}.txt"
                  else
                     ## note - add season "inline" (to basename) or use dir
                     "/#{season.to_path}_#{basename}.txt"
                  end

      outpath_full = "#{root_dir}/#{repo_path}"
      outpath_full +=  if classic_flag
                     "/#{season.to_path}/#{basename}-full.txt"
                  else
                     ## note - add season "inline" (to basename) or use dir
                     "/#{season.to_path}_#{basename}-full.txt"
                  end

       puts "   writing to >#{outpath}<..."
       puts "   writing (full) to >#{outpath_full}<..."

       league_name      = config[:name]
       format_opts      = config[:opts]
       format_opts_full = config[:opts_full]

        header = String.new
        header << "= #{league_name} #{season}\n\n"

        buf = pp_matches( slug: slug, season: season,
                          indir: opts[:convert_dir],
                          opts: FormatOpts.build( **format_opts ))

        puts buf[0..300]
        write_text( outpath, header+buf )

        if opts[:full]
          buf = pp_matches_full( slug: slug, season: season,
                                 indir: opts[:convert_dir],
                                  opts: FormatOpts.build_full( **format_opts_full ))


          puts buf[0..300]
          write_text( outpath_full, header+buf )
        end
  end
end


sync.git_push_if_changes   if opts[:push]


puts "bye"
end