-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglivetable.cpp
More file actions
308 lines (296 loc) · 10.1 KB
/
glivetable.cpp
File metadata and controls
308 lines (296 loc) · 10.1 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <time.h>
#include <mysql/mysql.h>
#include <string.h>
#include "shtml.h"
#include <stdarg.h>
#define BUFFER_SIZE 1024
static char host_name[BUFFER_SIZE];
static char user_name[BUFFER_SIZE];
static char password [BUFFER_SIZE];
static char db_name [BUFFER_SIZE];
static int port_number;
static int DEBUG=0;
static MYSQL *conn;
string teamid, teamname,group;
string tleague;
int pos,play,hw,hd,hl,aw,ad,al,ow,od,ol,tabletype;
int hf,ha,af,aa,of,oa;
int gd,pts;
string teamlistname="'0'";
int after_equal(char * c){
int i=0;
for(;c[i]!='\0'&&c[i]!='=';i++);
return ++i;
}
void trim(char * c)
{
char buf[BUFFER_SIZE];
char * start,*end;
strcpy(buf,c);
start=buf;
while(isspace(*start)) start++;
end=start;
while(!isspace(*end)) end++;
*end='\0';
strcpy(c,start);
}
bool read_buf(char * buf,const char * key,char * value)
{
if (strncmp(buf,key, strlen(key)) == 0)
{
strcpy(value, buf + after_equal(buf));
trim(value);
//if (DEBUG) printf("%s\n",value);
return 1;
}
return 0;
}
void read_int(char * buf,const char * key,int * value)
{
char buf2[BUFFER_SIZE];
if (read_buf(buf,key,buf2))
sscanf(buf2, "%d", value);
}
void write_log(const char *fmt, ...)
{
va_list ap;
char buffer[4096];
char times[20];
struct tm *sTm;
time_t now = time (0);
sTm = gmtime (&now);
strftime (times, sizeof(times), "%Y-%m-%d %H:%M:%S", sTm);
sprintf(buffer,"/var/log/footygoat/table.log");
FILE *fp = fopen(buffer, "a+");
if (fp==NULL)
{
fprintf(stderr,"openfile error!\n");
system("pwd");
}va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
fprintf(fp,"%s live \t %s\n",times,buffer);
if (DEBUG) printf("%s\n",buffer);
va_end(ap);
fclose(fp);
}
bool init_mysql_conf()
{
FILE *fp=NULL;
char buf[BUFFER_SIZE];
host_name[0]=0;
user_name[0]=0;
password[0]=0;
db_name[0]=0;
port_number=3306;
fp = fopen("/etc/footygoat/footygoat.conf", "r");
if(fp!=NULL)
{
while (fgets(buf, BUFFER_SIZE - 1, fp))
{
read_buf(buf,"F_HOST_NAME",host_name);
read_buf(buf, "F_USER_NAME",user_name);
read_buf(buf, "F_PASSWORD",password);
read_buf(buf, "F_DB_NAME",db_name);
read_int(buf , "F_PORT_NUMBER", &port_number);
}
return true;
// fclose(fp);
}
else
{
write_log("Cannot open file 'footygoat.conf'");
return false;
}
}
bool executesql(const char * sql)
{
if (DEBUG) cout<<sql<<endl;
if (mysql_real_query(conn,sql,strlen(sql)))
{
//sleep(20);
write_log("Error in sql %s:%s",sql,mysql_error(conn));
//conn=NULL;
return false;
}
else
return true;
}
int init_mysql() {
if(conn==NULL)
{
conn=mysql_init(NULL); // init the database connection
/* connect the database */
const char timeout=30;
mysql_options(conn,MYSQL_OPT_CONNECT_TIMEOUT,&timeout);
if(!mysql_real_connect(conn,host_name,user_name,password,db_name,port_number,0,0))
{
write_log("Error init mysql: %s",mysql_error(conn));
//sleep(20);
return false;
}
}
if (!executesql("set names utf8"))
return false;
return true;
}
void addTeam(string tid, string tname, int type)
{
char sql[1000];
if (tid.empty())
{
//return;
if (type)
{
sprintf(sql,"Update f_teams set team_pos=%d,`team_op`=%d,`team_ow`=%d,`team_od`=%d,`team_ol`=%d,`team_of`=%d,`team_oa`=%d,`team_hw`=%d,`team_hd`=%d,`team_hl`=%d,`team_hf`=%d,`team_ha`=%d,`team_aw`=%d,`team_ad`=%d,`team_al`=%d,`team_af`=%d,`team_aa`=%d,`team_gd`=%d,`team_pts`=%d,team_updated=1,team_date=NOW() where team_league='%s' and team_name='%s'",pos,play,ow,od,ol,of,oa,hw,hd,hl,hf,ha,aw,ad,al,af,aa,gd,pts,tleague.c_str(),tname.c_str());
}
else
{
sprintf(sql,"Update f_teams set `team_pos`=%d,`team_op`=%d,`team_ow`=%d,`team_od`=%d,`team_ol`=%d,`team_of`=%d,`team_oa`=%d,`team_gd`=%d,`team_pts`=%d,team_updated=1,team_date=NOW() where team_league='%s' and team_name='%s'",pos,play,ow,od,ol,of,oa,gd,pts,tleague.c_str(),tname.c_str());
}
teamlistname+=",'"+tname+"'";
}
else
{
if (type)
{
sprintf(sql,"INSERT INTO `f_teams` (`team_id`,`team_name`,`team_group`,`team_league`,`team_pos`,`team_op`,`team_ow`,`team_od`,`team_ol`,`team_of`,`team_oa`,`team_hw`,`team_hd`,`team_hl`,`team_hf`,`team_ha`,`team_aw`,`team_ad`,`team_al`,`team_af`,`team_aa`,`team_gd`,`team_pts`,team_date,team_updated) VALUES ('%s','%s','%s','%s','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d','%d',NOW(),1) ON DUPLICATE KEY UPDATE team_group='%s',team_league='%s',team_pos='%d',team_op=%d,team_ow=%d,team_od=%d,team_ol=%d,team_of=%d,team_oa=%d,team_hw=%d,team_hd=%d,team_hl=%d,team_hf=%d,team_ha=%d,team_aw=%d,team_ad=%d,team_al=%d,team_af=%d,team_aa=%d,team_gd=%d,team_pts=%d,team_date=NOW(),team_updated=1;",tid.c_str(),tname.c_str(),group.c_str(),tleague.c_str(),pos,play,ow,od,ol,of,oa,hw,hd,hl,hf,ha,aw,ad,al,af,aa,gd,pts,group.c_str(),tleague.c_str(),pos,play,ow,od,ol,of,oa,hw,hd,hl,hf,ha,aw,ad,al,af,aa,gd,pts);
}
else
{
sprintf(sql,"INSERT INTO `f_teams` (`team_id`,`team_name`,`team_group`,`team_league`,`team_pos`,`team_op`,`team_ow`,`team_od`,`team_ol`,`team_of`,`team_oa`,`team_gd`,`team_pts`,team_date,team_updated) VALUES ('%s','%s','%s','%s','%d','%d','%d','%d','%d','%d','%d','%d','%d',NOW(),1) ON DUPLICATE KEY UPDATE team_group='%s',team_league='%s',team_pos='%d',team_op=%d,team_ow=%d,team_od=%d,team_ol=%d,team_of=%d,team_oa=%d,team_gd=%d,team_pts=%d,team_date=NOW(),team_update=1;",tid.c_str(),tname.c_str(),group.c_str(),tleague.c_str(),pos,play,ow,od,ol,of,oa,gd,pts,group.c_str(),tleague.c_str(),pos,play,ow,od,ol,of,oa,gd,pts);
}
}
//cout<<"Chay sql"<<endl;
//write_log(sql);
executesql(sql);
}
void resetTable(string sLeague, string tids, string tnames)
{
char sql[500];
sprintf(sql,"update f_teams set team_updated=0,team_date=NOW() where (team_league='%s') and (team_id not in (%s)) and (team_name not in (%s))",sLeague.c_str(),tids.c_str(),tnames.c_str());
executesql(sql);
}
void getLiveTable(string sLeague)
{
shtml m,t,n,nh;
string teamlist="0";
//m.loadfromfile("tables.htm");
if (!m.loadFromURL((string("http://espnfc.com/livetables?league=")+sLeague).c_str())) return;
m.removeBetween("<!--","-->",-1);
t=m.cutTagByName("div");
while (!t.isEmpty())
{
if (t.containAttr("bg-elements"))
{
//sh.setContent(t.getContent());
m=t;
break;
}
if (m.isEmpty()) break;
t=m.cutTagByName("div");
}
//sh.viewContent();
m.retainTagByName("div",2);
m.retainTagByName("div");
m.retainTagByName("div",2);
m.retainTagByName("div");
m.retainTagByName("div",2);
t=m.cutTagByName("div");
while (!t.isEmpty())
{
t.retainTagByName("table");
n.setContent(t.getAttr());
group=n.getBetween("Live_false_group_","\"");
if (!group.empty())
{
//cout<<"Group "<<group<<endl;
n=t.cutTagByName("thead");
if (n.contain("Overall")) tabletype=1;
else tabletype=0;
//t.retainTagByName("tbody");
n=t.cutTagByName("tr");
while (!n.isEmpty())
{
nh=n.cutTagByName("td");
pos=nh.toInt();
nh=n.cutTagByName("td");
nh=n.cutTagByName("td");
if (nh.containTag("a")) nh.retainTagByName("a");
nh.replace("'","\\'",-1);
teamname = nh.getContent();
nh.setContent(nh.getAttr());
teamid = nh.getBetween("/id/","/");
nh=n.cutTagByName("td");
play=nh.toInt();
nh=n.cutTagByName("td");
ow=nh.toInt();
nh=n.cutTagByName("td");
od=nh.toInt();
nh=n.cutTagByName("td");
ol=nh.toInt();
nh=n.cutTagByName("td");
of=nh.toInt();
nh=n.cutTagByName("td");
oa=nh.toInt();
nh=n.cutTagByName("td");//remove
if (tabletype==1)
{
nh=n.cutTagByName("td");
hw=nh.toInt();
nh=n.cutTagByName("td");
hd=nh.toInt();
nh=n.cutTagByName("td");
hl=nh.toInt();
nh=n.cutTagByName("td");
hf=nh.toInt();
nh=n.cutTagByName("td");
ha=nh.toInt();
nh=n.cutTagByName("td");//remove
nh=n.cutTagByName("td");
aw=nh.toInt();
nh=n.cutTagByName("td");
ad=nh.toInt();
nh=n.cutTagByName("td");
al=nh.toInt();
nh=n.cutTagByName("td");
af=nh.toInt();
nh=n.cutTagByName("td");
aa=nh.toInt();
nh=n.cutTagByName("td");
}
nh=n.cutTagByName("td");
gd=nh.toInt();
nh=n.cutTagByName("td");
pts=nh.toInt();
if (DEBUG) cout<<pos<<"/"<<teamid<<"/"<<teamname<<"/"<<play<<"/"<<ow<<"/"<<od<<"/"<<ol<<"/"<<of<<"/"<<oa<<"/"<<hw<<"/"<<hd<<"/"<<hl<<"/"<<hf<<"/"<<ha<<"/"<<aw<<"/"<<ad<<"/"<<al<<"/"<<af<<"/"<<aa<<"/"<<gd<<"/"<<pts<<endl;
if (!teamid.empty())
{
teamlist+=","+teamid;
}
addTeam(teamid,teamname,tabletype);
//cout<<"Continue"<<endl;
n=t.cutTagByName("tr");
}
}
t=m.cutTagByName("div");
}
//m.viewContent();
resetTable(sLeague, teamlist,teamlistname);
}
int main(int argc, char** argv)
{
if (argc>1)
{
if (argc>2) DEBUG=true;
tleague = argv[1];
init_mysql_conf();
init_mysql();
getLiveTable(argv[1]);
}
else
{
cout<<"Usage: glivetable <league_id>"<<endl;
cout<<"by Tran Huu Nam, huunam0@gmail.com"<<endl;
}
return 0;
}