-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbtmanip.cpp
3849 lines (3276 loc) · 116 KB
/
btmanip.cpp
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
/*
-------------------------------------------------------------------
Copyright (C) 2015-2025, Andrew W. Steiner
This file is part of btmanip.
btmanip is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
btmanip is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with btmanip. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------
*/
/** \file btmanip.cpp
\brief File defining \ref btmanip_class
OSTI URL is
https://www.osti.gov/search/identifier:osti_id
*/
#include "bib_file.h"
#include "hdf_bibtex.h"
// For time()
#include <ctime>
#include <string>
#include <vector>
#include <boost/algorithm/string/replace.hpp>
#include <o2scl/cli_readline.h>
#include <o2scl/string_conv.h>
#include "json.hpp"
using namespace std;
using namespace o2scl;
using namespace btmanip;
namespace btmanip {
/** \brief Main class for the command-line BibTeX manipulator
*/
class btmanip_class {
protected:
/// Command-line interface
o2scl::cli_readline *cl;
/// \name Parameters for 'set' command
//@{
o2scl::cli::parameter_int p_verbose;
o2scl::cli::parameter_bool p_recase_tag;
o2scl::cli::parameter_bool p_reformat_journal;
o2scl::cli::parameter_bool p_trans_latex_html;
o2scl::cli::parameter_bool p_normalize_tags;
o2scl::cli::parameter_bool p_lowercase_fields;
o2scl::cli::parameter_bool p_check_required;
o2scl::cli::parameter_bool p_natbib_jours;
o2scl::cli::parameter_bool p_remove_vol_letters;
o2scl::cli::parameter_bool p_autoformat_urls;
o2scl::cli::parameter_bool p_add_empty_titles;
o2scl::cli::parameter_bool p_remove_author_tildes;
/// A file of BibTeX entries
bib_file bf;
/// If true, a journal list has been read
bool jlist_read;
int get_screen_width() {
int nrow, ncol=80;
// AWS 5/20/20: I think this causes problems when run
// in a script
//#ifdef O2SCL_NCURSES
// Use curses
//get_screen_size(nrow,ncol);
//#endif
// If not, attempt to obtain the result from the environment
char *ncstring=getenv("COLUMNS");
if (ncstring) {
int nc2;
int sret=o2scl::stoi_nothrow(ncstring,nc2);
if (sret==0 && nc2>0) {
ncol=nc2;
} else {
cerr << "Failed to interpret COLUMNS value " << ncstring
<< " as a positive number of columns." << endl;
}
}
return ncol;
}
/** \brief Read journal list file
<file>
Read a list of journal names and synonyms from <file>.
*/
virtual int read_jlist(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()<2) {
cerr << "Command 'read-jlist' needs filename." << endl;
return 1;
}
int ret=bf.read_journals(sv[1]);
if (ret!=o2scl::exc_efilenotfound) {
jlist_read=true;
} else {
O2SCL_ERR((((string)"Failed to read journal list file named '")+
sv[1]+"'.").c_str(),o2scl::exc_efilenotfound);
}
return 0;
}
/** \brief Set the value of one field in one entry
<entry> <field> <value> or if one entry, <field> <value>
Set the value of <field> in <entry> to <value>, or if there is
only one entry in the current list and only two arguments are
given, set <field> to <value>.
*/
virtual int set_field(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()==4) {
bf.set_field_value(sv[1],sv[2],sv[3]);
} else if (sv.size()==3) {
if (bf.entries.size()==1) {
bibtex_entry &bt=static_cast<bibtex_entry &>(bf.entries[0]);
bf.set_field_value(bt,sv[1],sv[2]);
} else {
cerr << "More than one entry, thus 'set-field' requires three "
<< "arguments." << endl;
return 1;
}
} else {
cerr << "Not enough arguments to 'set-field'." << endl;
}
return 0;
}
/** \brief Search current list for field and pattern pairs.
["and"] ["or"] <field 1> <pattern 1> [field 2] [pattern 2]
Search the current list for entries which have fields which
match a specified pattern. Combine multiple criteria with
"or" or "and" if specified. If at least one entry is
found, then the current list is replaced with the search
results. Replace one of the field arguments with "key" to
search by key name.
*/
virtual int search(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()==3) {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
bf.search_or(sv);
} else if (sv[1]=="or") {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
it=sv.begin();
sv.erase(it);
bf.search_or(sv);
} else if (sv[1]=="and") {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
it=sv.begin();
sv.erase(it);
bf.search_and(sv);
} else {
cerr << "Failed in search." << endl;
return 1;
}
return 0;
}
/** \brief Remove fields which match field and pattern pairs.
["and"] ["or"] <field 1> "<pattern 1> [field 2] [pattern 2] ...",
Remove entries from the current list of which have fields
which match a specified pattern. Combine multiple criteria
with "or" or "and" if specified.
*/
virtual int remove(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()==3) {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
bf.remove_or(sv);
} else if (sv[1]=="or") {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
it=sv.begin();
sv.erase(it);
bf.remove_or(sv);
} else if (sv[1]=="and") {
std::vector<std::string>::iterator it=sv.begin();
sv.erase(it);
it=sv.begin();
sv.erase(it);
O2SCL_ERR("Remove 'and' unimplemented.",exc_eunimpl);
//bf.remove_and(sv);
} else {
cerr << "Failed in remove." << endl;
return 1;
}
return 0;
}
/** \brief Subtract a .bib file from the current entries
<file>
This takes all entries in <file> and looks for them in the
list of current entries. If any duplicates are found, they are
removed from the current list.
*/
virtual int sub(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()<2) {
cerr << "Not enough arguments to 'sub'" << endl;
return 1;
}
bib_file bf2;
bf2.parse_bib(sv[1]);
bool found_duplicate=false;
for(size_t i=0;i<bf.entries.size();) {
bool found=false;
for(size_t j=0;j<bf2.entries.size();j++) {
std::string key1=*(bf.entries[i].key);
std::string key2=*(bf2.entries[j].key);
if (key1==key2 && bf.entries[i].tag==bf2.entries[j].tag) {
cout << "Duplicate keys and duplicate tags: "
<< bf.entries[i].tag << " " << key1 << endl;
bf.entries.erase(bf.entries.begin()+i);
j=bf2.entries.size();
found_duplicate=true;
found=true;
}
}
if (!found) i++;
}
// Remake 'sort' object if necessary
if (found_duplicate) {
bf.sort.clear();
for(size_t i=0;i<bf.entries.size();i++) {
bibtex::BibTeXEntry &bt=bf.entries[i];
if (bf.sort.find(*bt.key)==bf.sort.end()) {
bf.sort.insert(make_pair(*bt.key,i));
}
}
}
return 0;
}
/** \brief Look up journal name.
<name>
If a journal name list is loaded, look up <name> in the list
and output all the synonyms. Journal name matching ignores
spacing, case, and punctuation.
*/
virtual int journal(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()<2) {
cerr << "Need journal name for 'journal' command." << endl;
return 1;
}
std::string sout;
std::vector<std::string> list;
int ret=bf.find_abbrevs(sv[1],list);
if (ret==1) {
cerr << "Couldn't find journal " << sv[1] << " ." << endl;
return 2;
}
for(size_t k=0;k<list.size();k++) {
cout << k << ". " << list[k] << endl;
}
return 0;
}
/** \brief Output an HDF5 file
<file>
Detailed desc.
*/
virtual int hdf5(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()<2) {
cerr << "Command 'hdf5' needs filename." << endl;
return 1;
}
o2scl_hdf::hdf_file hf;
hf.compr_type=1;
hf.open_or_create(sv[1]);
hdf_output(hf,bf.entries,"btmanip");
hf.close();
return 0;
}
/** \brief Clear the current bibliography
*/
virtual int clear(std::vector<std::string> &sv, bool itive_com) {
bf.entries.clear();
bf.sort.clear();
return 0;
}
/** \brief Parse an HDF5 file
<file>
Parse a bibliography stored in an HDF5 file.
*/
virtual int parse_hdf5(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()<2) {
cerr << "Command 'parse-hdf5' needs filename." << endl;
return 1;
}
bf.entries.clear();
bf.sort.clear();
o2scl_hdf::hdf_file hf;
hf.open(sv[1]);
std::string name;
hdf_input(hf,bf.entries,name);
hf.close();
bf.refresh_sort();
return 0;
}
/** \brief Get information from adsabs
*/
virtual int ads_get(std::vector<std::string> &sv, bool itive_com) {
int verbose=1;
// Get API token
std::string token;
char *token_ptr=getenv("ADSABS_TOKEN");
if (token_ptr) {
token=token_ptr;
} else {
cerr << "Token not found in \"ads-get\"." << endl;
return 1;
}
std::string prefix="curl -X GET -H 'Authorization: Bearer:"+token+
"' ";
std::string base_url="https://api.adsabs.harvard.edu/v1/";
for(size_t i=0;i<bf.entries.size();i++) {
bibtex_entry &bt=static_cast<bibtex_entry &>(bf.entries[i]);
if (bf.is_field_present(bt,"doi")) {
string doi=bf.get_field(bt,"doi");
while (doi.find("/")!=std::string::npos) {
doi.replace(doi.find("/"),1,"%2F");
}
string cmd=prefix+"\""+base_url+"search/query?q=doi:"+
doi+"&fl=bibcode\"";
cout << "cmd: " << cmd << endl;
string result;
int ret=pipe_cmd_string(cmd,result,false,1000);
cout << result.size() << " " << result << endl;
auto j=nlohmann::json::parse(result);
auto j_bibcode=j["response"]["docs"][0]["bibcode"];
string bibcode=j_bibcode.get<std::string>();
cout << bibcode << endl;
cmd=prefix+"-H \"Content-Type: application/json\" "+
base_url+"export/bibtex -X POST -d '{\"bibcode\":[\""+
bibcode+"\"]}'";
cout << "cmd: " << cmd << endl;
ret=pipe_cmd_string(cmd,result,false,100000);
cout << result.size() << " " << result << endl;
auto j2=nlohmann::json::parse(result);
cout << j2["export"] << endl;
string bib=j2["export"].get<std::string>();
cout << bib << endl;
exit(-1);
}
}
return 0;
}
/** \brief Get information from inspirehep.net
(no arguments)
This uses the old inspirehep.net API and probably doesn't
work any more.
*/
virtual int inspire_get(std::vector<std::string> &sv, bool itive_com) {
for(size_t i=0;i<bf.entries.size();i++) {
bibtex_entry &bt=static_cast<bibtex_entry &>(bf.entries[i]);
if (bf.is_field_present(bt,"doi")) {
string doi=bf.get_field(bt,"doi");
if (bf.verbose>1) {
cout << "doi: " << doi << endl;
}
string cmd=((string)"curl -X GET \"http://old.inspire")+
"hep.net/search?action_search=Search&rg=1&of=recjson&"+
"ln=en&p=find+doi+"+doi+"&jrec=0\"";
string result;
if (bf.verbose>1) {
cout << cmd << endl;
}
static const size_t nbuf=200000;
int ret=pipe_cmd_string(cmd,result,false,nbuf);
if (bf.verbose>1) {
cout << "Result length: " << result.length() << endl;
}
bool dl_failed=false;
if (result.length()>=nbuf-1) {
cerr << "Inspire result was longer than buffer size." << endl;
dl_failed=true;
}
if (dl_failed==false) {
auto j=nlohmann::json::parse(result);
if (bf.verbose>1) {
cout << "Number of inspirehep.net results: "
<< j.size() << endl;
}
if (j.size()!=1) {
cerr << "Inspire search led to more than one result." << endl;
} else {
bibtex_entry bt_new;
//bibtex_entry &bt=static_cast<bibtex_entry &>(bt_new);
bt_new.key=bt.key;
bt_new.tag="Article";
// References cited
//cout << "reference: " << j[0]["reference"] << endl;
auto jauthors=j[0]["authors"];
string auth_list;
bool auth_success=true;
for(size_t k=0;k<jauthors.size();k++) {
auto auth=jauthors[k];
bool found=false;
for (nlohmann::json::iterator it=auth.begin();
it != auth.end();++it) {
if (it.key()==((string)"full_name")) {
found=true;
string str=it.value().get<std::string>();
if (auth_list.length()==0) {
auth_list+=str;
} else {
auth_list+=((string)" and ")+str;
}
}
}
if (found==false) {
cerr << "Full name for author of index "
<< k << " not found." << endl;
auth_success=false;
}
}
if (bf.verbose>1) {
cout << "authors: " << auth_list << endl;
}
bf.set_field_value(bt_new,"author",auth_list);
if (auth_success==true) {
auto j_title=j[0]["title"].begin().value();
std::string title=j_title.get<std::string>();
if (bf.verbose>1) {
cout << "title: " << bf.get_field(bt,"title") << endl;
}
bf.set_field_value(bt_new,"title",title);
auto j_doi2=j[0]["doi"].begin().value();
std::string doi2=j_doi2.get<std::string>();
if (bf.verbose>1) {
cout << "doi2: " << doi2 << endl;
}
bf.set_field_value(bt_new,"doi",doi2);
auto j_eprint=j[0]["primary_report_number"].begin();
std::string eprint=j_eprint.value().get<std::string>();
if (eprint.substr(0,6)==((string)"arXiv:")) {
eprint=eprint.substr(6,eprint.length()-6);
}
if (bf.verbose>1) {
cout << "eprint: " << eprint << endl;
}
bf.set_field_value(bt_new,"eprint",eprint);
auto j_pages=j[0]["publication_info"]["pagination"].begin();
std::string pages=j_pages.value().get<std::string>();
if (bf.verbose>1) {
cout << "pages: " << pages << endl;
}
bf.set_field_value(bt_new,"pages",pages);
auto j_journal=j[0]["publication_info"]["title"].begin();
std::string journal=j_journal.value().get<std::string>();
if (bf.verbose>1) {
cout << "journal: " << journal << endl;
}
bf.set_field_value(bt_new,"journal",journal);
auto j_volume=j[0]["publication_info"]["volume"].begin();
std::string volume=j_volume.value().get<std::string>();
if (bf.verbose>1) {
cout << "volume: " << volume << endl;
}
bf.set_field_value(bt_new,"volume",volume);
auto j_year=j[0]["publication_info"]["year"].begin();
std::string year=j_year.value().get<std::string>();
if (bf.verbose>1) {
cout << "year: " << year << endl;
}
bf.set_field_value(bt_new,"year",year);
// Reformat journal and volume from inspirehep.net
bf.entry_remove_vol_letters(bt_new);
journal=bf.get_field(bt_new,"journal");
// Normalize journal abbreviation
std::string abbrev;
if (bf.find_abbrev(journal,abbrev)==0) {
journal=abbrev;
bf.set_field_value(bt_new,"journal",journal);
}
bf.bib_output_twoup(cout,bt,bt_new,"Original",
"Inspirehep record");
cout << "Keep original ('<' or ','), "
<< "use new inspirehep entry ('>' or '.'), "
<< "cherry pick ('c'), or quit ('q'): " << endl;
char ch;
cin >> ch;
}
}
}
} else {
cout << "No DOI present for key: " << *bt.key << endl;
}
}
return 0;
}
/** \brief Get information from inspirehep.net
<inspire id>
*/
virtual int inspire_refersto(std::vector<std::string> &sv,
bool itive_com) {
std::string recid=sv[1];
int pages=1;
int total=1;
bool first_page_only=true;
vector<string> id_list;
for(int ip=0;ip<pages;ip++) {
// Download from the inspirehep.net API and place the JSON result
// in /tmp/btmanip.tmp
std::string cmd=((string)"curl -X GET \"https://inspirehep.net")+
"/api/literature?format=json&sort=mostrecent&page="+
o2scl::itos(ip+1)+"&q=refersto:recid:"+sv[1]+
"\" > /tmp/btmanip.tmp";
if (bf.verbose>1) {
cout << "cmd: " << cmd << endl;
}
int ret=system(cmd.c_str());
// Load the file into a string so we can parse the JSON
cmd="cat /tmp/btmanip.tmp";
std::string result;
// 20 MB
int max_size=20*1e3*1e3;
pipe_cmd_string(cmd,result,false,max_size);
// Parse the JSON
auto j=nlohmann::json::parse(result);
if (ip==0) {
// Determine the total number of citations
auto j_total=j["hits"]["total"];
total=j_total.get<int>();
if (total==0) {
cout << "No citations yet." << endl;
} else {
cout << "Total citations: " << total << endl;
pages=total/10+1;
cout << "Pages of results: " << pages << endl;
}
}
int n_arr=10;
if (n_arr>total) n_arr=total;
if (ip==pages-1) {
n_arr=total-ip*10;
}
cout << "Number of entries in this page: " << n_arr << endl;
// Get the record ID of each citation
for(int i=0;i<n_arr;i++) {
auto j_id=j["hits"]["hits"][i]["id"];
std::string id=j_id.get<std::string>();
cout << "id: " << id << endl;
id_list.push_back(id);
}
char ch;
cin >> ch;
if (first_page_only) {
ip=pages;
}
}
//bf.entries.clear();
//bf.sort.clear();
//bf.parse_bib("/tmp/btmanip.tmp");
return 0;
}
/** \brief Get information from inspirehep.net
<cited id list> <recent html> [header] [footer]
This uses the new API and might work?
*/
virtual int inspire_recent_cites(std::vector<std::string> &sv,
bool itive_com) {
bool first_page_only=true;
vector<string> id_list_new;
vector<string> id_list_old;
string date_new, date_old;
string stmp;
ifstream fin;
fin.open(sv[1]);
getline(fin,date_old);
while (fin >> stmp) {
id_list_old.push_back(stmp);
}
fin.close();
cout << "Found " << id_list_old.size()
<< " entries in cache." << endl;
// Compute new date
{
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(buffer,80,"%c",timeinfo);
date_new=buffer;
}
// Begin creating 'recent.html'
ofstream frecent(sv[2]);
// File header
if (sv.size()>=4) {
ifstream fheader(sv[3]);
std::string line;
while (getline(fheader,line)) {
frecent << line << endl;
}
}
cout << "date_old: " << date_old << endl;
cout << "date_new: " << date_new << endl;
// --
size_t margin=10;
for(size_t im=0;im<margin;im++) frecent << ' ';
frecent << "<p>From inspirehep.net between "
<< date_old << " and " << date_new << ". </p>" << endl;
frecent << endl;
for(size_t ie=0;ie<bf.entries.size();ie++) {
bool title_written=false;
cout << ie << " of " << bf.entries.size()
<< " publications." << endl;
bibtex_entry &bt=static_cast<bibtex_entry &>(bf.entries[ie]);
cout << "Article titled " << endl;
vector<string> sv2;
o2scl::rewrap(bf.get_field(bt,"title"),sv2);
for(size_t ik=0;ik<sv2.size();ik++) {
cout << " " << sv2[ik];
if (ik==sv2.size()-1) cout << ":";
cout << endl;
}
int pages=1;
int total=1;
if (bf.is_field_present(bt,"inspireid")) {
string inspire_id=bf.get_field(bt,"inspireid");
for(int ip=0;ip<pages;ip++) {
// Download from the inspirehep.net API and place the JSON result
// in /tmp/btmanip.tmp
std::string cmd=((string)"curl -X GET \"https://inspirehep.net")+
"/api/literature?format=json&sort=mostrecent&page="+
o2scl::itos(ip+1)+"&q=refersto:recid:"+inspire_id+
"\" > /tmp/btmanip.tmp";
if (bf.verbose>1) {
cout << "cmd: " << cmd << endl;
}
int ret=system(cmd.c_str());
ifstream fin2;
fin2.open("/tmp/btmanip.tmp");
std::string result;
getline(fin2,result);
fin2.close();
// Parse the JSON
auto j=nlohmann::json::parse(result);
if (ip==0) {
// Determine the total number of citations
auto j_total=j["hits"]["total"];
total=j_total.get<int>();
if (total==0) {
cout << "No citations yet." << endl;
} else {
cout << "Total citations: " << total << endl;
pages=total/10+1;
cout << "Pages of results: " << pages << endl;
}
}
int n_arr=10;
if (n_arr>total) n_arr=total;
if (ip==pages-1) {
n_arr=total-ip*10;
}
cout << "Number of entries in this page: " << n_arr << endl;
// Get the record ID of each citation
for(int i=0;i<n_arr;i++) {
auto j_id=j["hits"]["hits"][i]["id"];
std::string id=j_id.get<std::string>();
cout << "id: " << id << endl;
size_t ix_found;
if (vector_search<vector<string>,string>(id_list_old.size(),
id_list_old,id,
ix_found)==false) {
if (title_written==false) {
for(size_t im=0;im<margin;im++) frecent << ' ';
frecent << "<p>New citations for " << endl;
for(size_t ik=0;ik<sv.size();ik++) {
if (ik==0) {
frecent << " \"" << sv[ik];
} else {
frecent << " " << sv[ik];
}
if (ik==sv.size()-1) frecent << "\":";
frecent << endl;
}
title_written=true;
}
//cout << "New citation recid: " << id << endl;
id_list_new.push_back(id);
auto j_entry=j["hits"]["hits"][i];
std::string title="<no title>";
auto j_title_list=j_entry["metadata"]["titles"];
if (j_title_list.size()>0) {
auto j_title=j_title_list[0]["title"];
title=j_title.get<std::string>();
}
std::string eprint="<none>";
auto j_eprint_list=j_entry["metadata"]["arxiv_eprints"];
if (j_eprint_list.size()>0) {
auto j_eprint=j_eprint_list[0]["value"];
eprint=j_eprint.get<std::string>();
}
if (title[0]!='\"' && title[title.length()-1]!='\"') {
title=((string)"\"")+title+"\"";
}
cout << " Cited by article " << eprint << " titled ";
cout << title << " ." << endl;
for(size_t im=0;im<margin;im++) frecent << ' ';
frecent << " <li>arXiv.org/" << eprint << ": ";
frecent << "<a href=\"https://arxiv.org/abs/" << eprint
<< "\">" << title << "</a>.</li>" << endl;
}
}
if (first_page_only) {
ip=pages;
}
}
}
if (title_written) {
frecent << "</ul></p>" << endl;
}
cout << "Sleeping for a minute." << endl;
sleep(60);
cout << "Done sleeping." << endl;
cout << endl;
}
if (sv.size()>=5) {
// File footer
ifstream ffooter(sv[4]);
std::string line;
while (getline(ffooter,line)) {
frecent << line << endl;
}
}
// Add entries in id_list_new to id_list_old, but check
// for duplicates
for(size_t i=0;i<id_list_new.size();i++) {
size_t ix_found;
if (vector_search<vector<string>,string>(id_list_old.size(),
id_list_old,id_list_new[i],
ix_found)==false) {
id_list_old.push_back(id_list_new[i]);
}
}
id_list_new=id_list_old;
// Update cache
ofstream foutcache(sv[1]);
foutcache << date_new << endl;
for(size_t j=0;j<id_list_new.size();j++) {
foutcache << id_list_new[j] << endl;
}
foutcache.close();
return 0;
}
/** \brief Sort current BibTeX entries by key.
(No arguments.)
Sort current BibTeX entries by key.
*/
virtual int sort(std::vector<std::string> &sv, bool itive_com) {
bf.sort_bib();
return 0;
}
/** \brief Sort current BibTeX entries by date.
["descending"]
Sort current BibTeX entries by date.
*/
virtual int sort_by_date(std::vector<std::string> &sv, bool itive_com) {
if (sv.size()>=2 && sv[1]==((string)"descending")) {
bf.sort_by_date(true);
} else {
bf.sort_by_date();
}
return 0;
}
/** \brief Find duplicates between two .bib files
[file 1] [file 2]
If no filenames are specified, then look for duplicates among
the current entries. If one filename is specified, then
compare the current entries to the specified file to find
duplicate entries. Finally, if two filenames are specified,
find duplicates between the two files.
*/
virtual int dup(std::vector<std::string> &sv, bool itive_com) {
bool found=false;
std::cout << "size: " << bf.journals.size() << endl;
if (sv.size()==2) {
if (bf.entries.size()==0) {
cerr << "No BibTeX entries to compare to." << endl;
return 1;
}
bib_file bf2;
bf2.parse_bib(sv[1]);
for(size_t i=0;i<bf.entries.size();i++) {
for(size_t j=0;j<bf2.entries.size();j++) {
std::string key1=*(bf.entries[i].key);
std::string key2=*(bf2.entries[j].key);
if (key1==key2 && bf.entries[i].tag==bf2.entries[j].tag) {
cout << "Duplicate: " << bf.entries[i].tag << " "
<< key1 << endl;
found=true;
}
}
}
} else if (sv.size()>=3) {
bib_file bf2;
bf2.parse_bib(sv[1]);
bib_file bf3;
bf3.parse_bib(sv[2]);
for(size_t i=0;i<bf2.entries.size();i++) {
for(size_t j=0;j<bf3.entries.size();j++) {
std::string key1=*(bf2.entries[i].key);
std::string key2=*(bf3.entries[j].key);
if (key1==key2 && bf2.entries[i].tag==bf3.entries[j].tag) {
cout << "Duplicate: " << bf2.entries[i].tag << " "
<< key1 << endl;
found=true;
}
}
}
} else {
if (bf.verbose>0) {
cout << "Looking for duplicates among current BibTeX entries."
<< endl;
}
bool restart=true;