Class: Rsssf::Repo

Inherits:
Object
  • Object
show all
Includes:
Filters, Utils
Defined in:
lib/rsssf/repo.rb

Instance Method Summary collapse

Methods included from Utils

#archive_dir_for_year, #year_from_file, #year_from_name, #year_to_season

Methods included from Filters

#html_to_txt, #sanitize

Constructor Details

#initialize(path, opts) ⇒ Repo

pass in title etc.



29
30
31
32
# File 'lib/rsssf/repo.rb', line 29

def initialize( path, opts )   ## pass in title etc.
  @repo_path = path
  @opts      = opts
end

Instance Method Details

#fetch_pagesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rsssf/repo.rb', line 35

def fetch_pages
  puts "fetch_pages:"
  cfg = YAML.load_file( "#{@repo_path}/tables/config.yml") 
  pp cfg

  dl_base = 'http://rsssf.com'

  cfg.each do |k,v|
    ## season = k   # as string e.g. 2011-12  or 2011 etc.
    path      = v  # as string e.g. tablesd/duit2011.html

    ## note: assumes extension is .html
    #    e.g. tablesd/duit2011.html => duit2011
    basename = File.basename( path, '.html' )

    src_url   = "#{dl_base}/#{path}"
    dest_path = "#{@repo_path}/tables/#{basename}.txt"

    page = Page.from_url( src_url )
    page.save( dest_path )
  end # each year
end

#make_pages_summaryObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rsssf/repo.rb', line 59

def make_pages_summary
  stats = []

  files = Dir[ "#{@repo_path}/tables/*.txt" ]
  files.each do |file|
    page = Page.from_file( file )
    stats << page.build_stat
  end

  ### save report as README.md in tables/ folder in repo
  report = PageReport.new( stats, @opts )    ## pass in title etc.  
  report.save( "#{@repo_path}/tables/README.md" )
end

#make_schedules(cfg) ⇒ Object



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
# File 'lib/rsssf/repo.rb', line 97

def make_schedules( cfg )

  ## note: return stats (for report eg. README)
  stats = []
  
  files = Dir[ "#{@repo_path}/tables/*.txt" ]
  files.each do |file|
    
## todo/check/fix:
##   use source: prop in rsssf page - why? why not???
##   move year/season/basename into page ???
#
#  assume every rsssf page has at least:
##    - basename  e.g. duit2014
##    - year      e.g. 2014 (numeric)
##    - season    (derived from config lookup???) - string e.g. 2014-15 or 2014 etc. 
    extname  = File.extname( file )
    basename = File.basename( file, extname )
    year     = year_from_name( basename )
    season   = year_to_season( year )

    if cfg.includes && cfg.includes.include?( year ) == false
      puts "   skipping #{basename}; not listed in includes"
      next
    end


    puts "  reading >#{basename}<"

    page = Page.from_file( file ) # note: always assume sources (already) converted to utf-8

    if cfg.opts_for_year.is_a?( Hash )
      opts = cfg.opts_for_year    ## just use as is 1:1 (constant/same for all years)
    else
      ## assume it's a proc/lambda (call to calculate)
      opts = cfg.opts_for_year.call( year ) 
    end
    pp opts

    schedule = page.find_schedule( opts )
    ## pp schedule

 
    if cfg.dir_for_year.nil?
      ## use default setting, that is, archive for dir (e.g. archive/1980s/1985-86 etc.)
      dir_for_year = archive_dir_for_year( year )
    else
      ## assume it's a proc/lambda
      dir_for_year = cfg.dir_for_year.call( year )
    end

    ## -- cfg.name               e.g. => 1-liga

    dest_path = "#{@repo_path}/#{dir_for_year}/#{cfg.name}.txt"
    puts "  save to >#{dest_path}<"
    FileUtils.mkdir_p( File.dirname( dest_path ))
    schedule.save( dest_path )

    rec = ScheduleStat.new
    rec.path     = dir_for_year
    rec.filename = "#{cfg.name}.txt"    ## change to basename - why?? why not?? 
    rec.year     = year
    rec.season   = season
    rec.rounds   = schedule.rounds

    stats << rec
  end

  stats  # return stats for reporting
end

#make_schedules_summary(stats) ⇒ Object

note: requires stats to be passed in for now



74
75
76
77
# File 'lib/rsssf/repo.rb', line 74

def make_schedules_summary( stats )   ## note: requires stats to be passed in for now
  report = ScheduleReport.new( stats, @opts )   ## pass in title etc.
  report.save( "#{@repo_path}/README.md" )
end

#patch_pages(patcher) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rsssf/repo.rb', line 81

def patch_pages( patcher )
  ## lets you run/use custom (repo/country-specific patches e.g. for adding/patching headings etc.)
  patch_dir( "#{@repo_path}/tables" ) do |txt, name, year|
    puts "patching #{year} (#{name}) (#{@repo_path})..."
    patcher.patch( txt, name, year )    ## note: must be last (that is, must return (patcher) t(e)xt)
  end
end

#sanitize_pagesObject



90
91
92
93
# File 'lib/rsssf/repo.rb', line 90

def sanitize_pages
   ## for debugging/testing lets you (re)run sanitize  (alreay incl. in html2txt filter by default)
   sanitize_dir( "#{@repo_path}/tables" )
end