-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLoLNexusParser.php
134 lines (111 loc) · 3.46 KB
/
LoLNexusParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
<?php
/*
* This file is part of the "EloGank League of Legends Replay Downloader" package.
*
* https://github.com/EloGank/lol-replay-downloader
*
* For the full license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Example\Utils;
use Buzz\Browser;
use EloGank\Replay\Output\OutputInterface;
/**
* @author Sylvain Lorinet <[email protected]>
*/
class LoLNexusParser
{
const REGION_NA = 1;
const REGION_EUW = 2;
const REGION_EUNE = 3;
const REGION_BR = 4;
const REGION_TR = 5;
const REGION_RU = 6;
const REGION_LAN = 7;
const REGION_LAS = 8;
const REGION_OCE = 9;
/**
* @var string
*/
protected $region;
/**
* @var int
*/
protected $gameId;
/**
* @var string
*/
protected $encryptionKey;
protected static $serverNames = [
'NA', 'EUW', 'EUNE', 'BR', 'TR', 'RU', 'LAN', 'LAS', 'OCE'
];
/**
* Select the first player in the recent games list for a selected region
*
* @param int $regionId
* @param OutputInterface $output
*/
public function parseRandom($regionId = self::REGION_EUW, OutputInterface $output = null)
{
$hasOutput = null != $output;
if ($hasOutput) {
$output->write("Parsing homepage...\t\t\t");
}
$buzz = new Browser();
$response = $buzz->get('http://www.lolnexus.com/recent-games?filter-region=' . $regionId . '&filter-sort=1');
if (!preg_match('/<a class="green-button scouter-button" href="\/' . self::$serverNames[$regionId - 1] . '\/search\?name=(.*)">Live Game<\/a>/', $response->getContent(), $matches)) {
throw new \RuntimeException('Cannot parse LoL Nexus website, maybe down ?');
}
if ($hasOutput) {
$output->writeln('<info>OK</info>');
}
$this->parsePlayer($regionId, $matches[1], $output, $buzz);
}
/**
* @param int $regionId
* @param string $playerName
* @param null|OutputInterface $output
* @param null|Browser $buzz
*/
public function parsePlayer($regionId, $playerName, OutputInterface $output = null, $buzz = null)
{
if (null == $buzz) {
$buzz = new Browser();
}
$hasOutput = null != $output;
if ($hasOutput) {
$output->write("Parsing game page...\t\t\t");
}
$response = $buzz->get('http://www.lolnexus.com/ajax/get-game-info/' . self::$serverNames[$regionId - 1] . '.json?name=' . urlencode($playerName));
if (!preg_match('/lrf:\/\/spectator [0-9a-zA-Z.:]+ (.*) ([0-9]+) ([A-Z0-9]+) [0-9\.]+/', $response->getContent(), $matches)) {
throw new \RuntimeException('Cannot parse LoL Nexus game page, the game may be ended (sometimes LoLNexus\'s cache is bad), please retry.');
}
if ($hasOutput) {
$output->writeln('<info>OK</info>');
}
$this->encryptionKey = $matches[1];
$this->gameId = $matches[2];
$this->region = $matches[3];
}
/**
* @return string
*/
public function getEncryptionKey()
{
return $this->encryptionKey;
}
/**
* @return int
*/
public function getGameId()
{
return $this->gameId;
}
/**
* @return string
*/
public function getRegion()
{
return $this->region;
}
}