-
Notifications
You must be signed in to change notification settings - Fork 1
/
so_chat_parser.php
161 lines (139 loc) · 5.49 KB
/
so_chat_parser.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
<?php
$minimumRepeat=2; //importance treshold, overridden by dynamic adjust
isset($_GET['where']) && $room=$_GET['where'];
$room=preg_replace('/[^0-9]/', '', $room);
$room===$_GET['where'] or die('404 does not exist!');
isset($_GET['word']) && $word=$_GET['word'];
$word=preg_replace('/[^$\pL@ \pN_.:;!?#^-]/i', '', $word);
''!=$word or die('404 word does not exist!');
$requestURL='http://chat.stackoverflow.com/search?q=';
$requestURL.=urlencode($word);
if ($room != '') {
$requestURL.='&Room='.$room;
$room.='/'; //used in redirect
}
$requestURL.='&page=1&pagesize=100&sort=newest';
$messageNumber=0;
try {
$data=file($requestURL);
if (count($data)<30) {
header('Location: http://chat.stackoverflow.com/rooms/'.$room); //page has unknown output
die();
}
} catch (Exception $e) {
$range='Wait';
$rangeDetailed="! Too many requests.";
$statisticsDetails="Nope";
goto theEndOfTheRoad; //don't worry, carry on
}
$roomname='All Rooms';
if ($room!='') {
$roomname=current(preg_grep('~class="searchroom~i', $data));
preg_match('~href=[^>]+>(.+?)</a>~i', $roomname, $matches);
$roomname=$matches[1];
}
$messageNumber=preg_grep('~<p>([0-9]+) messages? found</p>~i', $data);
preg_match('~>([0-9]+)~', current($messageNumber), $allTimeMsg);
$messageNumber=$allTimeMsg[1]+0;
if ($messageNumber<1) {
$range='';
$avg='never';
$avgPeriod=' ever';
$rangeDetailed="The begining of time <small><small>clock on chat.stackoverflow server</small></small> — Today, Current Time";
$statisticsDetails="Zero Results";
goto theEndOfTheRoad;
}
$start=preg_grep('~<div class="timestamp">~i', $data);
$end=trim(strip_tags(array_shift($start)));
count($start)<1 ? $start=$end : $start=trim(strip_tags(array_pop($start)));
$start=new DateTime(str_replace("'", '20', $start));
$end=new DateTime(str_replace("'", '20', $end));
$interval=$start->diff($end);
$format="F jS Y";
$rangeDetailed=' ( '.$start->format($format).' — '.$end->format($format).' )';
if ($interval->days<=1) {
$range='1 day: ';
$format="l ( F jS Y";
$rangeDetailed=$start->format($format).' )';
} elseif ($interval->days>92) {
$range=round($interval->days/30.2, 1).' months';
} else {
$range=$interval->days.' days';
}
$users=preg_grep('~<div class="username"><a href="/users/~', $data);
$count=[];
$names=[];
$shownMessages=0;
foreach ($users as $user) {
preg_match('~/users/(-?\d+)/[^>]+>([^<]+)~', $user, $m);
isset($names[$m[1]]) || $names[$m[1]]=$m[2];
@$count[$m[1]]++; //fu
$shownMessages++;
}
arsort($count);
$avg=($end->format("U")-$start->format("U"))/$shownMessages; //seconds
$avgPeriod=' seconds';
if ($avg>=120) {
$avg/=60;
$avgPeriod=' minutes';
if ($avg>=120) {
$avg/=60;
$avgPeriod=' hours';
if ($avg>=48) {
$avg/=24;
$avgPeriod=' days';
}
}
}
$avg=abs(round($avg)); //heck
$list=[];
$other=0; //counts messages
$totalUsersShown=count($names);
if ($totalUsersShown<=12) $minimumRepeat=1; //dynamic userlist adjustment
$topUsers=0;
$otherUsers=0; //counts users
foreach ($count as $id=>$num):
if ($topUsers==10) { //dynamic userlist adjustment
if ($minimumRepeat<$num) $minimumRepeat=$num;
}
if ($num>=$minimumRepeat) {
$topUsers++;
$name=$names[$id];
//if ($id==-2) // FEEDS
$tlist=[];
$tlist['y']=$num;
$tlist['label']="$name";
$tlist["place"]="#$topUsers";
$percent=round($num*100/$shownMessages, 1);
$tlist["percent"]="$percent";
$uniqNameColor=substr(hash('md4', $id),1 ,6); //color algo #1
$tlist["color"]='#'.$uniqNameColor;
//$tlist["indexLabelFontColor"]='#'.substr(preg_replace('/[^0-9a-f]/', '', $name.$id), 0 ,6); //color algo #2
$font=round(16/strlen($name)*$percent);
if ($font<12) $font=12;
elseif ($font>24) $font=24;
$tlist['indexLabelFontSize']=$font;
if ($topUsers==1 and $totalUsersShown>2) $tlist['exploded']='true';
$list[]=$tlist;
} else {
$other+=$num; //add messages
$otherUsers++; //add users
}
endforeach;
if ($other) $list[]=['y'=>$other, 'label'=>"Others", "percent"=>(string)round($other*100/$shownMessages, 1)];
$allTimePercent=round($shownMessages*100/$messageNumber, 1);
if ($allTimePercent==100) $statisticsDetails='<b>All time data</b>';
else $statisticsDetails='<b>recent data</b> ('.$allTimePercent.'% of all time)';
if ($totalUsersShown>1) {
$statisticsDetails.=" showing $totalUsersShown users";
} else {
$statisticsDetails.=" only 1 user";
}
if ($otherUsers>0) $statisticsDetails.=" ($topUsers trending and $otherUsers others)";
$footerNotice='';
if ($minimumRepeat>1) {
$footerNotice="* Users reiterating the phrase less than $minimumRepeat times ";
$footerNotice.='<b>are not shown in the list</b>, and counted towards "Others" group for readability.';
}
$list=json_encode($list)."\n";
theEndOfTheRoad: