<?php

require_once '../init.php';
$minute = (int) date('i');
$hour = (int) date('H');
if ($hour != 13) {
    exit;
}
if ($minute != 15) {
    exit;
}
$json = CrestTools::getJSON('https://public-crest.eveonline.com/inventory/categories/7/');
foreach ($json['groups'] as $group) {
    $href = $group['href'];
    $types = CrestTools::getJSON($href);
    if ($types != null && $types['types'] != null) {
        foreach ($types['types'] as $type) {
            $typeID = getTypeID($type['href']);
            $mdb->set('information', ['type' => 'typeID', 'id' => $typeID], ['fittable' => true]);
        }
    }
    sleep(1);
}
function getTypeID($href)
{
    $ex = explode('/', $href);
    return (int) $ex[4];
}
function insertdata($url, $cjtype)
{
    global $db, $action, $rtype, $rpage, $rkey, $rday, $cjurl, $xt, $cjflag, $backurl;
    if ($xt == "1" || $xt == "2") {
        $xn_vod = '/<video><last>([\\s\\S]*?)<\\/last><id>([0-9]+)<\\/id><tid>([0-9]+)<\\/tid><name><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/name><type>([\\s\\S]*?)<\\/type><pic>([\\s\\S]*?)<\\/pic><lang>([\\s\\S]*?)<\\/lang><area>([\\s\\S]*?)<\\/area><year>([\\s\\S]*?)<\\/year><state>([\\s\\S]*?)<\\/state><note><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/note><actor><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/actor><director><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/director><dl>([\\s\\S]*?)<\\/dl><des><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/des><\\/video>/';
        $xn_url = '/<dd flag="([\\s\\S]*?)"><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/dd>/';
        $xn_vod_time = 1;
        $xn_vod_id = 2;
        $xn_vod_name = 4;
        $xn_vod_type = 3;
        $xn_vod_pic = 6;
        $xn_vod_language = 7;
        $xn_vod_area = 8;
        $xn_vod_year = 9;
        $xn_vod_state = 10;
        $xn_vod_remarks = 11;
        $xn_vod_starring = 12;
        $xn_vod_directed = 13;
        $xn_vod_urls = 14;
        $xn_vod_des = 15;
    } else {
        $xn_vod = '/<vod><id>([0-9]+)<\\/id><name><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/name><note><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/note><state>([\\s\\S]*?)<\\/state><type>([\\s\\S]*?)<\\/type><starring><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/starring><directed><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/directed><pic>([\\s\\S]*?)<\\/pic><time>([\\s\\S]*?)<\\/time><year>([\\s\\S]*?)<\\/year><area><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/area><language><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/language><urls>([\\s\\S]*?)<\\/urls><des><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/des><\\/vod>/';
        $xn_url = '/<url from="([\\s\\S]*?)"><\\!\\[CDATA\\[([\\s\\S]*?)\\]\\]><\\/url>/';
        $xn_vod_id = 1;
        $xn_vod_name = 2;
        $xn_vod_remarks = 3;
        $xn_vod_state = 4;
        $xn_vod_type = 5;
        $xn_vod_starring = 6;
        $xn_vod_directed = 7;
        $xn_vod_pic = 8;
        $xn_vod_time = 9;
        $xn_vod_year = 10;
        $xn_vod_area = 11;
        $xn_vod_language = 12;
        $xn_vod_des = 14;
        $xn_vod_urls = 13;
    }
    $html = getPage($url, "utf-8");
    if (html == false) {
        echo "err获取数据失败,请点击我重试";
        exit;
    }
    if ($xt == "1" || $xt == "2") {
        $pagecount = intval(getBody($html, "pagecount=\"", "\" pagesize"));
    } else {
        $pagecount = intval(getBody($html, "<pagecount>", "</pagecount>"));
    }
    if ($pagecount == 0) {
        echo "err没有任何可用数据";
        exit;
    }
    preg_match_all($xn_vod, $html, $array3);
    $i = 0;
    foreach ($array3[1] as $key => $value) {
        $rc = false;
        $vodid = $array3[$xn_vod_id][$key];
        $vodname = $array3[$xn_vod_name][$key];
        $vodremarks = $array3[$xn_vod_remarks][$key];
        $vodstate = $array3[$xn_vod_state][$key];
        $vodtype = $cjflag . $array3[$xn_vod_type][$key];
        $vodstarring = $array3[$xn_vod_starring][$key];
        $voddirected = $array3[$xn_vod_directed][$key];
        $vodpic = $array3[$xn_vod_pic][$key];
        $vodtime = $array3[$xn_vod_time][$key];
        $vodyear = $array3[$xn_vod_year][$key];
        $vodarea = $array3[$xn_vod_area][$key];
        $vodlanguage = $array3[$xn_vod_language][$key];
        $voddes = $array3[$xn_vod_des][$key];
        $vodurls = $array3[$xn_vod_urls][$key];
        preg_match_all($xn_url, $vodurls, $array4);
        $vodsubname = "";
        $vodcolor = "";
        $vodhitstime = "";
        $voddownurl = "";
        $vodplayserver = "0";
        $vodfrom = "";
        $vodurl = "";
        $vodname = replaceStr($vodname, "'", "");
        if (strlen($vodname) > 255) {
            $vodname = substring($vodname, 255);
        }
        $vodenname = Hanzi2PinYin($vodname);
        if (strlen($vodenname) > 255) {
            $vodenname = substring($vodenname, 255);
        }
        if (!isN($vodenname)) {
            $vodletter = strtoupper(substring($vodenname, 1));
        }
        $vodstate = replaceStr($vodstate, "[", "");
        $vodstate = replaceStr($vodstate, "]", "");
        if (!isNum($vodstate)) {
            $vodstate = 0;
        }
        $vodstarring = replaceStr($vodstarring, "'", "");
        $vodstarring = replaceStr($vodstarring, "、", " ");
        $vodstarring = replaceStr($vodstarring, ",", " ");
        $vodstarring = replaceStr($vodstarring, ",", " ");
        $vodstarring = replaceStr($vodstarring, "  ", " ");
        if (strlen($vodstarring) > 255) {
            $vodstarring = substring($vodstarring, 255);
        }
        $vodyear = replaceStr($vodyear, "'", "");
        if (strlen($vodyear) > 32) {
            $vodyear = substring($vodyear, 32);
        }
        $vodlanguage = replaceStr($vodlanguage, "'", "");
        if (strlen($vodlanguage) > 32) {
            $vodlanguage = substring($vodlanguage, 32);
        }
        $vodarea = replaceStr($vodarea, "'", "");
        if (strlen($vodarea) > 32) {
            $vodarea = substring($vodarea, 32);
        }
        $vodpic = replaceStr($vodpic, "'", "");
        if (strlen($vodpic) > 255) {
            $vodpic = substring($vodpic, 255);
        }
        $voddes = replaceStr($voddes, "'", "");
        $vodtype = getTypeID($vodtype);
        if (!isNum($vodtype)) {
            $vodtype = 0;
        }
        if ($vodtype > 0) {
            $sql = "SELECT * FROM {pre}vod WHERE d_name ='" . $vodname . "' ";
            $row = $db->getRow($sql);
            if (!$row) {
                foreach ($array4[1] as $key => $value) {
                    if ($rc) {
                        $vodfrom .= "\$\$\$";
                        $vodurl .= "\$\$\$";
                    }
                    $vodfrom .= getFrom($value);
                    if ($xt == "1" || $xt == "2") {
                        $vodurl .= getVUrl($array4[2][$key]);
                    } else {
                        $vodurl .= $array4[2][$key];
                    }
                    $vodurl = replaceStr($vodurl, Chr(10), Chr(13));
                    if (substring($vodurl, 1, strlen($vodurl) - 1) == Chr(13)) {
                        $vodurl = substring($vodurl, strlen($vodurl) - 1);
                    }
                    $vodurl = replaceStr($vodurl, Chr(13), "#");
                    $vodurl = replaceStr($vodurl, "##", "#");
                    $vodurl = replaceStr($vodurl, "'", "''");
                    $rc = true;
                }
                $db->Add("{pre}vod", array("d_type", "d_name", "d_subname", "d_enname", "d_letter", "d_state", "d_color", "d_content", "d_remarks", "d_pic", "d_level", "d_hits", "d_starring", "d_directed", "d_year", "d_area", "d_language", "d_addtime", "d_time", "d_playurl", "d_downurl", "d_playfrom", "d_playserver"), array($vodtype, $vodname, $vodsubname, $vodenname, $vodletter, $vodstate, $vodcolor, $voddes, $vodremarks, $vodpic, 0, 0, $vodstarring, $voddirected, $vodyear, $vodarea, $vodlanguage, date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', time()), $vodurl, $voddownurl, $vodfrom, $vodplayserver));
                wTips($vodname, "新增数据");
            } else {
                $tmpplayurl = $row["d_playurl"];
                $tmpplayfrom = $row["d_playfrom"];
                $tmpplayserver = $row["d_playserver"];
                foreach ($array4[1] as $key => $value) {
                    $vodfrom = getFrom($value);
                    if ($xt == "1" || $xt == "2") {
                        $vodurl = getVUrl($array4[2][$key]);
                    } else {
                        $vodurl = $array4[2][$key];
                    }
                    $vodurl = replaceStr($vodurl, Chr(10), Chr(13));
                    if (substring($vodurl, 1, strlen($vodurl) - 1) == Chr(13)) {
                        $vodurl = substring($vodurl, strlen($vodurl) - 1);
                    }
                    $vodurl = replaceStr($vodurl, Chr(13), "#");
                    $vodurl = replaceStr($vodurl, "##", "#");
                    $vodurl = replaceStr($vodurl, "'", "''");
                    if ($tmpplayurl == $vodurl) {
                        $resultdes = "无需更新播放地址";
                        continue;
                    } else {
                        if (isN($vodfrom)) {
                            wTips($vodname, "播放器类型为空,跳过");
                            continue;
                        } else {
                            if (isN($tmpplayurl) || strpos("," . $row["d_playfrom"], $vodfrom) <= 0) {
                                $resultdes = "新增播放地址组";
                                $tmpplayurl .= "\$\$\$" . $vodurl;
                                $tmpplayfrom .= "\$\$\$" . $vodfrom;
                                $tmpplayserver .= "\$\$\$" . $vodplayserver;
                            } else {
                                $resultdes = "更新播放地址";
                                $arr1 = explode("\$\$\$", $tmpplayurl);
                                $arr2 = explode("\$\$\$", $tmpplayfrom);
                                $rc = false;
                                $tmpplayurl = "";
                                for ($k = 0; $k < count($arr2); $k++) {
                                    if ($rc) {
                                        $tmpplayurl .= "\$\$\$";
                                    }
                                    if ($arr2[$k] == $vodfrom) {
                                        $arr1[$k] = $vodurl;
                                    }
                                    $tmpplayurl .= $arr1[$k];
                                    $rc = true;
                                }
                            }
                        }
                    }
                }
                $tmpplayurl = replaceStr($tmpplayurl, Chr(13), "#");
                if (strpos("," . $row["d_pic"], "http:") <= 0) {
                    $vodpic = $row["d_pic"];
                }
                $db->Update("{pre}vod", array("d_state", "d_pic", "d_remarks", "d_time", "d_playurl", "d_playfrom", "d_playserver"), array($vodstate, $vodpic, $vodremarks, date('Y-m-d H:i:s', time()), $tmpplayurl, $tmpplayfrom, $tmpplayserver), "d_id=" . $row["d_id"]);
                wTips($vodname, $resultdes);
            }
            unset($row);
        } else {
            wTips($vodname, "没有绑定分类、跳过");
        }
        unset($array4);
    }
    unset($array3);
    echo "ok第" . $rpage . "页完成,共" . $pagecount . "页";
    if ($rpage < $pagecount) {
        $rpage = $rpage + 1;
        cjday();
    }
}
$newGroups = 0;
$newItems = 0;
do {
    foreach ($groups['items'] as $group) {
        $href = $group['href'];
        $groupID = (int) getGroupID($href);
        $name = $group['name'];
        $exists = $mdb->count('information', ['type' => 'groupID', 'id' => $groupID]);
        if ($exists == 0) {
            ++$newGroups;
        }
        $mdb->insertUpdate('information', ['type' => 'groupID', 'id' => $groupID], ['name' => $name, 'lastCrestUpdate' => $mdb->now(-86400)]);
        $types = CrestTools::getJSON($href);
        if (@$types['types'] != null) {
            foreach ($types['types'] as $type) {
                $typeID = (int) getTypeID($type['href']);
                $name = $type['name'];
                $exists = $mdb->count('information', ['type' => 'typeID', 'id' => $typeID]);
                if ($exists > 0) {
                    continue;
                }
                Util::out("Discovered item: {$name}");
                ++$newItems;
                $mdb->insertUpdate('information', ['type' => 'typeID', 'id' => $typeID], ['name' => $name, 'groupID' => $groupID, 'lastCrestUpdate' => new MongoDate(1)]);
            }
        }
    }
    $next = @$groups['next']['href'];
    if ($next != null) {
        $groups = CrestTools::getJSON($next);
    }
function bindunionsave()
{
    global $db;
    $u_type = be("all", "u_type");
    $d_type = be("all", "d_type");
    $typeid = getTypeID($u_type);
    $backurl = be("all", "backurl");
    if (isN($d_type)) {
        $sql = "select * from {pre}vod_type where t_id =" . $typeid;
        $row = $db->getRow($sql);
        if ($row) {
            if (strpos("," . $row["t_union"], $u_type) > 0) {
                $newunionids = replaceStr($row["t_union"], $u_type, "");
                $newunionids = replaceStr($newunionids, ",,", ",");
                if (trim($newunionids) == ",") {
                    $newunionids = "";
                }
                $db->Update("{pre}vod_type", array("t_union"), array($newunionids), "t_id=" . $row["t_id"]);
            }
        }
        unset($row);
    } else {
        $sql = "select * from {pre}vod_type where t_id =" . $d_type;
        $row = $db->getRow($sql);
        if ($row) {
            if (isN($row["t_union"]) || strpos("," . $row["t_union"], "," . $u_type . ",") <= 0) {
                $newunionids = $row["t_union"] . "," . $u_type . ",";
                $newunionids = replaceStr($newunionids, ",,", ",");
                if (trim($newunionids) == ",") {
                    $newunionids = "";
                }
                $db->Update("{pre}vod_type", array("t_union"), array($newunionids), "t_id=" . $row["t_id"]);
            }
        }
        unset($row);
    }
    echo "ok";
}