-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeUGC.php
194 lines (155 loc) · 5.54 KB
/
ScrapeUGC.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Scrapes two UGC team pages (denoted as "our team" and "their team") and returns
* the name of the team and structured lists of the verified roster of "player
* name" and "Steam ID"
* Jacky Liang July 18 2015
*/
class ScrapeUGC {
// XPath to get the team abbreviation and name
const TEAM_ABB_XPATH = '//*[@id="wrapper"]/section/div/div[1]/div[1]/div/div/h1/text()';
const TEAM_NAME_XPATH = '//*[@id="wrapper"]/section/div/div[1]/div[1]/div/div/h1/b';
// XPath to get the Steam Name and ID
const NAME_AND_ID_XPATH = '//*[@id="wrapper"]//div[@class="col-md-12"]//h5/b';
// Two URLs - our and their teams
private $ourTeamURL;
private $theirTeamURL;
// Two HTML objects - our and their team's
private $ourTeamHTML;
private $theirTeamHTML;
// Two associative arrays - our and their team's
private $ourTeamPlayers = array();
private $theirTeamPlayers = array();
// Two team names - our and their team's
private $ourTeamName;
private $theirTeamName;
public function ScrapeUGC($ourTeamURL, $theirTeamURL) {
// Silence the DOM errors
libxml_use_internal_errors(true);
// Store the two URLs - ours and theirs
$this->ourTeamURL = $ourTeamURL;
$this->theirTeamURL = $theirTeamURL;
// Set HTTP response header to plain text for debugging output
header("Content-type: text/plain");
// Generate our team's HTML file
$this->ourTeamHTML = new DomDocument;
$this->ourTeamHTML->loadHTMLFile($this->ourTeamURL);
// Generate their team's HTML file
$this->theirTeamHTML = new DomDocument;
$this->theirTeamHTML->loadHTMLFile($this->theirTeamURL);
$this->ourTeamPlayers = $this->scrapeUGCTeam($this->ourTeamHTML);
$this->theirTeamPlayers = $this->scrapeUGCTeam($this->theirTeamHTML);
$this->ourTeamName = $this->scrapeUGCTeamName($this->ourTeamHTML);
$this->theirTeamName = $this->scrapeUGCTeamName($this->theirTeamHTML);
}
/**
* Gets our team's URL
* @return string Our Team's URL
*/
public function getOurTeamURL(){
return $this->ourTeamURL;
}
/**
* Get their team's URL
* @return string Their Team's URL
*/
public function getTheirTeamURL(){
return $this->theirTeamURL;
}
/**
* When supplied a valid UGC team URL, return the team name
* @param $url UGC Team URL
* @return string Team name in the form of 'Team Abbreviation' - 'Team Name'
*/
private function scrapeUGCTeamName($url){
$xpath = new DomXPath($url);
$teamAbbNode = $xpath->query(ScrapeUGC::TEAM_ABB_XPATH);
$teamNameNode = $xpath->query(ScrapeUGC::TEAM_NAME_XPATH);
$teamAbb = '';
$teamName = '';
// Save the team's abbreviation
foreach($teamAbbNode as $node) {
$teamAbb = trim($node->nodeValue);
break;
}
// Save the team's name
foreach($teamNameNode as $node) {
$teamName = trim($node->nodeValue);
break;
}
return $teamAbb . ' - ' . $teamName;
}
/**
* When supplied a valid UGC team URL, return an associative array
* of the UGC team of Steam Name and Steam ID
* @param $url UGC Team URL
* @return array Associative array of Steam Name -> Steam ID
*/
private function scrapeUGCTeam($url){
$xpath = new DomXPath($url);
// XPath to get the Steam names and Steam IDs
// Get the Steam names and Steam IDs using the XPath
$steamNameAndIDs = $xpath->query(ScrapeUGC::NAME_AND_ID_XPATH);
// Temporary lists to store the Steam Names and Steam IDs
$steamNameList = array();
$steamIDList = array();
foreach ($steamNameAndIDs as $id => $node) {
$steamName = "";
$steamID = "";
// Store Steam name first (which is the even-numbered item)
// in the scraped list. Then store the Steam ID (which is
// the odd-numbered item).
if($id % 2 == 0) {
$steamName = $node->nodeValue;
} else if ($id % 2 == 1) {
$steamID = $node->nodeValue;
}
if(!empty($steamName)) {
// Add each Steam name onto our team's list
array_push($steamNameList, $steamName);
} else if(!empty($steamID)){
// Add each Steam ID onto our team's list
array_push($steamIDList, $steamID);
}
}
// Combine Steam ID and Steam Name to team's associative
// list
$team = array_combine(
$steamNameList,
$steamIDList
);
return $team;
}
/**
* Returns an associative array of our team
* where the key is the Steam name and the
* value is the Steam ID
* @return array Associative array of our team
*/
public function getOurTeam(){
return $this->ourTeamPlayers;
}
/**
* Returns an associative array of their team
* where the key is the Steam name and the
* value is the Steam ID
* @return array
*/
public function getTheirTeam(){
return $this->theirTeamPlayers;
}
/**
* Get our team name
* @return string Our team name
*/
public function getOurTeamName(){
return $this->ourTeamName;
}
/**
* Get their team name
* @return string Their team name
*/
public function getTheirTeamName(){
return $this->theirTeamName;
}
}