Ru-MaNGOS

Ru-MaNGOS (http://mangos.ytdb.ru/index.php)
-   Tools (http://mangos.ytdb.ru/forumdisplay.php?f=22)
-   -   [PHP] Wowhead Items Parser (http://mangos.ytdb.ru/showthread.php?t=4936)

Shadez 26.07.2011 17:32

[PHP] Wowhead Items Parser
 
Скрипт парсит итемы с вовхеда, генерирует sql. Попытался учесть данные по максимуму. Написан на коленке, строго не судите.
Код:

<?php
class WHItemParser {
        private $id = 0;
        private $data = '';
        private $file = '';
        private $sql = '';
        private $item_stats = array(
                'health' => 1,
                'mana' => 2,
                'agi' => 3,
                'str' => 4,
                'int' => 5,
                'spi' => 6,
                'sta' => 7,
                'energy' => 8,
                'rage' => 9,
                'focus' => 10,
                'dodgertng' => 13,
                'parryrtng' => 14,
                'mlehitrtng' => 16,
                'rgdhitrtng' => 17,
                'splhitrtng' => 18,
                'mlecritstrkrtng' => 19,
                'rgdcritstrkrtng' => 20,
                'splcritstrkrtng' => 21,
                '_mlehitrtng' => 22,
                '_rgdhitrtng' => 23,
                '_splhitrtng' => 24,
                '_mlecritstrkrtng' => 25,
                '_rgdcritstrkrtng' => 26,
                '_splcritstrkrtng' => 27,
                'mlehastertng' => 28,
                'rgdhastertng' => 29,
                'splhastertng' => 30,
                'hitrtng' => 31,
                'critstrkrtng' => 32,
                '_hitrtng' => 33,
                '_critstrkrtng' => 34,
                'resirtng' => 35,
                'hastertng' => 36,
                'exprtng' => 37,
                'atkpwr' => 38,
                'rgdatkpwr' => 39,
                'splheal' => 41,
                'spldmg' => 42,
                'manargn' => 43,
                'armorpenrtng' => 44,
                'splpwr' => 45,
                'healthrgn' => 46,
                'splpen' => 47,
                'mastrtng' => 49,
                'firres' => 51,
                'frores' => 52,
                'holres' => 53,
                'shares' => 54,
                'natres' => 55,
                'arcres' => 56,
        );
       
        public function WHItemParser($id, $file = 'query.sql') {
                $this->id = $id;
                $this->file = $file;
                $ch = curl_init('http://www.wowhead.com/item=' . $this->id . '&xml');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $this->data = curl_exec($ch);
                if ($this->data == '<?xml version="1.0" encoding="UTF-8"?><wowhead><error>Item not found!</error></wowhead>')
                        return false;
                curl_close($ch);
                $this->Init()->ParseData();
        }
       
        private function Init() {
                $this->sql = 'INSERT INTO `item_template` SET `entry` = ' . $this->id;
                return $this;
        }
       
        public function __destruct() {
                $this->data = '';
                $this->id = 0;
                $this->sql = '';
                unset($this->data, $this->id, $this->sql);
                return true;
        }
       
        private function ParseData() {
                if(!$this->data)
                        return false;
                $this->parseName()
                        ->parseLevel()
                        ->parseQuality()
                        ->parseClassSubClass()
                        ->parseDispalyId()
                        ->parseSlotId()
                        ->parseRequirements()
                        ->parseStats()
                        ->parseTooltip()
                        ->finish();
        }
       
        private function findRegex($pattern) {
                if (preg_match($pattern, $this->data, $arr)) {
                        return $arr[1];
                }
                return '';
        }
       
        private function parseName() {
                $this->sql .= ', `name` = \'' . str_replace("'", "''", $this->findRegex('/\<name\><!\[CDATA\[(.+?)\]\]><\/name\>/')) . '\'';
                return $this;
        }
       
        private function parseLevel() {
                $this->sql .= ', `ItemLevel` = ' . $this->findRegex('/\<level\>(.+?)<\/level\>/');
                return $this;
        }
       
        private function parseQuality() {
                $this->sql .= ', `Quality` = ' . $this->findRegex('/\<quality id\="(.+?)"\>/');
                return $this;
        }
       
        private function parseClassSubClass() {
                $this->sql .= ', `class` = ' . $this->findRegex('/\<class id\="(.+?)"\>/');
                $this->sql .= ', `subclass` = ' . $this->findRegex('/\<subclass id\="(.+?)"\>/');
                return $this;
        }
       
        private function parseDispalyId() {
                $this->sql .= ', `displayid` = ' . $this->findRegex('/\<icon displayId\="(.+?)"\>/');
                return $this;
        }
       
        private function parseSlotId() {
                $this->sql .= ', `InventoryType` = ' . $this->findRegex('/<inventorySlot id="(.+?)">/');
                return $this;
        }
       
        private function parseRequirements() {
                $json = '{' . $this->findRegex('/<jsonEquip\>\<\!\[CDATA\[(.+?)\]\]\>\<\/jsonEquip\>/') . '}';
                $data = json_decode($json, true);
                if (isset($data['maxlevel'])) {
                        $this->sql .= ', `maxcount` = 1';
                }
                if (isset($data['reqlevel'])) {
                        $this->sql .= ', `RequiredLevel` = ' . $data['reqlevel'];
                }
                if (isset($data['reqspell'])) {
                        $this->sql .= ', `requiredspell` = ' . $data['reqspell'];
                }
                if (isset($data['nsockets']) && $data['nsockets'] > 0) {
                        for($i = 1; $i <= $data['nsockets']; ++$i) {
                                $this->sql .= ', `socketColor_' . $i . '` = ' . $data['socket' . $i];
                        }
                        $this->sql .= ', `socketBonus` = ' . $data['socketbonus'];
                }
                if (isset($data['itemset'])) {
                        $this->sql .= ', `itemset` = ' . $data['itemset'];
                }
                if (isset($data['dura'])) {
                        $this->sql .= ', `MaxDurability` = ' . $data['dura'];
                }
                if (isset($data['armor'])) {
                        $this->sql .= ', `armor` = ' . $data['armor'];
                }
                if (isset($data['races'])) {
                        $this->sql .= ', `AllowableRace` = ' . $data['races'];
                }
                if (isset($data['classes'])) {
                        $this->sql .= ', `AllowableClass` = ' . $data['classes'];
                }
                if (isset($data['sellprice'])) {
                        $this->sql .= ', `SellPrice` = ' . $data['sellprice'];
                }
                return $this;
        }
       
        private function parseStats() {
                $json = '{' . $this->findRegex('/<jsonEquip\>\<\!\[CDATA\[(.+?)\]\]\>\<\/jsonEquip\>/') . '}';
                $data = json_decode($json, true);
                $statid = 0;
                $b = false;
                foreach($data as $key => $value) {
                        if (isset($this->item_stats[$key])) {
                                ++$statid;
                                $b = true;
                                $this->sql .= ', `stat_type' . $statid . '` = ' . $this->item_stats[$key] . ', `stat_value' . $statid . '` = ' . $value;
                                if ($statid > 10) {
                                        $this->sql .= ', `StatsCount` = 10';
                                        return $this;
                                }
                        }
                }
                if($b)
                        $this->sql .= ', `StatsCount` = ' . $statid;
                return $this;
        }
       
        private function parseTooltip() {
                $data = $this->findRegex('/<htmlTooltip\>\<\!\[CDATA\[(.+?)\]\]\>\<\/htmlTooltip\>/');
                $triggers = array(
                        'Use' => 0,
                        'Equip' => 1,
                        'Chance on hit' => 2,
                );
                $sid = 1;
                foreach($triggers as $tr => $val) {
                        if (preg_match_all('/' . $tr . ':(.+?)/', $data, $arr)) {
                                if (preg_match_all('/spell=(.+?)"/', $data, $ids)) {
                                        if (preg_match_all('/\([0-9] Min/', $data, $coold)) {
                                                foreach($coold[0] as &$k) {
                                                        $k = str_replace(array('(', ' Min'), null, $k);
                                                }
                                        }
                                        for($i = 0; $i < sizeof($ids[1]); ++$i) {
                                                $this->sql .= ', `spellid_' . $sid . '` = ' . $ids[1][$i] . ', `spelltrigger_' . $sid . '` = ' . $triggers[$tr] . ', `spellcooldown_' . $sid . '` = ' . (is_array($coold) ? $coold[0][$i] * 1000 : 0);
                                                $sid++;
                                        }
                                }
                        }
                }
                if (preg_match('/<span class="q">&quot;(.+?)&quot;<\/span>/', $data, $arr)) {
                        $this->sql .= ', `description` = \'' . str_replace("'", "''", $arr[1]) . '\'';
                }
                if (preg_match('/Requires <a href="http\:\/\/www.wowhead.com\/skill\=(.+?)" class="q1">(.+?)<\/a> \((.+?)\)/', $data, $skills)) {
                        $this->sql .= ', `RequiredSkill` = ' . $skills[1] . ', `RequiredSkillRank` = ' . $skills[3];
                }
                $rep_ranks = array(
                        'hated' => 0,
                        'hostile' => 1,
                        'unfriendly' => 2,
                        'neutral' => 3,
                        'friendly' => 4,
                        'honored' => 5,
                        'revered' => 6,
                        'exalted' => 7
                );
                if (preg_match('/Requires <a href="http:\/\/www.wowhead.com\/faction=(.+?)" class="q1">(.+?)<\/a> - (.+?)<\/td>/', $data, $rep)) {
                        if (isset($rep_ranks[strtolower($rep[3])]))
                                $this->sql .= ', `RequiredReputationFaction` = ' . $rep[1] . ', `RequiredReputationRank` = ' . $rep_ranks[strtolower($rep[3])];
                }
                if (preg_match('/[0-9]{1,} Slot Bag/', $data, $slot)) {
                        $this->sql .= ', `ContainerSlots` = ' . ((int)str_replace(' Slot Bag', null, $slot[0]));
                }
                $binds = array(
                        1 => 'Binds when picked up', 2 => 'Binds when equipped', 3 => 'Binds when used', 4 => 'Quest item'
                );
                $bond = -1;
                foreach($binds as $bId => $b) {
                        if (preg_match('/' . $b . '/', $data, $arr)) {
                                $bond = $bId;
                        }
                }
                if ($bond == -1) {
                        if (preg_match('/Battle\.net/', $data, $arr)) {
                                $bond = 1; // какой ид бондинга для боа итемов?
                        }
                        else
                                $bond = 0;
                }
                $this->sql .= ', `bonding` = ' . $bond;
                return $this;
        }
       
        private function finish() {
                $this->sql .= ";\n";
                file_put_contents($this->file, $this->sql, FILE_APPEND);
                return $this;
        }
}
?>

Использование:
Код:

<?php
$wh = new WHItemParser(32548, 'items.sql');
?>


Den 26.07.2011 18:12

В пхп:
if (isset( $data***91;'reqlevel'***93; )
Мб лучше кодом ? ;)

Shadez 26.07.2011 18:35

Поправил, спасибо, не заметил)

SergiK_KilleR 22.08.2011 15:49

альтернатива вовхеду
http://blizzard.github.com/api-wow-docs/#id3382076

Код:

URL = Host + "/api/wow/item/" + ItemId
Цитата:

GET /api/wow//item/38268 HTTP/1.1
Host: us.battle.net
<http headers>


Текущее время: 17:18. Часовой пояс GMT +3.

ru-mangos.ru - Русское сообщество MaNGOS