<commit_before>local InfoMessage = require("ui/widget/infomessage")
local ConfirmBox = require("ui/widget/confirmbox")
local NetworkMgr = require("ui/networkmgr")
local lfs = require("libs/libkoreader-lfs")
local UIManager = require("ui/uimanager")
local Device = require("device")
local DEBUG = require("dbg")
local T = require("ffi/util").template
local _ = require("gettext")

local OTAManager = {
    ota_servers = {
        "http://vislab.bjmu.edu.cn:80/apps/koreader/ota/",
        "http://koreader.ak-team.com:80/",
        "http://hal9k.ifsc.usp.br:80/koreader/",
    },
    ota_channels = {
        "nightly",
    },
    zsync_template = "koreader-%s-latest-%s.zsync",
    installed_package = "ota/koreader.installed.tar",
    package_indexfile = "ota/package.index",
    updated_package = "ota/koreader.updated.tar",
}

function OTAManager:getOTAModel()
    if Device:isKindle() then
        return "kindle"
    elseif Device:isKobo() then
        return "kobo"
    else
        return ""
    end
end

function OTAManager:getOTAServer()
    return G_reader_settings:readSetting("ota_server") or self.ota_servers[1]
end

function OTAManager:setOTAServer(server)
    DEBUG("Set OTA server:", server)
    G_reader_settings:saveSetting("ota_server", server)
end

function OTAManager:getOTAChannel()
    return G_reader_settings:readSetting("ota_channel") or self.ota_channels[1]
end

function OTAManager:setOTAChannel(channel)
    DEBUG("Set OTA channel:", channel)
    G_reader_settings:saveSetting("ota_channel", channel)
end

function OTAManager:getZsyncFilename()
    return self.zsync_template:format(self:getOTAModel(), self:getOTAChannel())
end

function OTAManager:checkUpdate()
    local http = require("socket.http")
    local ltn12 = require("ltn12")

    local zsync_file = self:getZsyncFilename()
    local ota_zsync_file = self:getOTAServer() .. zsync_file
    local local_zsync_file = "ota/" .. zsync_file
    -- download zsync file from OTA server
    local r, c, h = http.request{
        url = ota_zsync_file,
        sink = ltn12.sink.file(io.open(local_zsync_file, "w"))}
    -- prompt users to turn on Wifi if network is unreachable
    if c ~= 200 then return end
    -- parse OTA package version
    local ota_package = nil
    local zsync = io.open(local_zsync_file, "r")
    if zsync then
        for line in zsync:lines() do
            ota_package = line:match("^Filename:%s*(.-)%s*$")
            if ota_package then break end
        end
        zsync:close()
    end
    local normalized_version = function(rev)
        local year, month, revision = rev:match("v(%d%d%d%d)%.(%d%d)-?(%d*)")
        return tonumber(year .. month .. (revision or "0"):format("%.4d"))
    end
    local local_ok, local_version = pcall(function()
        local rev_file = io.open("git-rev", "r")<commit_msg>fix OTA server broken on checking stable release<commit_after>local InfoMessage = require("ui/widget/infomessage")
local ConfirmBox = require("ui/widget/confirmbox")
local NetworkMgr = require("ui/networkmgr")
local lfs = require("libs/libkoreader-lfs")
local UIManager = require("ui/uimanager")
local Device = require("device")
local DEBUG = require("dbg")
local T = require("ffi/util").template
local _ = require("gettext")

local OTAManager = {
    ota_servers = {
        "http://vislab.bjmu.edu.cn:80/apps/koreader/ota/",
        "http://koreader.ak-team.com:80/",
        "http://hal9k.ifsc.usp.br:80/koreader/",
    },
    ota_channels = {
        "nightly",
    },
    zsync_template = "koreader-%s-latest-%s.zsync",
    installed_package = "ota/koreader.installed.tar",
    package_indexfile = "ota/package.index",
    updated_package = "ota/koreader.updated.tar",
}

function OTAManager:getOTAModel()
    if Device:isKindle() then
        return "kindle"
    elseif Device:isKobo() then
        return "kobo"
    else
        return ""
    end
end

function OTAManager:getOTAServer()
    return G_reader_settings:readSetting("ota_server") or self.ota_servers[1]
end

function OTAManager:setOTAServer(server)
    DEBUG("Set OTA server:", server)
    G_reader_settings:saveSetting("ota_server", server)
end

function OTAManager:getOTAChannel()
    return G_reader_settings:readSetting("ota_channel") or self.ota_channels[1]
end

function OTAManager:setOTAChannel(channel)
    DEBUG("Set OTA channel:", channel)
    G_reader_settings:saveSetting("ota_channel", channel)
end

function OTAManager:getZsyncFilename()
    return self.zsync_template:format(self:getOTAModel(), self:getOTAChannel())
end

function OTAManager:checkUpdate()
    local http = require("socket.http")
    local ltn12 = require("ltn12")

    local zsync_file = self:getZsyncFilename()
    local ota_zsync_file = self:getOTAServer() .. zsync_file
    local local_zsync_file = "ota/" .. zsync_file
    -- download zsync file from OTA server
    local r, c, h = http.request{
        url = ota_zsync_file,
        sink = ltn12.sink.file(io.open(local_zsync_file, "w"))}
    -- prompt users to turn on Wifi if network is unreachable
    if c ~= 200 then return end
    -- parse OTA package version
    local ota_package = nil
    local zsync = io.open(local_zsync_file, "r")
    if zsync then
        for line in zsync:lines() do
            ota_package = line:match("^Filename:%s*(.-)%s*$")
            if ota_package then break end
        end
        zsync:close()
    end
    local normalized_version = function(rev)
        local year, month, revision = rev:match("v(%d%d%d%d)%.(%d%d)-?(%d*)")
        return tonumber(year .. month .. string.format("%.4d", revision or "0"))
    end
    local local_ok, local_version = pcall(function()
        local rev_file = io.open("git-rev", "r")
{'fraction_non_alphanumeric': 0.12963940193491644, 'fraction_numerical': 0.007036059806508356, 'mean_word_length': 3.5707395498392285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '486445', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1909, 'n_words': 405}