mcbot/cmds/tvcal.lua
changeset 58 4c3028c21ccb
parent 57 100a831f4f12
child 59 0a39d217b80a
equal deleted inserted replaced
57:100a831f4f12 58:4c3028c21ccb
    30 
    30 
    31     if not contents then
    31     if not contents then
    32         return nil, "Could not fetch calendar, please try again later!"
    32         return nil, "Could not fetch calendar, please try again later!"
    33     end
    33     end
    34 
    34 
    35     local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>%s*\n%s*'..
    35     -- Regex for each day
    36                      '<strong>.-</strong>%s*\n%s*'..
    36     local tabregex = '<td id="d_(%d+_%d+_%d%d%d%d)" class="t?o?day"%s*>'..
    37                      '(.-)</td>'
    37                      '%s*(.-)%s*</td>'
    38 
    38 
    39     for day, tab in string.gmatch(contents, tabregex) do
    39     -- Regex for parsing one day
    40         local epregex
    40     local epregex = '<p>'..
       
    41                       '<a href=[^>]+>([^<]+)</a>'..       -- Name
       
    42                       '<br /><a href=[^>]+>([^<]+)</a>'.. -- Episode
       
    43                     '</p>'
       
    44 
       
    45     -- loop over all days
       
    46     for day, daytab in string.gmatch(contents, tabregex) do
    41         local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$")
    47         local d, m, y = day:match("^(%d+)_(%d+)_(%d%d%d%d)$")
    42         day = string.format("%04d-%02d-%02d", y, m, d)
    48         day = string.format("%04d-%02d-%02d", y, m, d)
    43 
    49 
    44         epregex = '<ul class="seasep%s*%w*">'..
    50         local shows = {}
    45                     '<li><a [^>]+>([^<]+)</a></li>'..   -- Name
       
    46                     '<li>([^<]+)</li>'..                -- Episode
       
    47                   '</ul>'
       
    48 
    51 
    49         local shows = {}
    52         -- Loop over all episodes of a day
    50         for name, ep in string.gmatch(tab, epregex) do
    53         for name, ep in string.gmatch(daytab, epregex) do
    51             name = name:gsub("&quot;", "\""):gsub("&amp;", "&")
    54             name = name:gsub("&quot;", "\""):gsub("&amp;", "&")
    52             name = name:gsub("&lt;", "<"):gsub("&gt;", ">")
    55             name = name:gsub("&lt;", "<"):gsub("&gt;", ">")
    53             name = name:gsub("&#39;", "'")
    56             name = name:gsub("&#39;", "'")
    54 
    57 
    55             local obj = {
    58             local obj = {
    58                 }
    61                 }
    59             table.insert(shows, obj)
    62             table.insert(shows, obj)
    60         end
    63         end
    61         tvcaldata[day] = shows
    64         tvcaldata[day] = shows
    62     end
    65     end
       
    66 
    63     tvcaldata_timestamp = os.date("%F")
    67     tvcaldata_timestamp = os.date("%F")
    64     return true
    68     return true
    65 end
    69 end
    66 
    70 
    67 local function tvcal_by_date (d)
    71 local function tvcal_by_date (d)