-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.cpp
More file actions
1019 lines (911 loc) · 23.2 KB
/
scrape.cpp
File metadata and controls
1019 lines (911 loc) · 23.2 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<cassert>
#include<set>
#include<string.h>
#include<type_traits>
#include<boost/numeric/ublas/matrix.hpp>
#include<boost/numeric/ublas/vector.hpp>
#include<boost/numeric/ublas/lu.hpp>
#include<boost/numeric/ublas/io.hpp>
#include "util.h"
#include "str.h"
#include "default.h"
#include "map.h"
#include "set.h"
#include "maybe.h"
using namespace std;
template<typename Func,typename Collection>
auto mapf(Func f,Collection const& c)->vector<decltype(f(*begin(c)))>{
vector<decltype(f(*begin(c)))> r;
for(auto elem:c) r|=f(elem);
return r;
}
template<typename T>
set<T> to_set(vector<T> const& v){
set<T> r;
for(auto e:v) r|=e;
return r;
}
template<typename T>
set<T>& operator|=(set<T>& a,set<T> const& b){
for(auto elem:b) a.insert(elem);
return a;
}
template<typename T>
vector<T> take(unsigned lim,vector<T> const& in){
vector<T> r;
for(unsigned i=0;i<lim;i++){
r|=in[i];
}
return r;
}
template<typename T>
vector<T> skip(unsigned i,vector<T> const& v){
vector<T> r;
for(;i<v.size();i++) r|=v[i];
return r;
}
string escape_to_commandline(char c){
//NOTE: If you ever want to run this as a cgi script or something, you will neeed to make this better.
stringstream ss;
if(c=='&' || c=='-' || c==';') ss<<"\\";
ss<<c;
return ss.str();
}
//remove spaces at the end of the string
string rstrip(string const& s){
int i=0;
while(s[s.size()-1-i]==' ') i++;
return s.substr(0,s.size()-i);
}
string escape_to_commandline(string const& s){
stringstream ss;
for(auto c:s) ss<<escape_to_commandline(c);
return ss.str();
}
//going to make a cache of downloaded pages...
void download_page(string const& url,string const& outfile){
static const string tmpfile="tmp1.tmp";
stringstream ss;
//moves the file into place when it's done instead of downloading it to the right place because otherwise if it gets killed halfway through you can end up with an invalid file.
ss<<"wget -q -O "<<tmpfile<<" "<<escape_to_commandline(url);
ss<<" && mv "<<tmpfile<<" "<<outfile;
//cout<<"Running: "<<ss.str()<<"\n";
auto r=system(ss.str().c_str());
assert(!r);
}
string scrape_webpage(string const& url){
string tmpfile="tmp1.tmp"; //todo: make this use the built-in tmpfile name generation.
download_page(url,tmpfile);
return slurp(tmpfile);
//probably want to delete the temprorary file...
}
string escape_url_to_file(string const& url){
stringstream ss;
for(char c:url){
if(c=='/'){
ss<<"sl";
}else{
ss<<c;
}
}
return ss.str();
}
void mkdir_p(string const& s){
//Note: If you care about security, change this to at the very least not let the argument create arbitrary shell commands.
stringstream ss;
ss<<"mkdir -p "<<s;
auto r=system(ss.str().c_str());
assert(!r);
}
string scrape_cached(string const& url){
static int made_dir=0;
if(!made_dir){
mkdir_p("this");
made_dir=1;
}
auto cache_file=string("this/")+escape_url_to_file(url);
try{
return slurp(cache_file);
}catch(...){}
cout<<"not found, redownloading:"<<url<<"\n";
cout.flush();
download_page(url,cache_file);
return slurp(cache_file);
}
vector<string> split_on(string const& haystack,string const& needle){
vector<string> r;
stringstream ss;
for(unsigned i=0;i<haystack.size();i++){
if(haystack.substr(i,needle.size())!=needle){
ss<<haystack[i];
}else{
r.push_back(ss.str());
ss.str("");
i+=needle.size()-1;
}
}
r.push_back(ss.str());
return r;
}
string quoted(string const& s){
stringstream ss;
ss<<"\""<<s<<"\"";
return ss.str();
}
string inside_td(string const& s){
return split_on(split_on(s,"<td>")[1],"</td>")[0];
}
//this is incomplete.
string interpret_html_escapes(string const& s){
stringstream ss;
for(unsigned i=0;i<s.size();i++){
const string m=" ";
if(s.substr(i,m.size())==m){
ss<<" ";
i+=m.size()-1;
}else{
ss<<s[i];
}
}
return ss.str();
}
typedef string Regional_type;
typedef string Match;
//typedef string Team;
class Team{
//the only complication with making this be an int or something is that sometimes offseason events have teams with "A" and "B" teams
string s;
public:
explicit Team(string a):s(a){
assert(s.size()>3 && s[0]=='f' && s[1]=='r' && s[2]=='c');
}
friend ostream& operator<<(ostream&,Team const&);
friend bool operator<(Team const&,Team const&);
friend bool operator==(Team const&,Team const&);
};
ostream& operator<<(ostream& o,Team const& t){ return o<<t.s; }
bool operator<(Team const& a,Team const& b){ return a.s<b.s; }
bool operator==(Team const& a,Team const& b){ return a.s==b.s; }
struct Event{
string name;
Regional_type type;
string venue,city,dates,url;
};
ostream& operator<<(ostream& o,Event const& e){
return o<<"Event("<<e.name<<")";
}
vector<Event> events(){
auto url="https://my.usfirst.org/myarea/index.lasso?event_type=FRC&year=2013";
auto page=scrape_cached(url);
//cout<<page.size()<<"\n";
auto pts=split_on(page,"<tr bgcolor=\"#FFFFFF\">");
//cout<<pts.size()<<" "<<skip(2,pts).size()<<"\n";
vector<Event> r;
//we may be skipping the championship event
for(auto part:skip(2,pts)){
//cout<<"yx ";
auto p=split_on(part,"</tr>");
auto d=p.at(0);
auto l=lines(d);
assert(l.size()>=10);
//cout<<l<<"\n";
auto regional_type=inside_td(l[2]);
auto event_name=rstrip(split(split(l[5],'>')[1],'<')[0]);
string event_url;
{
auto a=split_on(split_on(l[5],"href=\"")[1],"\">")[0];
event_url=split(url,'?')[0]+a;
}
auto venue=inside_td(l[7]);
auto city=interpret_html_escapes(inside_td(l[8]));
auto dates=inside_td(l[9]);
//cout<<event_url<<"\n";
//cout<<regional_type<<" "<<quoted(event_name)<<" ";
//cout<<city<<"\n";
//cout<<quoted(venue)<<" "<<dates<<"\n";
Event e{event_name,regional_type,venue,city,dates,event_url};
r|=e;
}
return r;
}
#include "json_spirit.h"
ostream& operator<<(ostream& o,json_spirit::Value const& v){
write(v,o,json_spirit::pretty_print);
return o;
}
ostream& operator<<(ostream& o,json_spirit::Object const& obj){
write(obj,o,json_spirit::pretty_print);
return o;
}
template<typename A>
ostream& operator<<(ostream& o,json_spirit::Pair_impl<A> const& p){
//write(p,o,json_spirit::pretty_print);
return o<<"("<<p.name_<<" "<<p.value_<<")";
}
struct BEvent{
//event info from the blue allaince.
string name;
Maybe<string> name_short;
string start_date,end_date;
bool official;
string key;
};
ostream& operator<<(ostream& o,BEvent const& b){
return o<<"BEvent("<<b.name<<" "<<b.key<<")";
}
//will return the keys
vector<BEvent> get_events(int year){
string url="http://www.thebluealliance.com/api/v1/events/list?year="+as_string(year);
json_spirit::Value value;
auto data=scrape_cached(url);
read(data,value);
//cout<<value<<"\n";
vector<BEvent> r;
for(auto a:value.get_array()){
BEvent b;
auto obj=a.get_obj();
//cout<<obj;
for(auto elem:obj){
//cout<<elem<<"\n";
auto n=elem.name_;
auto val=elem.value_;
auto v=[&](){ return val.get_str(); };
if(n=="name"){
b.name=v(); //r|=elem.value_.get_str();
}else if(n=="end_date"){
b.end_date=v();
}else if(n=="official"){
switch(val.type()){
case json_spirit::bool_type:
b.official=val.get_bool();
break;
case json_spirit::null_type:
b.official=0;
break;
default:
nyi
}
//b.official=v.get_bool();
}else if(n=="short_name"){
if(val.type()==json_spirit::null_type) continue;
b.name_short=v();
}else if(n=="key"){
b.key=v();
}else if(n=="start_date"){
b.start_date=v();
}else{
cout<<"Got "<<n<<"\n";
nyi
}
}
//cout<<"b!\n";
r|=b;
}
return r;
}
vector<string> get_event_keys(int year){
vector<string> r;
for(auto a:get_events(year)){
r|=a.key;
}
return r;
}
//data from the event details page of the blue alliance
struct BEvent_details{
Maybe<string> location;
string key;
string year;//could actually make this an int
string start_date,name;
vector<Team> teams;
bool official;
vector<string> matches;
string event_code;
string end_date;
Maybe<string> name_short;
Maybe<bool> facebook_eid; //fix the type of this.
};
ostream& operator<<(ostream& o,BEvent_details const& b){
return o<<"benent("<<b.name_short<<" "<<b.location<<")";
}
BEvent_details get_details(string const& event_code){
auto s=scrape_cached("http://www.thebluealliance.com/api/v1/event/details?event="+event_code);
//cout<<s<<"\n";
json_spirit::Value value;
read(s,value);
BEvent_details r;
//cout<<value<<"\n";
for(auto p:value.get_obj()){
//cout<<"p="<<p<<"\n";
auto n=p.name_;
auto v=p.value_;
auto s=[&](){ return v.get_str(); };
auto a=[&](){
vector<string> r;
for(auto elem:v.get_array()) r|=elem.get_str();
return r;
};
if(n=="event_code"){
r.event_code=s();
}else if(n=="start_date"){
r.start_date=s();
}else if(n=="end_date"){
r.end_date=s();
}else if(n=="short_name"){
if(v.type()!=json_spirit::null_type){
r.name_short=s();
}
}else if(n=="facebook_eid"){
//ignore for now
}else if(n=="matches"){
r.matches=a();
}else if(n=="official"){
switch(v.type()){
case json_spirit::bool_type:
r.official=v.get_bool();
break;
case json_spirit::null_type:
r.official=0;
break;
default:
nyi
}
}else if(n=="location"){
if(v.type()!=json_spirit::null_type){
r.location=s();
}
}else if(n=="teams"){
r.teams=mapf([](string s){ return Team(s); },a());
}else if(n=="key"){
r.key=s();
}else if(n=="year"){
r.year=v.get_int();
}else if(n=="name"){
r.name=s();
}else{
cout<<"got "<<n<<"\n";
nyi
}
}
return r;
}
vector<BEvent_details> all_event_details(int year){
return mapf(get_details,get_event_keys(year));
}
typedef string Competition_level;
//struct Match_id
struct Match_info{
int match_number;
int set_number;
Competition_level competition_level;
string key;
set<Team> teams; //warning: This is not accurate when just parsed in because doesn't work with "B" teams
struct Alliance{
int score;
vector<Team> teams;
};
map<string,Alliance> alliances;
string event;
};
ostream& operator<<(ostream& o,Match_info::Alliance const& a){
return o<<"Alliance("<<a.score<<" "<<a.teams<<")";
}
ostream& operator<<(ostream& o,Match_info const& m){
o<<"Match_info("<<m.key<<","<<m.match_number<<",";
o<<m.teams<<",";
o<<m.alliances;
return o<<")";
}
bool operator<(Match_info::Alliance const& a,Match_info::Alliance const& b){
#define X(name) if(a.name<b.name) return 1; if(b.name<a.name) return 0;
X(score);
X(teams);
#undef X
return 0;
}
template<typename T>
bool contains(set<T> const& s,T const& t){
return s.find(t)!=end(s);
}
template<typename T>
set<T> symmetric_difference(set<T> const& a,set<T> const& b){
set<T> r;
for(auto elem:a){
if(!contains(b,elem)) r|=elem;
}
for(auto elem:b){
if(!contains(a,elem)) r|=elem;
}
return r;
}
bool ok(Match_info const& m){
set<Team> listed;
for(auto a:values(m.alliances)){
listed|=to_set(a.teams);
}
auto a=m.teams;
if(a!=listed){
//cout<<"Diff: "<<symmetric_difference(a,listed)<<"\n";
//exit(1);
}
return listed==a;
}
vector<string> get_array(json_spirit::Value const& v){
vector<string> r;
for(auto elem:v.get_array()){
r|=elem.get_str();
}
return r;
}
//example input: 2010cmp_f1m1
Match_info match_info(string const& match_key){
//cout<<"going to ...\n";
auto s=scrape_cached("http://www.thebluealliance.com/api/v1/match/details?match="+match_key);
json_spirit::Value value;
read(s,value);
Match_info r;
//cout<<"starting\n";
for(auto p:value.get_obj()){
//cout<<"p1="<<p<<"\n";
auto n=p.name_;
auto v=p.value_;
auto s=[&v](){ return v.get_str(); };
if(n=="match_number"){
r.match_number=v.get_int();
}else if(n=="team_keys"){
for(auto elem:v.get_array()){
r.teams|=Team(elem.get_str());
}
}else if(n=="set_number"){
r.set_number=v.get_int();
}else if(n=="competition_level"){
r.competition_level=s();
}else if(n=="key"){
r.key=s();
}else if(n=="alliances"){
for(auto p:v.get_obj()){
auto color=p.name_;
Match_info::Alliance alliance;
for(auto p2:p.value_.get_obj()){
//cout<<"p2="<<p2<<"\n";
auto n2=p2.name_;
auto v2=p2.value_;
if(n2=="score"){
//woo non-normalized data type!
int x;
switch(v2.type()){
case json_spirit::int_type:
x=v2.get_int();
break;
case json_spirit::str_type:
//could try to use a conversion w/ error checking.
x=atoi(v2.get_str().c_str());
break;
default:
nyi
}
alliance.score=x;
}else if(n2=="teams"){
alliance.teams=mapf([](string s){ return Team(s); },get_array(v2));
}else{
cout<<p2<<"\n";
nyi
}
}
r.alliances[color]=alliance;
}
}else if(n=="event"){
//cout<<"a1\n";
r.event=s();
//cout<<"a2\n";
}else{
cout<<"got "<<n<<"\n";
nyi
}
//cout<<"d1\n";
}
//cout<<"done\n";
if(!ok(r)){
//cout<<"Not ok: "<<r<<"\n";
}
//assert(ok(r));
return r;
}
//This is rediculously inefficient, just in case you were wondering.
template<typename T>
set<T>& operator|=(set<T>& a,vector<T> const& b){ return a|=to_set(b); }
set<Team> teams(Match_info const& m){
set<Team> r;
for(auto a:m.alliances){
//set<string> t1=a.second.teams;
r|=a.second.teams;
}
return r;
}
set<Team> teams(vector<Match_info> const& v){
set<Team> r;
for(auto a:v) r|=teams(a);
return r;
}
//opr: columns=teams,rows=present on this alliance?, vector=allaince score
//ccwm: columns=teams,rows=(red?1:(blue?-1:0)), vector=red score-blue score
vector<Match> matches_with_team(Team team,int year){
vector<string> r;
nyi
return r;
}
bool substring(string const& haystack,string const& needle){
return strstr(haystack.c_str(),needle.c_str())!=NULL;
}
template<typename T>
multiset<T> to_multiset(vector<T> const& v){
multiset<T> r;
for(auto elem:v){
r.insert(elem);
}
return r;
}
template<typename T>
ostream& operator<<(ostream& o,multiset<T> const& m){
o<<"{ ";
for(auto e:m){
o<<e<<" ";
}
return o<<"}";
}
multiset<Regional_type> regional_types(int year){
return to_multiset(mapf(
[](BEvent_details b){
if(!b.official) return string("Unofficial");
if(substring(b.name,"District")){
return string("District");
}else if(substring(b.name,"Regional")){
return string("Regional");
}else if(substring(b.name,"Division")){
return string("Division");
}else if(substring(b.name,"Championship")){
//Michigan/Mid atlantic championships
return string("Championship");
}else{
return b.name;
}
},
all_event_details(year)
));
}
template<typename T>
vector<T> flatten(vector<vector<T>> const& v){
vector<T> r;
for(auto a:v) for(auto elem:a) r|=elem;
return r;
}
vector<string> match_keys(int year){
return flatten(mapf([](string s){ return get_details(s).matches; },get_event_keys(year)));
}
vector<Match_info> matches(int year){
return mapf(match_info,match_keys(year));
}
template<typename Func,typename T>
vector<T> filter(Func f,vector<T> const& in){
vector<T> r;
for(auto elem:in){
if(f(elem)) r|=elem;
}
return r;
}
vector<Match_info> matches(int year,Team team){
return filter([=](Match_info m)->bool{ return contains(m.teams,team); },matches(year));
}
vector<Match_info::Alliance> alliances(vector<Match_info> const& m){
return flatten(mapf([](Match_info m){ return values(m.alliances); },m));
}
vector<int> scores(vector<Match_info> const& m){
return mapf([](Match_info::Alliance m){ return m.score; },alliances(m));
}
vector<Match_info::Alliance> alliances(int year,Team team){
matches(year,team);
nyi
}
template<typename T>
T max(vector<T> const& v){
T r=*begin(v);
for(auto a:v) r=max(r,a);
return r;
}
template<typename T>
T min(vector<T> const& v){
T r=*begin(v);
for(auto a:v) r=min(r,a);
return r;
}
template<typename Func,typename T>
T with_max(Func f,vector<T> const& v){
return max(mapf([&](T t){ return make_pair(f(t),t); },v)).second;
}
template<typename Func,typename T>
T with_min(Func f,vector<T> const& v){
return min(mapf([&](T t){ return make_pair(f(t),t); },v)).second;
}
vector<Match_info::Alliance> winning_alliances(vector<Match_info> const& matches){
return mapf([](Match_info m)->Match_info::Alliance{
return with_max([](Match_info::Alliance m)->int{ return m.score; },values(m.alliances));
},matches);
}
vector<Match_info::Alliance> losing_alliances(vector<Match_info> const& m){
return mapf([](Match_info m)->Match_info::Alliance{
return with_min([](Match_info::Alliance m)->int{ return m.score; },values(m.alliances));
},m);
}
double sum(vector<int> const& v){
double t=0;
for(auto a:v) t+=a;
return t;
}
double mean(vector<int> const& v){
assert(v.size());
return sum(v)/v.size();
}
double mean_score(vector<Match_info::Alliance> const& v){
return mean(mapf([](Match_info::Alliance a){ return a.score; },v));
}
double median(vector<int> v){
sort(begin(v),end(v));
return v[v.size()/2];//could do the right thing when length is divisible by 2.
}
int mode(vector<int> const& v){
map<int,Default<int,0>> m;
for(auto a:v) m[a]++;
vector<pair<int,int>> vout;
for(auto p:m){
vout|=make_pair((int)p.second,p.first);
}
sort(begin(vout),end(vout));
return vout[vout.size()-1].second;
}
#define RM_REF(X) typename remove_reference<X>::type
#define RM_CONST(X) typename remove_const<X>::type
#define ELEMENT(X) RM_CONST(RM_REF(decltype(*begin(X))))
template<typename Collection>
auto enumerate(Collection const& v)->vector<pair<unsigned,ELEMENT(v)>>{
vector<pair<unsigned,ELEMENT(v)>> r;
unsigned i=0;
for(auto e:v){
r.push_back(make_pair(i,e));
i++;
}
return r;
}
template<typename Collection>
auto number(Collection const& v)->map<ELEMENT(v),unsigned>{
enumerate(v);
map<ELEMENT(v),unsigned> r;
for(auto p:enumerate(v)){
r[p.second]=p.first;
}
return r;
}
//pulled from random webpage
template<class T>
boost::numeric::ublas::matrix<T> InvertMatrix(const boost::numeric::ublas::matrix<T>& input)
{
using namespace boost::numeric::ublas;
typedef permutation_matrix<std::size_t> pmatrix;
// create a working copy of the input
matrix<T> inverse;
matrix<T> A(input);
// create a permutation matrix for the LU-factorization
pmatrix pm(A.size1());
// perform LU-factorization
int res = lu_factorize(A, pm);
if (res != 0){
cout<<"res="<<res<<"\n";
nyi //return false;
}
// create identity matrix of "inverse"
inverse.assign(identity_matrix<T> (A.size1()));
// backsubstitute to get the inverse
lu_substitute(A, pm, inverse);
return inverse;
}
void t(){
using namespace boost::numeric::ublas;
int year=2013;
auto match_list=take(2,matches(year));
auto alliance_list=alliances(match_list);
auto team_list=teams(match_list);
auto nums=number(team_list);
matrix<double> m(team_list.size(),alliance_list.size()); //asume we've just got two sides.
for(unsigned i=0;i<team_list.size();i++){
for(unsigned j=0;j<alliance_list.size();j++){
m(i,j)=0;
}
}
boost::numeric::ublas::vector<double> v(alliance_list.size());
//need to declare the vector here...
cout<<"nums="<<nums<<"\n";
cout<<"match_list="<<match_list<<"\n";
cout<<"alliance_list="<<alliance_list<<"\n";
for(auto p:enumerate(alliance_list)){
cout<<p<<"\n";
for(auto team:p.second.teams){
cout<<"t="<<team<<" "<<nums[team]<<"\n";
m(nums[team],p.first)=1;
}
v[p.first]=p.second.score;
}
cout<<m<<"\n";
cout<<InvertMatrix(m)<<"\n";
cout<<trans(m)<<"\n";
auto p=prod(trans(m),m);
cout<<prod(m,trans(m))<<"\n";
cout<<"vec="<<v<<"\n";
cout<<prod(p,v)<<"\n";
//inverse( trans(x)*x ) * trans(x) * y = X+ * y = Beta_hat
}
struct BTeam_basic{
string key_name;
int team_number;
string name,nickname,website;
vector<string> event_keys;
string location;
string locality,region,country_name;
};
ostream& operator<<(ostream& o,BTeam_basic const& b){
o<<"BTeam_basic( ";
#define X(name) o<<""#name<<"="<<b.name<<" ";
X(key_name)
X(team_number)
X(name)
X(nickname)
X(website)
X(event_keys)
X(location)
X(locality)
X(region)
X(country_name)
#undef X
return o<<")";
}
BTeam_basic team_basic(Team team){
stringstream ss;
ss<<"http://www.thebluealliance.com/api/v1/teams/show?teams="<<team;
auto data=scrape_cached(ss.str());
json_spirit::Value value;
read(data,value);
//cout<<value<<"\n";
for(auto a:value.get_array()){
BTeam_basic r;
auto obj=a.get_obj();
for(auto elem:obj){
auto n=elem.name_;
auto v=elem.value_;
auto s=[&](){ return v.get_str(); };
if(n=="website"){
r.website=s();
}else if(n=="name"){
r.name=s();
}else if(n=="locality"){
r.locality=s();
}else if(n=="region"){
r.region=s();
}else if(n=="team_number"){
r.team_number=v.get_int();
}else if(n=="location"){
r.location=s();
}else if(n=="key"){
r.key_name=s();
}else if(n=="country_name"){
r.country_name=s();
}else if(n=="nickname"){
r.nickname=s();
}else if(n=="events"){
//note that this contains only the events for the current year.
for(auto elem:v.get_array()){
r.event_keys|=elem.get_str();
}
}else{
cout<<"n="<<n<<"\n";
nyi
}
}
return r;
}
nyi
}
struct BTeam_details{
string key_name,team_number,nickname,website;
vector<string> event_keys;
string location,locality,country,region;
};
ostream& operator<<(ostream& o,BTeam_details const& b){
o<<"BTeam_details( ";
#define X(name) o<<""#name<<"="<<b.name<<" ";
X(key_name)
X(team_number)
X(nickname)
X(website)
X(event_keys)
X(location)
X(locality)
X(country)
X(region)
#undef X
return o<<")";
}
BTeam_details team_details(Team team)nyi
//if wanted to be fancier, would actually make this be a structure and then have a way to turn it into a string.
string tag(string name,string body){
stringstream ss;
ss<<"<"<<name<<">"<<body<<"</"<<name<<">";
return ss.str();
}
template<typename T>
struct Tag{
string name;
T body;//this just needs to be something that is convertible to a string.
};
template<typename T>
ostream& operator<<(ostream& o,Tag<T>)nyi
template<typename T>
Tag<T> tag2(string a,T b){ return Tag<T>{a,b}; }
template<typename T>
string operator+(string,Tag<T>)nyi
template<typename T>
string operator+(Tag<T>,string)nyi
string team_page(Team team){
auto title=string("Team ")+as_string(team);
return as_string(tag("html",
tag("head",
tag("title",title)
)+
tag("body",
tag("h1",title)+
"The body"+as_string(team_basic(team))
)
));
}
//takes about 5 minutes to get all the details on the events, then extra time beyond that to get the matches.
int main(){
cout<<team_basic(Team("frc1425"))<<"\n";
//t();
//return 0;
int year=2013;
auto m=matches(year);
//cout<<alliances(m).size()<<"\n";
cout<<"mean="<<mean(scores(m))<<"\n";
cout<<"median="<<median(scores(m))<<"\n";
cout<<"mode="<<mode(scores(m))<<"\n";
cout<<"Mean given won="<<mean_score(winning_alliances(m))<<"\n";
cout<<"Mean given lost="<<mean_score(losing_alliances(m))<<"\n";
return 0;
cout<<regional_types(2012)<<"\n";
cout<<regional_types(2013)<<"\n";
//cout<<matches(2013)<<"\n";
for(auto elem:matches(2012,Team("frc1425"))){
cout<<elem<<"\n";
}
// match_info();
return 0;
auto keys=get_event_keys(2013);
cout<<"got keys\n";
for(auto key:keys){
auto det=get_details(key);
cout<<det<<"\n";
for(auto match:det.matches){