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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
|
package cgeo.geocaching.connector.gc;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.Image;
import cgeo.geocaching.LogEntry;
import cgeo.geocaching.PocketQueryList;
import cgeo.geocaching.R;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.Trackable;
import cgeo.geocaching.TrackableLog;
import cgeo.geocaching.Waypoint;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.LogTypeTrackable;
import cgeo.geocaching.enumerations.StatusCode;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.files.LocParser;
import cgeo.geocaching.gcvote.GCVote;
import cgeo.geocaching.gcvote.GCVoteRating;
import cgeo.geocaching.loaders.RecaptchaReceiver;
import cgeo.geocaching.location.DistanceParser;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.DirectionImage;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.HtmlUtils;
import cgeo.geocaching.utils.JsonUtils;
import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.MatcherWrapper;
import cgeo.geocaching.utils.RxUtils;
import cgeo.geocaching.utils.SynchronizedDateFormat;
import cgeo.geocaching.utils.TextUtils;
import ch.boye.httpclientandroidlib.HttpResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func2;
import rx.schedulers.Schedulers;
import rx.util.async.Async;
import android.net.Uri;
import android.text.Html;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
public abstract class GCParser {
@NonNull
private final static SynchronizedDateFormat DATE_TB_IN_1 = new SynchronizedDateFormat("EEEEE, dd MMMMM yyyy", Locale.ENGLISH); // Saturday, 28 March 2009
@NonNull
private final static SynchronizedDateFormat DATE_TB_IN_2 = new SynchronizedDateFormat("EEEEE, MMMMM dd, yyyy", Locale.ENGLISH); // Saturday, March 28, 2009
@NonNull
private final static ImmutablePair<StatusCode, Geocache> UNKNOWN_PARSE_ERROR = ImmutablePair.of(StatusCode.UNKNOWN_ERROR, null);
@Nullable
private static SearchResult parseSearch(final String url, final String pageContent, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (StringUtils.isBlank(pageContent)) {
Log.e("GCParser.parseSearch: No page given");
return null;
}
final List<String> cids = new ArrayList<>();
String page = pageContent;
final SearchResult searchResult = new SearchResult();
searchResult.setUrl(url);
searchResult.viewstates = GCLogin.getViewstates(page);
// recaptcha
if (showCaptcha) {
final String recaptchaJsParam = TextUtils.getMatch(page, GCConstants.PATTERN_SEARCH_RECAPTCHA, false, null);
if (recaptchaJsParam != null) {
recaptchaReceiver.setKey(recaptchaJsParam.trim());
recaptchaReceiver.fetchChallenge();
}
if (recaptchaReceiver != null && StringUtils.isNotBlank(recaptchaReceiver.getChallenge())) {
recaptchaReceiver.notifyNeed();
}
}
if (!page.contains("SearchResultsTable")) {
// there are no results. aborting here avoids a wrong error log in the next parsing step
return searchResult;
}
int startPos = page.indexOf("<div id=\"ctl00_ContentBody_ResultsPanel\"");
if (startPos == -1) {
Log.e("GCParser.parseSearch: ID \"ctl00_ContentBody_dlResults\" not found on page");
return null;
}
page = page.substring(startPos); // cut on <table
startPos = page.indexOf('>');
final int endPos = page.indexOf("ctl00_ContentBody_UnitTxt");
if (startPos == -1 || endPos == -1) {
Log.e("GCParser.parseSearch: ID \"ctl00_ContentBody_UnitTxt\" not found on page");
return null;
}
page = page.substring(startPos + 1, endPos - startPos + 1); // cut between <table> and </table>
final String[] rows = StringUtils.splitByWholeSeparator(page, "<tr class=");
final int rowsCount = rows.length;
int excludedCaches = 0;
final ArrayList<Geocache> caches = new ArrayList<>();
for (int z = 1; z < rowsCount; z++) {
final Geocache cache = new Geocache();
final String row = rows[z];
// check for cache type presence
if (!row.contains("images/wpttypes")) {
continue;
}
try {
final MatcherWrapper matcherGuidAndDisabled = new MatcherWrapper(GCConstants.PATTERN_SEARCH_GUIDANDDISABLED, row);
while (matcherGuidAndDisabled.find()) {
if (matcherGuidAndDisabled.groupCount() > 0) {
if (matcherGuidAndDisabled.group(2) != null) {
cache.setName(Html.fromHtml(matcherGuidAndDisabled.group(2).trim()).toString());
}
if (matcherGuidAndDisabled.group(3) != null) {
cache.setLocation(Html.fromHtml(matcherGuidAndDisabled.group(3).trim()).toString());
}
final String attr = matcherGuidAndDisabled.group(1);
if (attr != null) {
cache.setDisabled(attr.contains("Strike"));
cache.setArchived(attr.contains("OldWarning"));
}
}
}
} catch (final RuntimeException e) {
// failed to parse GUID and/or Disabled
Log.w("GCParser.parseSearch: Failed to parse GUID and/or Disabled data", e);
}
if (Settings.isExcludeDisabledCaches() && (cache.isDisabled() || cache.isArchived())) {
// skip disabled and archived caches
excludedCaches++;
continue;
}
cache.setGeocode(TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_GEOCODE, true, 1, cache.getGeocode(), true));
// cache type
cache.setType(CacheType.getByPattern(TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_TYPE, null)));
// cache direction - image
if (Settings.getLoadDirImg()) {
final String direction = TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_DIRECTION_DISTANCE, false, null);
if (direction != null) {
cache.setDirectionImg(direction);
}
}
// cache distance - estimated distance for basic members
final String distance = TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_DIRECTION_DISTANCE, false, 2, null, false);
if (distance != null) {
cache.setDistance(DistanceParser.parseDistance(distance,
!Settings.useImperialUnits()));
}
// difficulty/terrain
final MatcherWrapper matcherDT = new MatcherWrapper(GCConstants.PATTERN_SEARCH_DIFFICULTY_TERRAIN, row);
if (matcherDT.find()) {
final Float difficulty = parseStars(matcherDT.group(1));
if (difficulty != null) {
cache.setDifficulty(difficulty);
}
final Float terrain = parseStars(matcherDT.group(3));
if (terrain != null) {
cache.setTerrain(terrain);
}
}
// size
final String container = TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_CONTAINER, false, null);
cache.setSize(CacheSize.getById(container));
// date hidden, makes sorting event caches easier
final String dateHidden = TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_HIDDEN_DATE, false, null);
if (StringUtils.isNotBlank(dateHidden)) {
try {
final Date date = GCLogin.parseGcCustomDate(dateHidden);
if (date != null) {
cache.setHidden(date);
}
} catch (final ParseException e) {
Log.e("Error parsing event date from search", e);
}
}
// cache inventory
final MatcherWrapper matcherTbs = new MatcherWrapper(GCConstants.PATTERN_SEARCH_TRACKABLES, row);
String inventoryPre = null;
while (matcherTbs.find()) {
if (matcherTbs.groupCount() > 0) {
try {
cache.setInventoryItems(Integer.parseInt(matcherTbs.group(1)));
} catch (final NumberFormatException e) {
Log.e("Error parsing trackables count", e);
}
inventoryPre = matcherTbs.group(2);
}
}
if (StringUtils.isNotBlank(inventoryPre)) {
assert inventoryPre != null;
final MatcherWrapper matcherTbsInside = new MatcherWrapper(GCConstants.PATTERN_SEARCH_TRACKABLESINSIDE, inventoryPre);
while (matcherTbsInside.find()) {
if (matcherTbsInside.groupCount() == 2 &&
matcherTbsInside.group(2) != null &&
!matcherTbsInside.group(2).equalsIgnoreCase("premium member only cache") &&
cache.getInventoryItems() <= 0) {
cache.setInventoryItems(1);
}
}
}
// premium cache
cache.setPremiumMembersOnly(row.contains("/images/icons/16/premium_only.png"));
// found it
cache.setFound(row.contains("/images/icons/16/found.png") || row.contains("uxUserLogDate\" class=\"Success\""));
// id
String result = TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_ID, null);
if (null != result) {
cache.setCacheId(result);
cids.add(cache.getCacheId());
}
// favorite count
try {
result = getNumberString(TextUtils.getMatch(row, GCConstants.PATTERN_SEARCH_FAVORITE, false, 1, null, true));
if (null != result) {
cache.setFavoritePoints(Integer.parseInt(result));
}
} catch (final NumberFormatException e) {
Log.w("GCParser.parseSearch: Failed to parse favorite count", e);
}
caches.add(cache);
}
searchResult.addAndPutInCache(caches);
// total caches found
try {
final String result = TextUtils.getMatch(page, GCConstants.PATTERN_SEARCH_TOTALCOUNT, false, 1, null, true);
if (null != result) {
searchResult.setTotalCountGC(Integer.parseInt(result) - excludedCaches);
}
} catch (final NumberFormatException e) {
Log.w("GCParser.parseSearch: Failed to parse cache count", e);
}
String recaptchaText = null;
if (recaptchaReceiver != null && StringUtils.isNotBlank(recaptchaReceiver.getChallenge())) {
recaptchaReceiver.waitForUser();
recaptchaText = recaptchaReceiver.getText();
}
if (!cids.isEmpty() && (Settings.isGCPremiumMember() || showCaptcha) && ((recaptchaReceiver == null || StringUtils.isBlank(recaptchaReceiver.getChallenge())) || StringUtils.isNotBlank(recaptchaText))) {
Log.i("Trying to get .loc for " + cids.size() + " caches");
final Observable<Set<Geocache>> storedCaches = Async.start(new Func0<Set<Geocache>>() {
@Override
public Set<Geocache> call() {
return DataStore.loadCaches(Geocache.getGeocodes(caches), LoadFlags.LOAD_CACHE_OR_DB);
}
}, Schedulers.io());
try {
// get coordinates for parsed caches
final Parameters params = new Parameters(
"__EVENTTARGET", "",
"__EVENTARGUMENT", "");
GCLogin.putViewstates(params, searchResult.viewstates);
for (final String cid : cids) {
params.put("CID", cid);
}
if (StringUtils.isNotBlank(recaptchaText) && recaptchaReceiver != null) {
params.put("recaptcha_challenge_field", recaptchaReceiver.getChallenge());
params.put("recaptcha_response_field", recaptchaText);
}
params.put("ctl00$ContentBody$uxDownloadLoc", "Download Waypoints");
final String coordinates = Network.getResponseData(Network.postRequest("http://www.geocaching.com/seek/nearest.aspx", params), false);
if (StringUtils.contains(coordinates, "You have not agreed to the license agreement. The license agreement is required before you can start downloading GPX or LOC files from Geocaching.com")) {
Log.i("User has not agreed to the license agreement. Can\'t download .loc file.");
searchResult.setError(StatusCode.UNAPPROVED_LICENSE);
return searchResult;
}
LocParser.parseLoc(searchResult, coordinates, storedCaches.toBlocking().single());
} catch (final RuntimeException e) {
Log.e("GCParser.parseSearch.CIDs", e);
}
}
// get direction images
if (Settings.getLoadDirImg()) {
final Set<Geocache> cachesReloaded = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB);
for (final Geocache cache : cachesReloaded) {
if (cache.getCoords() == null && StringUtils.isNotEmpty(cache.getDirectionImg())) {
DirectionImage.getDrawable(cache.getDirectionImg());
}
}
}
return searchResult;
}
@Nullable
private static Float parseStars(final String value) {
final float floatValue = Float.parseFloat(StringUtils.replaceChars(value, ',', '.'));
return floatValue >= 0.5 && floatValue <= 5.0 ? floatValue : null;
}
@Nullable
static SearchResult parseCache(final String page, final CancellableHandler handler) {
final ImmutablePair<StatusCode, Geocache> parsed = parseCacheFromText(page, handler);
// attention: parseCacheFromText already stores implicitly through searchResult.addCache
if (parsed.left != StatusCode.NO_ERROR) {
return new SearchResult(parsed.left);
}
final Geocache cache = parsed.right;
getExtraOnlineInfo(cache, page, handler);
// too late: it is already stored through parseCacheFromText
cache.setDetailedUpdatedNow();
if (CancellableHandler.isCancelled(handler)) {
return null;
}
// save full detailed caches
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_cache);
DataStore.saveCache(cache, EnumSet.of(SaveFlag.DB));
// update progress message so user knows we're still working. This is more of a place holder than
// actual indication of what the program is doing
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_render);
return new SearchResult(cache);
}
@NonNull
static SearchResult parseAndSaveCacheFromText(final String page, @Nullable final CancellableHandler handler) {
final ImmutablePair<StatusCode, Geocache> parsed = parseCacheFromText(page, handler);
final SearchResult result = new SearchResult(parsed.left);
if (parsed.left == StatusCode.NO_ERROR) {
result.addAndPutInCache(Collections.singletonList(parsed.right));
DataStore.saveLogs(parsed.right.getGeocode(), getLogs(parseUserToken(page), Logs.ALL).toBlocking().toIterable());
}
return result;
}
/**
* Parse cache from text and return either an error code or a cache object in a pair. Note that inline logs are
* not parsed nor saved, while the cache itself is.
*
* @param pageIn
* the page text to parse
* @param handler
* the handler to send the progress notifications to
* @return a pair, with a {@link StatusCode} on the left, and a non-null cache object on the right
* iff the status code is {@link cgeo.geocaching.enumerations.StatusCode#NO_ERROR}.
*/
@NonNull
static private ImmutablePair<StatusCode, Geocache> parseCacheFromText(final String pageIn, @Nullable final CancellableHandler handler) {
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_details);
if (StringUtils.isBlank(pageIn)) {
Log.e("GCParser.parseCache: No page given");
return UNKNOWN_PARSE_ERROR;
}
if (pageIn.contains(GCConstants.STRING_UNPUBLISHED_OTHER) || pageIn.contains(GCConstants.STRING_UNPUBLISHED_FROM_SEARCH)) {
return ImmutablePair.of(StatusCode.UNPUBLISHED_CACHE, null);
}
if (pageIn.contains(GCConstants.STRING_PREMIUMONLY_1) || pageIn.contains(GCConstants.STRING_PREMIUMONLY_2)) {
return ImmutablePair.of(StatusCode.PREMIUM_ONLY, null);
}
final String cacheName = Html.fromHtml(TextUtils.getMatch(pageIn, GCConstants.PATTERN_NAME, true, "")).toString();
if (GCConstants.STRING_UNKNOWN_ERROR.equalsIgnoreCase(cacheName)) {
return UNKNOWN_PARSE_ERROR;
}
// first handle the content with line breaks, then trim everything for easier matching and reduced memory consumption in parsed fields
String personalNoteWithLineBreaks = "";
final MatcherWrapper matcher = new MatcherWrapper(GCConstants.PATTERN_PERSONALNOTE, pageIn);
if (matcher.find()) {
personalNoteWithLineBreaks = matcher.group(1).trim();
}
final String page = TextUtils.replaceWhitespace(pageIn);
final Geocache cache = new Geocache();
cache.setDisabled(page.contains(GCConstants.STRING_DISABLED));
cache.setArchived(page.contains(GCConstants.STRING_ARCHIVED));
cache.setPremiumMembersOnly(TextUtils.matches(page, GCConstants.PATTERN_PREMIUMMEMBERS));
cache.setFavorite(TextUtils.matches(page, GCConstants.PATTERN_FAVORITE));
// cache geocode
cache.setGeocode(TextUtils.getMatch(page, GCConstants.PATTERN_GEOCODE, true, cache.getGeocode()));
// cache id
cache.setCacheId(TextUtils.getMatch(page, GCConstants.PATTERN_CACHEID, true, cache.getCacheId()));
// cache guid
cache.setGuid(TextUtils.getMatch(page, GCConstants.PATTERN_GUID, true, cache.getGuid()));
// name
cache.setName(cacheName);
// owner real name
cache.setOwnerUserId(Network.decode(TextUtils.getMatch(page, GCConstants.PATTERN_OWNER_USERID, true, cache.getOwnerUserId())));
cache.setUserModifiedCoords(false);
String tableInside = page;
final int pos = tableInside.indexOf(GCConstants.STRING_CACHEDETAILS);
if (pos == -1) {
Log.e("GCParser.parseCache: ID \"cacheDetails\" not found on page");
return UNKNOWN_PARSE_ERROR;
}
tableInside = tableInside.substring(pos);
if (StringUtils.isNotBlank(tableInside)) {
// cache terrain
String result = TextUtils.getMatch(tableInside, GCConstants.PATTERN_TERRAIN, true, null);
if (result != null) {
try {
cache.setTerrain(Float.parseFloat(StringUtils.replaceChars(result, '_', '.')));
} catch (final NumberFormatException e) {
Log.e("Error parsing terrain value", e);
}
}
// cache difficulty
result = TextUtils.getMatch(tableInside, GCConstants.PATTERN_DIFFICULTY, true, null);
if (result != null) {
try {
cache.setDifficulty(Float.parseFloat(StringUtils.replaceChars(result, '_', '.')));
} catch (final NumberFormatException e) {
Log.e("Error parsing difficulty value", e);
}
}
// owner
cache.setOwnerDisplayName(StringEscapeUtils.unescapeHtml4(TextUtils.getMatch(tableInside, GCConstants.PATTERN_OWNER_DISPLAYNAME, true, cache.getOwnerDisplayName())));
// hidden
try {
String hiddenString = TextUtils.getMatch(tableInside, GCConstants.PATTERN_HIDDEN, true, null);
if (StringUtils.isNotBlank(hiddenString)) {
cache.setHidden(GCLogin.parseGcCustomDate(hiddenString));
}
if (cache.getHiddenDate() == null) {
// event date
hiddenString = TextUtils.getMatch(tableInside, GCConstants.PATTERN_HIDDENEVENT, true, null);
if (StringUtils.isNotBlank(hiddenString)) {
cache.setHidden(GCLogin.parseGcCustomDate(hiddenString));
}
}
} catch (final ParseException e) {
// failed to parse cache hidden date
Log.w("GCParser.parseCache: Failed to parse cache hidden (event) date", e);
}
// favorite
try {
cache.setFavoritePoints(Integer.parseInt(TextUtils.getMatch(tableInside, GCConstants.PATTERN_FAVORITECOUNT, true, "0")));
} catch (final NumberFormatException e) {
Log.e("Error parsing favorite count", e);
}
// cache size
cache.setSize(CacheSize.getById(TextUtils.getMatch(tableInside, GCConstants.PATTERN_SIZE, true, CacheSize.NOT_CHOSEN.id)));
}
// cache found
cache.setFound(TextUtils.matches(page, GCConstants.PATTERN_FOUND) || TextUtils.matches(page, GCConstants.PATTERN_FOUND_ALTERNATIVE));
// cache type
cache.setType(CacheType.getByGuid(TextUtils.getMatch(page, GCConstants.PATTERN_TYPE, true, cache.getType().id)));
// on watchlist
cache.setOnWatchlist(TextUtils.matches(page, GCConstants.PATTERN_WATCHLIST));
// latitude and longitude. Can only be retrieved if user is logged in
String latlon = TextUtils.getMatch(page, GCConstants.PATTERN_LATLON, true, "");
if (StringUtils.isNotEmpty(latlon)) {
try {
cache.setCoords(new Geopoint(latlon));
cache.setReliableLatLon(true);
} catch (final Geopoint.GeopointException e) {
Log.w("GCParser.parseCache: Failed to parse cache coordinates", e);
}
}
// cache location
cache.setLocation(TextUtils.getMatch(page, GCConstants.PATTERN_LOCATION, true, ""));
// cache hint
final String result = TextUtils.getMatch(page, GCConstants.PATTERN_HINT, false, null);
if (result != null) {
// replace linebreak and paragraph tags
final String hint = GCConstants.PATTERN_LINEBREAK.matcher(result).replaceAll("\n");
cache.setHint(StringUtils.replace(hint, "</p>", "").trim());
}
cache.checkFields();
// cache personal note
cache.setPersonalNote(personalNoteWithLineBreaks);
// cache short description
cache.setShortDescription(TextUtils.getMatch(page, GCConstants.PATTERN_SHORTDESC, true, ""));
// cache description
final String longDescription = TextUtils.getMatch(page, GCConstants.PATTERN_DESC, true, "");
String relatedWebPage = TextUtils.getMatch(page, GCConstants.PATTERN_RELATED_WEB_PAGE, true, "");
if (StringUtils.isNotEmpty(relatedWebPage)) {
relatedWebPage = String.format("<br/><br/><a href=\"%s\"><b>%s</b></a>", relatedWebPage, relatedWebPage);
}
cache.setDescription(longDescription + relatedWebPage);
// cache attributes
try {
final ArrayList<String> attributes = new ArrayList<>();
final String attributesPre = TextUtils.getMatch(page, GCConstants.PATTERN_ATTRIBUTES, true, null);
if (attributesPre != null) {
final MatcherWrapper matcherAttributesInside = new MatcherWrapper(GCConstants.PATTERN_ATTRIBUTESINSIDE, attributesPre);
while (matcherAttributesInside.find()) {
if (matcherAttributesInside.groupCount() > 1 && !matcherAttributesInside.group(2).equalsIgnoreCase("blank")) {
// by default, use the tooltip of the attribute
String attribute = matcherAttributesInside.group(2).toLowerCase(Locale.US);
// if the image name can be recognized, use the image name as attribute
final String imageName = matcherAttributesInside.group(1).trim();
if (StringUtils.isNotEmpty(imageName)) {
final int start = imageName.lastIndexOf('/');
final int end = imageName.lastIndexOf('.');
if (start >= 0 && end >= 0) {
attribute = imageName.substring(start + 1, end).replace('-', '_').toLowerCase(Locale.US);
}
}
attributes.add(attribute);
}
}
}
cache.setAttributes(attributes);
} catch (final RuntimeException e) {
// failed to parse cache attributes
Log.w("GCParser.parseCache: Failed to parse cache attributes", e);
}
// cache spoilers
try {
if (CancellableHandler.isCancelled(handler)) {
return UNKNOWN_PARSE_ERROR;
}
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_spoilers);
final MatcherWrapper matcherSpoilersInside = new MatcherWrapper(GCConstants.PATTERN_SPOILER_IMAGE, page);
while (matcherSpoilersInside.find()) {
// the original spoiler URL (include .../display/... contains a low-resolution image
// if we shorten the URL we get the original-resolution image
final String url = matcherSpoilersInside.group(1).replace("/display", "");
String title = null;
if (matcherSpoilersInside.group(3) != null) {
title = matcherSpoilersInside.group(3);
}
String description = null;
if (matcherSpoilersInside.group(4) != null) {
description = matcherSpoilersInside.group(4);
}
cache.addSpoiler(new Image(url, title, description));
}
} catch (final RuntimeException e) {
// failed to parse cache spoilers
Log.w("GCParser.parseCache: Failed to parse cache spoilers", e);
}
// cache inventory
try {
cache.setInventoryItems(0);
final MatcherWrapper matcherInventory = new MatcherWrapper(GCConstants.PATTERN_INVENTORY, page);
if (matcherInventory.find()) {
if (cache.getInventory() == null) {
cache.setInventory(new ArrayList<Trackable>());
}
if (matcherInventory.groupCount() > 1) {
final String inventoryPre = matcherInventory.group(2);
if (StringUtils.isNotBlank(inventoryPre)) {
final MatcherWrapper matcherInventoryInside = new MatcherWrapper(GCConstants.PATTERN_INVENTORYINSIDE, inventoryPre);
while (matcherInventoryInside.find()) {
if (matcherInventoryInside.groupCount() > 0) {
final Trackable inventoryItem = new Trackable();
inventoryItem.setGuid(matcherInventoryInside.group(1));
inventoryItem.setName(matcherInventoryInside.group(2));
cache.getInventory().add(inventoryItem);
cache.setInventoryItems(cache.getInventoryItems() + 1);
}
}
}
}
}
} catch (final RuntimeException e) {
// failed to parse cache inventory
Log.w("GCParser.parseCache: Failed to parse cache inventory (2)", e);
}
// cache logs counts
try {
final String countlogs = TextUtils.getMatch(page, GCConstants.PATTERN_COUNTLOGS, true, null);
if (null != countlogs) {
final MatcherWrapper matcherLog = new MatcherWrapper(GCConstants.PATTERN_COUNTLOG, countlogs);
while (matcherLog.find()) {
final String typeStr = matcherLog.group(1);
final String countStr = getNumberString(matcherLog.group(2));
if (StringUtils.isNotBlank(typeStr)
&& LogType.UNKNOWN != LogType.getByIconName(typeStr)
&& StringUtils.isNotBlank(countStr)) {
cache.getLogCounts().put(LogType.getByIconName(typeStr), Integer.parseInt(countStr));
}
}
}
} catch (final NumberFormatException e) {
// failed to parse logs
Log.w("GCParser.parseCache: Failed to parse cache log count", e);
}
// waypoints - reset collection
cache.setWaypoints(Collections.<Waypoint> emptyList(), false);
// add waypoint for original coordinates in case of user-modified listing-coordinates
try {
final String originalCoords = TextUtils.getMatch(page, GCConstants.PATTERN_LATLON_ORIG, false, null);
if (null != originalCoords) {
final Waypoint waypoint = new Waypoint(CgeoApplication.getInstance().getString(R.string.cache_coordinates_original), WaypointType.ORIGINAL, false);
waypoint.setCoords(new Geopoint(originalCoords));
cache.addOrChangeWaypoint(waypoint, false);
cache.setUserModifiedCoords(true);
}
} catch (final Geopoint.GeopointException ignored) {
}
int wpBegin = page.indexOf("<table class=\"Table\" id=\"ctl00_ContentBody_Waypoints\">");
if (wpBegin != -1) { // parse waypoints
if (CancellableHandler.isCancelled(handler)) {
return UNKNOWN_PARSE_ERROR;
}
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_waypoints);
String wpList = page.substring(wpBegin);
int wpEnd = wpList.indexOf("</p>");
if (wpEnd > -1 && wpEnd <= wpList.length()) {
wpList = wpList.substring(0, wpEnd);
}
if (!wpList.contains("No additional waypoints to display.")) {
wpEnd = wpList.indexOf("</table>");
wpList = wpList.substring(0, wpEnd);
wpBegin = wpList.indexOf("<tbody>");
wpEnd = wpList.indexOf("</tbody>");
if (wpBegin >= 0 && wpEnd >= 0 && wpEnd <= wpList.length()) {
wpList = wpList.substring(wpBegin + 7, wpEnd);
}
final String[] wpItems = StringUtils.splitByWholeSeparator(wpList, "<tr");
for (int j = 1; j < wpItems.length; j += 2) {
final String[] wp = StringUtils.splitByWholeSeparator(wpItems[j], "<td");
assert wp != null;
if (wp.length < 8) {
Log.e("GCParser.cacheParseFromText: not enough waypoint columns in table");
continue;
}
// waypoint name
// res is null during the unit tests
final String name = TextUtils.getMatch(wp[6], GCConstants.PATTERN_WPNAME, true, 1, CgeoApplication.getInstance().getString(R.string.waypoint), true);
// waypoint type
final String resulttype = TextUtils.getMatch(wp[3], GCConstants.PATTERN_WPTYPE, null);
final Waypoint waypoint = new Waypoint(name, WaypointType.findById(resulttype), false);
// waypoint prefix
waypoint.setPrefix(TextUtils.getMatch(wp[4], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, true, 2, waypoint.getPrefix(), false));
// waypoint lookup
waypoint.setLookup(TextUtils.getMatch(wp[5], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, true, 2, waypoint.getLookup(), false));
// waypoint latitude and longitude
latlon = Html.fromHtml(TextUtils.getMatch(wp[7], GCConstants.PATTERN_WPPREFIXORLOOKUPORLATLON, false, 2, "", false)).toString().trim();
if (!StringUtils.startsWith(latlon, "???")) {
waypoint.setCoords(new Geopoint(latlon));
}
if (wpItems.length >= j) {
final String[] wpNote = StringUtils.splitByWholeSeparator(wpItems[j + 1], "<td");
assert wpNote != null;
if (wpNote.length < 4) {
Log.d("GCParser.cacheParseFromText: not enough waypoint columns in table to extract note");
continue;
}
// waypoint note
waypoint.setNote(TextUtils.getMatch(wpNote[3], GCConstants.PATTERN_WPNOTE, waypoint.getNote()));
}
cache.addOrChangeWaypoint(waypoint, false);
}
}
}
// last check for necessary cache conditions
if (StringUtils.isBlank(cache.getGeocode())) {
return UNKNOWN_PARSE_ERROR;
}
cache.setDetailedUpdatedNow();
return ImmutablePair.of(StatusCode.NO_ERROR, cache);
}
@Nullable
private static String getNumberString(final String numberWithPunctuation) {
return StringUtils.replaceChars(numberWithPunctuation, ".,", "");
}
@Nullable
public static SearchResult searchByNextPage(final SearchResult search, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (search == null) {
return null;
}
final String[] viewstates = search.getViewstates();
final String url = search.getUrl();
if (StringUtils.isBlank(url)) {
Log.e("GCParser.searchByNextPage: No url found");
return search;
}
if (GCLogin.isEmpty(viewstates)) {
Log.e("GCParser.searchByNextPage: No viewstate given");
return search;
}
final Parameters params = new Parameters(
"__EVENTTARGET", "ctl00$ContentBody$pgrBottom$ctl08",
"__EVENTARGUMENT", "");
GCLogin.putViewstates(params, viewstates);
final String page = GCLogin.getInstance().postRequestLogged(url, params);
if (!GCLogin.getInstance().getLoginStatus(page)) {
Log.e("GCParser.postLogTrackable: Can not log in geocaching");
return search;
}
if (StringUtils.isBlank(page)) {
Log.e("GCParser.searchByNextPage: No data from server");
return search;
}
final SearchResult searchResult = parseSearch(url, page, showCaptcha, recaptchaReceiver);
if (searchResult == null || CollectionUtils.isEmpty(searchResult.getGeocodes())) {
Log.w("GCParser.searchByNextPage: No cache parsed");
return search;
}
// search results don't need to be filtered so load GCVote ratings here
GCVote.loadRatings(new ArrayList<>(searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB)));
// save to application
search.setError(searchResult.getError());
search.setViewstates(searchResult.viewstates);
for (final String geocode : searchResult.getGeocodes()) {
search.addGeocode(geocode);
}
return search;
}
/**
* Possibly hide caches found or hidden by user. This mutates its params argument when possible.
*
* @param params the parameters to mutate, or null to create a new Parameters if needed
* @param my {@code true} if the user's caches must be forcibly included regardless of their settings
* @return the original params if not null, maybe augmented with f=1, or a new Parameters with f=1 or null otherwise
*/
private static Parameters addFToParams(final Parameters params, final boolean my) {
if (!my && Settings.isExcludeMyCaches()) {
if (params == null) {
return new Parameters("f", "1");
}
params.put("f", "1");
Log.i("Skipping caches found or hidden by user.");
}
return params;
}
@Nullable
private static SearchResult searchByAny(@NonNull final CacheType cacheType, final boolean my, final boolean showCaptcha, final Parameters params, final RecaptchaReceiver recaptchaReceiver) {
insertCacheType(params, cacheType);
final String uri = "http://www.geocaching.com/seek/nearest.aspx";
final Parameters paramsWithF = addFToParams(params, my);
final String fullUri = uri + "?" + paramsWithF;
final String page = GCLogin.getInstance().getRequestLogged(uri, paramsWithF);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.searchByAny: No data from server");
return null;
}
assert page != null;
final SearchResult searchResult = parseSearch(fullUri, page, showCaptcha, recaptchaReceiver);
if (searchResult == null || CollectionUtils.isEmpty(searchResult.getGeocodes())) {
Log.e("GCParser.searchByAny: No cache parsed");
return searchResult;
}
final SearchResult search = searchResult.filterSearchResults(Settings.isExcludeDisabledCaches(), false, cacheType);
GCLogin.getInstance().getLoginStatus(page);
return search;
}
public static SearchResult searchByCoords(final @NonNull Geopoint coords, @NonNull final CacheType cacheType, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
final Parameters params = new Parameters("lat", Double.toString(coords.getLatitude()), "lng", Double.toString(coords.getLongitude()));
return searchByAny(cacheType, false, showCaptcha, params, recaptchaReceiver);
}
public static SearchResult searchByKeyword(final @NonNull String keyword, @NonNull final CacheType cacheType, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (StringUtils.isBlank(keyword)) {
Log.e("GCParser.searchByKeyword: No keyword given");
return null;
}
final Parameters params = new Parameters("key", keyword);
return searchByAny(cacheType, false, showCaptcha, params, recaptchaReceiver);
}
private static boolean isSearchForMyCaches(final String userName) {
if (userName.equalsIgnoreCase(Settings.getGcCredentials().left)) {
Log.i("Overriding users choice because of self search, downloading all caches.");
return true;
}
return false;
}
public static SearchResult searchByUsername(final String userName, @NonNull final CacheType cacheType, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (StringUtils.isBlank(userName)) {
Log.e("GCParser.searchByUsername: No user name given");
return null;
}
final Parameters params = new Parameters("ul", userName);
return searchByAny(cacheType, isSearchForMyCaches(userName), showCaptcha, params, recaptchaReceiver);
}
public static SearchResult searchByPocketQuery(final String pocketGuid, @NonNull final CacheType cacheType, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (StringUtils.isBlank(pocketGuid)) {
Log.e("GCParser.searchByPocket: No guid name given");
return null;
}
final Parameters params = new Parameters("pq", pocketGuid);
return searchByAny(cacheType, false, showCaptcha, params, recaptchaReceiver);
}
public static SearchResult searchByOwner(final String userName, @NonNull final CacheType cacheType, final boolean showCaptcha, final RecaptchaReceiver recaptchaReceiver) {
if (StringUtils.isBlank(userName)) {
Log.e("GCParser.searchByOwner: No user name given");
return null;
}
final Parameters params = new Parameters("u", userName);
return searchByAny(cacheType, isSearchForMyCaches(userName), showCaptcha, params, recaptchaReceiver);
}
@Nullable
public static Trackable searchTrackable(final String geocode, final String guid, final String id) {
if (StringUtils.isBlank(geocode) && StringUtils.isBlank(guid) && StringUtils.isBlank(id)) {
Log.w("GCParser.searchTrackable: No geocode nor guid nor id given");
return null;
}
Trackable trackable = new Trackable();
final Parameters params = new Parameters();
if (StringUtils.isNotBlank(geocode)) {
params.put("tracker", geocode);
trackable.setGeocode(geocode);
} else if (StringUtils.isNotBlank(guid)) {
params.put("guid", guid);
} else if (StringUtils.isNotBlank(id)) {
params.put("id", id);
}
final String page = GCLogin.getInstance().getRequestLogged("http://www.geocaching.com/track/details.aspx", params);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.searchTrackable: No data from server");
return trackable;
}
assert page != null;
trackable = parseTrackable(page, geocode);
if (trackable == null) {
Log.w("GCParser.searchTrackable: No trackable parsed");
return null;
}
return trackable;
}
/**
* Observable that fetches a list of pocket queries. Returns a single element (which may be an empty list).
* Executes on the network scheduler.
*/
public static final Observable<List<PocketQueryList>> searchPocketQueryListObservable = Async.fromCallable(new Func0<List<PocketQueryList>>() {
@Override
public List<PocketQueryList> call() {
final Parameters params = new Parameters();
final String page = GCLogin.getInstance().getRequestLogged("http://www.geocaching.com/pocket/default.aspx", params);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.searchPocketQueryList: No data from server");
return Collections.emptyList();
}
final String subPage = StringUtils.substringAfter(page, "class=\"PocketQueryListTable");
if (StringUtils.isEmpty(subPage)) {
Log.e("GCParser.searchPocketQueryList: class \"PocketQueryListTable\" not found on page");
return Collections.emptyList();
}
final List<PocketQueryList> list = new ArrayList<>();
final MatcherWrapper matcherPocket = new MatcherWrapper(GCConstants.PATTERN_LIST_PQ, subPage);
while (matcherPocket.find()) {
int maxCaches;
try {
maxCaches = Integer.parseInt(matcherPocket.group(1));
} catch (final NumberFormatException e) {
maxCaches = 0;
Log.e("GCParser.searchPocketQueryList: Unable to parse max caches", e);
}
final String guid = Html.fromHtml(matcherPocket.group(2)).toString();
final String name = Html.fromHtml(matcherPocket.group(3)).toString();
final PocketQueryList pqList = new PocketQueryList(guid, name, maxCaches);
list.add(pqList);
}
// just in case, lets sort the resulting list
Collections.sort(list, new Comparator<PocketQueryList>() {
@Override
public int compare(final PocketQueryList left, final PocketQueryList right) {
return String.CASE_INSENSITIVE_ORDER.compare(left.getName(), right.getName());
}
});
return list;
}
}, RxUtils.networkScheduler);
public static ImmutablePair<StatusCode, String> postLog(final String geocode, final String cacheid, final String[] viewstates,
final LogType logType, final int year, final int month, final int day,
final String log, final List<TrackableLog> trackables) {
if (GCLogin.isEmpty(viewstates)) {
Log.e("GCParser.postLog: No viewstate given");
return new ImmutablePair<>(StatusCode.LOG_POST_ERROR, "");
}
if (StringUtils.isBlank(log)) {
Log.e("GCParser.postLog: No log text given");
return new ImmutablePair<>(StatusCode.NO_LOG_TEXT, "");
}
final String logInfo = log.replace("\n", "\r\n").trim(); // windows' eol and remove leading and trailing whitespaces
Log.i("Trying to post log for cache #" + cacheid + " - action: " + logType
+ "; date: " + year + "." + month + "." + day + ", log: " + logInfo
+ "; trackables: " + (trackables != null ? trackables.size() : "0"));
final Parameters params = new Parameters(
"__EVENTTARGET", "",
"__EVENTARGUMENT", "",
"__LASTFOCUS", "",
"ctl00$ContentBody$LogBookPanel1$ddLogType", Integer.toString(logType.id),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited", GCLogin.formatGcCustomDate(year, month, day),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Month", Integer.toString(month),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Day", Integer.toString(day),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Year", Integer.toString(year),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged", String.format("%02d", month) + "/" + String.format("%02d", day) + "/" + String.format("%04d", year),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Month", Integer.toString(month),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Day", Integer.toString(day),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Year", Integer.toString(year),
"ctl00$ContentBody$LogBookPanel1$LogButton", "Submit Log Entry",
"ctl00$ContentBody$LogBookPanel1$uxLogInfo", logInfo,
"ctl00$ContentBody$LogBookPanel1$btnSubmitLog", "Submit Log Entry",
"ctl00$ContentBody$LogBookPanel1$uxLogCreationSource", "Old",
"ctl00$ContentBody$uxVistOtherListingGC", "");
GCLogin.putViewstates(params, viewstates);
if (trackables != null && !trackables.isEmpty()) { // we have some trackables to proceed
final StringBuilder hdnSelected = new StringBuilder();
for (final TrackableLog tb : trackables) {
if (tb.action != LogTypeTrackable.DO_NOTHING) {
hdnSelected.append(Integer.toString(tb.id));
hdnSelected.append(tb.action.action);
hdnSelected.append(',');
}
}
params.put("ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnSelectedActions", hdnSelected.toString(), // selected trackables
"ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnCurrentFilter", "");
}
final String uri = new Uri.Builder().scheme("http").authority("www.geocaching.com").path("/seek/log.aspx").encodedQuery("ID=" + cacheid).build().toString();
final GCLogin gcLogin = GCLogin.getInstance();
String page = gcLogin.postRequestLogged(uri, params);
if (!gcLogin.getLoginStatus(page)) {
Log.e("GCParser.postLog: Cannot log in geocaching");
return new ImmutablePair<>(StatusCode.NOT_LOGGED_IN, "");
}
// maintenance, archived needs to be confirmed
final MatcherWrapper matcher = new MatcherWrapper(GCConstants.PATTERN_MAINTENANCE, page);
try {
if (matcher.find() && matcher.groupCount() > 0) {
final String[] viewstatesConfirm = GCLogin.getViewstates(page);
if (GCLogin.isEmpty(viewstatesConfirm)) {
Log.e("GCParser.postLog: No viewstate for confirm log");
return new ImmutablePair<>(StatusCode.LOG_POST_ERROR, "");
}
params.clear();
GCLogin.putViewstates(params, viewstatesConfirm);
params.put("__EVENTTARGET", "");
params.put("__EVENTARGUMENT", "");
params.put("__LASTFOCUS", "");
params.put("ctl00$ContentBody$LogBookPanel1$btnConfirm", "Yes");
params.put("ctl00$ContentBody$LogBookPanel1$uxLogInfo", logInfo);
params.put("ctl00$ContentBody$uxVistOtherListingGC", "");
if (trackables != null && !trackables.isEmpty()) { // we have some trackables to proceed
final StringBuilder hdnSelected = new StringBuilder();
for (final TrackableLog tb : trackables) {
final String action = Integer.toString(tb.id) + tb.action.action;
final StringBuilder paramText = new StringBuilder("ctl00$ContentBody$LogBookPanel1$uxTrackables$repTravelBugs$ctl");
if (tb.ctl < 10) {
paramText.append('0');
}
paramText.append(tb.ctl).append("$ddlAction");
params.put(paramText.toString(), action);
if (tb.action != LogTypeTrackable.DO_NOTHING) {
hdnSelected.append(action);
hdnSelected.append(',');
}
}
params.put("ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnSelectedActions", hdnSelected.toString()); // selected trackables
params.put("ctl00$ContentBody$LogBookPanel1$uxTrackables$hdnCurrentFilter", "");
}
page = Network.getResponseData(Network.postRequest(uri, params));
}
} catch (final RuntimeException e) {
Log.e("GCParser.postLog.confim", e);
}
if (page == null) {
Log.e("GCParser.postLog: didn't get response");
return new ImmutablePair<>(StatusCode.LOG_POST_ERROR, "");
}
try {
final MatcherWrapper matcherOk = new MatcherWrapper(GCConstants.PATTERN_OK1, page);
if (matcherOk.find()) {
Log.i("Log successfully posted to cache #" + cacheid);
if (geocode != null) {
DataStore.saveVisitDate(geocode);
}
gcLogin.getLoginStatus(page);
// the log-successful-page contains still the old value
if (gcLogin.getActualCachesFound() >= 0) {
gcLogin.setActualCachesFound(gcLogin.getActualCachesFound() + 1);
}
final String logID = TextUtils.getMatch(page, GCConstants.PATTERN_LOG_IMAGE_UPLOAD, "");
return new ImmutablePair<>(StatusCode.NO_ERROR, logID);
}
} catch (final Exception e) {
Log.e("GCParser.postLog.check", e);
}
Log.e("GCParser.postLog: Failed to post log because of unknown error");
return new ImmutablePair<>(StatusCode.LOG_POST_ERROR, "");
}
/**
* Upload an image to a log that has already been posted
*
* @param logId
* the ID of the log to upload the image to. Found on page returned when log is uploaded
* @param caption
* of the image; max 50 chars
* @param description
* of the image; max 250 chars
* @param imageUri
* the URI for the image to be uploaded
* @return status code to indicate success or failure
*/
public static ImmutablePair<StatusCode, String> uploadLogImage(final String logId, final String caption, final String description, final Uri imageUri) {
final String uri = new Uri.Builder().scheme("http").authority("www.geocaching.com").path("/seek/upload.aspx").encodedQuery("LID=" + logId).build().toString();
final String page = GCLogin.getInstance().getRequestLogged(uri, null);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.uploadLogImage: No data from server");
return new ImmutablePair<>(StatusCode.UNKNOWN_ERROR, null);
}
assert page != null;
final String[] viewstates = GCLogin.getViewstates(page);
final Parameters uploadParams = new Parameters(
"__EVENTTARGET", "",
"__EVENTARGUMENT", "",
"ctl00$ContentBody$ImageUploadControl1$uxFileCaption", caption,
"ctl00$ContentBody$ImageUploadControl1$uxFileDesc", description,
"ctl00$ContentBody$ImageUploadControl1$uxUpload", "Upload");
GCLogin.putViewstates(uploadParams, viewstates);
final File image = new File(imageUri.getPath());
final String response = Network.getResponseData(Network.postRequest(uri, uploadParams, "ctl00$ContentBody$ImageUploadControl1$uxFileUpload", "image/jpeg", image));
if (response == null) {
Log.e("GCParser.uploadLogIMage: didn't get response for image upload");
return ImmutablePair.of(StatusCode.LOGIMAGE_POST_ERROR, null);
}
final MatcherWrapper matcherUrl = new MatcherWrapper(GCConstants.PATTERN_IMAGE_UPLOAD_URL, response);
if (matcherUrl.find()) {
Log.i("Logimage successfully uploaded.");
final String uploadedImageUrl = matcherUrl.group(1);
return ImmutablePair.of(StatusCode.NO_ERROR, uploadedImageUrl);
}
Log.e("GCParser.uploadLogIMage: Failed to upload image because of unknown error");
return ImmutablePair.of(StatusCode.LOGIMAGE_POST_ERROR, null);
}
/**
* Post a log to GC.com.
*
* @return status code of the upload and ID of the log
*/
public static StatusCode postLogTrackable(final String tbid, final String trackingCode, final String[] viewstates,
final LogType logType, final int year, final int month, final int day, final String log) {
if (GCLogin.isEmpty(viewstates)) {
Log.e("GCParser.postLogTrackable: No viewstate given");
return StatusCode.LOG_POST_ERROR;
}
if (StringUtils.isBlank(log)) {
Log.e("GCParser.postLogTrackable: No log text given");
return StatusCode.NO_LOG_TEXT;
}
Log.i("Trying to post log for trackable #" + trackingCode + " - action: " + logType + "; date: " + year + "." + month + "." + day + ", log: " + log);
final String logInfo = log.replace("\n", "\r\n"); // windows' eol
final Calendar currentDate = Calendar.getInstance();
final Parameters params = new Parameters(
"__EVENTTARGET", "",
"__EVENTARGUMENT", "",
"__LASTFOCUS", "",
"ctl00$ContentBody$LogBookPanel1$ddLogType", Integer.toString(logType.id),
"ctl00$ContentBody$LogBookPanel1$tbCode", trackingCode);
GCLogin.putViewstates(params, viewstates);
if (currentDate.get(Calendar.YEAR) == year && (currentDate.get(Calendar.MONTH) + 1) == month && currentDate.get(Calendar.DATE) == day) {
params.put("ctl00$ContentBody$LogBookPanel1$DateTimeLogged", "");
params.put("ctl00$ContentBody$LogBookPanel1$uxDateVisited", "");
} else {
params.put("ctl00$ContentBody$LogBookPanel1$DateTimeLogged", Integer.toString(month) + "/" + Integer.toString(day) + "/" + Integer.toString(year));
params.put("ctl00$ContentBody$LogBookPanel1$uxDateVisited", GCLogin.formatGcCustomDate(year, month, day));
}
params.put(
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Day", Integer.toString(day),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Month", Integer.toString(month),
"ctl00$ContentBody$LogBookPanel1$DateTimeLogged$Year", Integer.toString(year),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Day", Integer.toString(day),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Month", Integer.toString(month),
"ctl00$ContentBody$LogBookPanel1$uxDateVisited$Year", Integer.toString(year),
"ctl00$ContentBody$LogBookPanel1$uxLogInfo", logInfo,
"ctl00$ContentBody$LogBookPanel1$btnSubmitLog", "Submit Log Entry",
"ctl00$ContentBody$uxVistOtherTrackableTB", "",
"ctl00$ContentBody$LogBookPanel1$LogButton", "Submit Log Entry",
"ctl00$ContentBody$uxVistOtherListingGC", "");
final String uri = new Uri.Builder().scheme("http").authority("www.geocaching.com").path("/track/log.aspx").encodedQuery("wid=" + tbid).build().toString();
final String page = GCLogin.getInstance().postRequestLogged(uri, params);
if (!GCLogin.getInstance().getLoginStatus(page)) {
Log.e("GCParser.postLogTrackable: Cannot log in geocaching");
return StatusCode.NOT_LOGGED_IN;
}
try {
final MatcherWrapper matcherOk = new MatcherWrapper(GCConstants.PATTERN_OK2, page);
if (matcherOk.find()) {
Log.i("Log successfully posted to trackable #" + trackingCode);
return StatusCode.NO_ERROR;
}
} catch (final Exception e) {
Log.e("GCParser.postLogTrackable.check", e);
}
Log.e("GCParser.postLogTrackable: Failed to post log because of unknown error");
return StatusCode.LOG_POST_ERROR;
}
/**
* Adds the cache to the watchlist of the user.
*
* @param cache
* the cache to add
* @return <code>false</code> if an error occurred, <code>true</code> otherwise
*/
static boolean addToWatchlist(final Geocache cache) {
final String uri = "http://www.geocaching.com/my/watchlist.aspx?w=" + cache.getCacheId();
final String page = GCLogin.getInstance().postRequestLogged(uri, null);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.addToWatchlist: No data from server");
return false; // error
}
final boolean guidOnPage = isGuidContainedInPage(cache, page);
if (guidOnPage) {
Log.i("GCParser.addToWatchlist: cache is on watchlist");
cache.setOnWatchlist(true);
} else {
Log.e("GCParser.addToWatchlist: cache is not on watchlist");
}
return guidOnPage; // on watchlist (=added) / else: error
}
/**
* Removes the cache from the watch list
*
* @param cache
* the cache to remove
* @return <code>false</code> if an error occurred, <code>true</code> otherwise
*/
static boolean removeFromWatchlist(final Geocache cache) {
final String uri = "http://www.geocaching.com/my/watchlist.aspx?ds=1&action=rem&id=" + cache.getCacheId();
String page = GCLogin.getInstance().postRequestLogged(uri, null);
if (StringUtils.isBlank(page)) {
Log.e("GCParser.removeFromWatchlist: No data from server");
return false; // error
}
// removing cache from list needs approval by hitting "Yes" button
final Parameters params = new Parameters(
"__EVENTTARGET", "",
"__EVENTARGUMENT", "",
"ctl00$ContentBody$btnYes", "Yes");
GCLogin.transferViewstates(page, params);
page = Network.getResponseData(Network.postRequest(uri, params));
final boolean guidOnPage = isGuidContainedInPage(cache, page);
if (!guidOnPage) {
Log.i("GCParser.removeFromWatchlist: cache removed from watchlist");
cache.setOnWatchlist(false);
} else {
Log.e("GCParser.removeFromWatchlist: cache not removed from watchlist");
}
return !guidOnPage; // on watch list (=error) / not on watch list
}
/**
* Checks if a page contains the guid of a cache
*
* @param cache the geocache
* @param page
* the page to search in, may be null
* @return true if the page contains the guid of the cache, false otherwise
*/
private static boolean isGuidContainedInPage(final Geocache cache, final String page) {
if (StringUtils.isBlank(page) || StringUtils.isBlank(cache.getGuid())) {
return false;
}
return Pattern.compile(cache.getGuid(), Pattern.CASE_INSENSITIVE).matcher(page).find();
}
@Nullable
static String requestHtmlPage(@Nullable final String geocode, @Nullable final String guid, final String log) {
final Parameters params = new Parameters("decrypt", "y");
if (StringUtils.isNotBlank(geocode)) {
params.put("wp", geocode);
} else if (StringUtils.isNotBlank(guid)) {
params.put("guid", guid);
}
params.put("log", log);
params.put("numlogs", "0");
return GCLogin.getInstance().getRequestLogged("http://www.geocaching.com/seek/cache_details.aspx", params);
}
/**
* Adds the cache to the favorites of the user.
*
* This must not be called from the UI thread.
*
* @param cache
* the cache to add
* @return <code>false</code> if an error occurred, <code>true</code> otherwise
*/
static boolean addToFavorites(final Geocache cache) {
return changeFavorite(cache, true);
}
private static boolean changeFavorite(final Geocache cache, final boolean add) {
final String userToken = getUserToken(cache);
if (StringUtils.isEmpty(userToken)) {
return false;
}
final String uri = "http://www.geocaching.com/datastore/favorites.svc/update?u=" + userToken + "&f=" + Boolean.toString(add);
final HttpResponse response = Network.postRequest(uri, null);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
Log.i("GCParser.changeFavorite: cache added/removed to/from favorites");
cache.setFavorite(add);
cache.setFavoritePoints(cache.getFavoritePoints() + (add ? 1 : -1));
return true;
}
Log.e("GCParser.changeFavorite: cache not added/removed to/from favorites");
return false;
}
private static String getUserToken(final Geocache cache) {
return parseUserToken(requestHtmlPage(cache.getGeocode(), null, "n"));
}
private static String parseUserToken(final String page) {
return TextUtils.getMatch(page, GCConstants.PATTERN_USERTOKEN, "");
}
/**
* Removes the cache from the favorites.
*
* This must not be called from the UI thread.
*
* @param cache
* the cache to remove
* @return <code>false</code> if an error occurred, <code>true</code> otherwise
*/
static boolean removeFromFavorites(final Geocache cache) {
return changeFavorite(cache, false);
}
/**
* Parse a trackable HTML description into a Trackable object
*
* @param page
* the HTML page to parse, already processed through {@link TextUtils#replaceWhitespace}
* @return the parsed trackable, or null if none could be parsed
*/
static Trackable parseTrackable(final String page, final String possibleTrackingcode) {
if (StringUtils.isBlank(page)) {
Log.e("GCParser.parseTrackable: No page given");
return null;
}
if (page.contains(GCConstants.ERROR_TB_DOES_NOT_EXIST) || page.contains(GCConstants.ERROR_TB_ARITHMETIC_OVERFLOW) || page.contains(GCConstants.ERROR_TB_ELEMENT_EXCEPTION)) {
return null;
}
final Trackable trackable = new Trackable();
// trackable geocode
trackable.setGeocode(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_GEOCODE, true, StringUtils.upperCase(possibleTrackingcode)));
if (trackable.getGeocode() == null) {
Log.e("GCParser.parseTrackable: could not figure out trackable geocode");
return null;
}
// trackable id
trackable.setGuid(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_GUID, true, trackable.getGuid()));
// trackable icon
trackable.setIconUrl(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_ICON, true, trackable.getIconUrl()));
// trackable name
trackable.setName(Html.fromHtml(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_NAME, true, "")).toString());
// trackable type
if (StringUtils.isNotBlank(trackable.getName())) {
trackable.setType(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_TYPE, true, trackable.getType()));
}
// trackable owner name
try {
final MatcherWrapper matcherOwner = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_OWNER, page);
if (matcherOwner.find() && matcherOwner.groupCount() > 0) {
trackable.setOwnerGuid(matcherOwner.group(1));
trackable.setOwner(matcherOwner.group(2).trim());
}
} catch (final RuntimeException e) {
// failed to parse trackable owner name
Log.w("GCParser.parseTrackable: Failed to parse trackable owner name", e);
}
// trackable origin
trackable.setOrigin(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_ORIGIN, true, trackable.getOrigin()));
// trackable spotted
try {
final MatcherWrapper matcherSpottedCache = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_SPOTTEDCACHE, page);
if (matcherSpottedCache.find() && matcherSpottedCache.groupCount() > 0) {
trackable.setSpottedGuid(matcherSpottedCache.group(1));
trackable.setSpottedName(matcherSpottedCache.group(2).trim());
trackable.setSpottedType(Trackable.SPOTTED_CACHE);
}
final MatcherWrapper matcherSpottedUser = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_SPOTTEDUSER, page);
if (matcherSpottedUser.find() && matcherSpottedUser.groupCount() > 0) {
trackable.setSpottedGuid(matcherSpottedUser.group(1));
trackable.setSpottedName(matcherSpottedUser.group(2).trim());
trackable.setSpottedType(Trackable.SPOTTED_USER);
}
if (TextUtils.matches(page, GCConstants.PATTERN_TRACKABLE_SPOTTEDUNKNOWN)) {
trackable.setSpottedType(Trackable.SPOTTED_UNKNOWN);
}
if (TextUtils.matches(page, GCConstants.PATTERN_TRACKABLE_SPOTTEDOWNER)) {
trackable.setSpottedType(Trackable.SPOTTED_OWNER);
}
} catch (final RuntimeException e) {
// failed to parse trackable last known place
Log.w("GCParser.parseTrackable: Failed to parse trackable last known place", e);
}
// released date - can be missing on the page
final String releaseString = TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_RELEASES, false, null);
if (releaseString != null) {
try {
trackable.setReleased(DATE_TB_IN_1.parse(releaseString));
} catch (final ParseException ignored) {
if (trackable.getReleased() == null) {
try {
trackable.setReleased(DATE_TB_IN_2.parse(releaseString));
} catch (final ParseException e) {
Log.e("Could not parse trackable release " + releaseString, e);
}
}
}
}
// trackable distance
final String distance = TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_DISTANCE, false, null);
if (null != distance) {
try {
trackable.setDistance(DistanceParser.parseDistance(distance,
!Settings.useImperialUnits()));
} catch (final NumberFormatException e) {
Log.e("GCParser.parseTrackable: Failed to parse distance", e);
}
}
// trackable goal
trackable.setGoal(HtmlUtils.removeExtraParagraph(convertLinks(TextUtils.getMatch(page, GCConstants.PATTERN_TRACKABLE_GOAL, true, trackable.getGoal()))));
// trackable details & image
try {
final MatcherWrapper matcherDetailsImage = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_DETAILSIMAGE, page);
if (matcherDetailsImage.find() && matcherDetailsImage.groupCount() > 0) {
final String image = StringUtils.trim(matcherDetailsImage.group(3));
final String details = StringUtils.trim(matcherDetailsImage.group(4));
if (StringUtils.isNotEmpty(image)) {
trackable.setImage(StringUtils.replace(image, "/display/", "/large/"));
}
if (StringUtils.isNotEmpty(details) && !StringUtils.equals(details, "No additional details available.")) {
trackable.setDetails(HtmlUtils.removeExtraParagraph(convertLinks(details)));
}
}
} catch (final RuntimeException e) {
// failed to parse trackable details & image
Log.w("GCParser.parseTrackable: Failed to parse trackable details & image", e);
}
if (StringUtils.isEmpty(trackable.getDetails()) && page.contains(GCConstants.ERROR_TB_NOT_ACTIVATED)) {
trackable.setDetails(CgeoApplication.getInstance().getString(R.string.trackable_not_activated));
}
// trackable logs
try {
final MatcherWrapper matcherLogs = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_LOG, page);
/*
* 1. Type (image)
* 2. Date
* 3. Author
* 4. Cache-GUID
* 5. <ignored> (strike-through property for ancient caches)
* 6. Cache-name
* 7. Log text
*/
while (matcherLogs.find()) {
long date = 0;
try {
date = GCLogin.parseGcCustomDate(matcherLogs.group(2)).getTime();
} catch (final ParseException ignored) {
}
final LogEntry logDone = new LogEntry(
Html.fromHtml(matcherLogs.group(3)).toString().trim(),
date,
LogType.getByIconName(matcherLogs.group(1)),
matcherLogs.group(7).trim());
if (matcherLogs.group(4) != null && matcherLogs.group(6) != null) {
logDone.cacheGuid = matcherLogs.group(4);
logDone.cacheName = matcherLogs.group(6);
}
// Apply the pattern for images in a trackable log entry against each full log (group(0))
final String logEntry = matcherLogs.group(0);
final MatcherWrapper matcherLogImages = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE_LOG_IMAGES, logEntry);
/*
* 1. Image URL
* 2. Image title
*/
while (matcherLogImages.find()) {
final Image logImage = new Image(matcherLogImages.group(1), matcherLogImages.group(2));
logDone.addLogImage(logImage);
}
trackable.getLogs().add(logDone);
}
} catch (final Exception e) {
// failed to parse logs
Log.w("GCParser.parseCache: Failed to parse cache logs", e);
}
// tracking code
if (!StringUtils.equalsIgnoreCase(trackable.getGeocode(), possibleTrackingcode)) {
trackable.setTrackingcode(possibleTrackingcode);
}
if (CgeoApplication.getInstance() != null) {
DataStore.saveTrackable(trackable);
}
return trackable;
}
private static String convertLinks(final String input) {
if (input == null) {
return null;
}
return StringUtils.replace(input, "../", GCConstants.GC_URL);
}
private enum Logs {
ALL(null),
FRIENDS("sf"),
OWN("sp");
final String paramName;
Logs(final String paramName) {
this.paramName = paramName;
}
private String getParamName() {
return this.paramName;
}
}
/**
* Extract special logs (friends, own) through seperate request.
*
* @param userToken the user token extracted from the web page
* @param logType the logType to request
* @return Observable<LogEntry> The logs
*/
private static Observable<LogEntry> getLogs(final String userToken, final Logs logType) {
if (userToken.isEmpty()) {
Log.e("GCParser.loadLogsFromDetails: unable to extract userToken");
return Observable.empty();
}
return Observable.defer(new Func0<Observable<LogEntry>>() {
@Override
public Observable<LogEntry> call() {
final Parameters params = new Parameters(
"tkn", userToken,
"idx", "1",
"num", String.valueOf(GCConstants.NUMBER_OF_LOGS),
"decrypt", "true");
if (logType != Logs.ALL) {
params.add(logType.getParamName(), Boolean.toString(Boolean.TRUE));
}
final HttpResponse response = Network.getRequest("http://www.geocaching.com/seek/geocache.logbook", params);
if (response == null) {
Log.e("GCParser.loadLogsFromDetails: cannot log logs, response is null");
return Observable.empty();
}
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
Log.e("GCParser.loadLogsFromDetails: error " + statusCode + " when requesting log information");
return Observable.empty();
}
final InputStream responseStream = Network.getResponseStream(response);
if (responseStream == null) {
Log.e("GCParser.loadLogsFromDetails: unable to read whole response");
return Observable.empty();
}
return parseLogs(logType != Logs.ALL, responseStream);
}
}).subscribeOn(RxUtils.networkScheduler);
}
private static Observable<LogEntry> parseLogs(final boolean markAsFriendsLog, final InputStream responseStream) {
return Observable.create(new OnSubscribe<LogEntry>() {
@Override
public void call(final Subscriber<? super LogEntry> subscriber) {
try {
final ObjectNode resp = (ObjectNode) JsonUtils.reader.readTree(responseStream);
if (!resp.path("status").asText().equals("success")) {
Log.e("GCParser.loadLogsFromDetails: status is " + resp.path("status").asText("[absent]"));
subscriber.onCompleted();
return;
}
final ArrayNode data = (ArrayNode) resp.get("data");
for (final JsonNode entry: data) {
// FIXME: use the "LogType" field instead of the "LogTypeImage" one.
final String logIconNameExt = entry.path("LogTypeImage").asText(".gif");
final String logIconName = logIconNameExt.substring(0, logIconNameExt.length() - 4);
final long date;
try {
date = GCLogin.parseGcCustomDate(entry.get("Visited").asText()).getTime();
} catch (ParseException | NullPointerException e) {
Log.e("GCParser.loadLogsFromDetails: failed to parse log date", e);
continue;
}
// TODO: we should update our log data structure to be able to record
// proper coordinates, and make them clickable. In the meantime, it is
// better to integrate those coordinates into the text rather than not
// display them at all.
final String latLon = entry.path("LatLonString").asText();
final String logText = (StringUtils.isEmpty(latLon) ? "" : (latLon + "<br/><br/>")) + TextUtils.removeControlCharacters(entry.path("LogText").asText());
final LogEntry logDone = new LogEntry(
TextUtils.removeControlCharacters(entry.path("UserName").asText()),
date,
LogType.getByIconName(logIconName),
logText);
logDone.found = entry.path("GeocacheFindCount").asInt();
logDone.friend = markAsFriendsLog;
final ArrayNode images = (ArrayNode) entry.get("Images");
for (final JsonNode image: images) {
final String url = "http://imgcdn.geocaching.com/cache/log/large/" + image.path("FileName").asText();
final String title = TextUtils.removeControlCharacters(image.path("Name").asText());
final Image logImage = new Image(url, title);
logDone.addLogImage(logImage);
}
subscriber.onNext(logDone);
}
} catch (final IOException e) {
Log.w("GCParser.loadLogsFromDetails: Failed to parse cache logs", e);
}
subscriber.onCompleted();
}
});
}
@NonNull
public static List<LogType> parseTypes(final String page) {
if (StringUtils.isEmpty(page)) {
return Collections.emptyList();
}
final List<LogType> types = new ArrayList<>();
final MatcherWrapper typeBoxMatcher = new MatcherWrapper(GCConstants.PATTERN_TYPEBOX, page);
if (typeBoxMatcher.find() && typeBoxMatcher.groupCount() > 0) {
final String typesText = typeBoxMatcher.group(1);
final MatcherWrapper typeMatcher = new MatcherWrapper(GCConstants.PATTERN_TYPE2, typesText);
while (typeMatcher.find()) {
if (typeMatcher.groupCount() > 1) {
try {
final int type = Integer.parseInt(typeMatcher.group(2));
if (type > 0) {
types.add(LogType.getById(type));
}
} catch (final NumberFormatException e) {
Log.e("Error parsing log types", e);
}
}
}
}
// we don't support this log type
types.remove(LogType.UPDATE_COORDINATES);
return types;
}
@NonNull
public static List<TrackableLog> parseTrackableLog(final String page) {
if (StringUtils.isEmpty(page)) {
return Collections.emptyList();
}
String table = StringUtils.substringBetween(page, "<table id=\"tblTravelBugs\"", "</table>");
// if no trackables are currently in the account, the table is not available, so return an empty list instead of null
if (StringUtils.isBlank(table)) {
return Collections.emptyList();
}
table = StringUtils.substringBetween(table, "<tbody>", "</tbody>");
if (StringUtils.isBlank(table)) {
Log.e("GCParser.parseTrackableLog: tbody not found on page");
return Collections.emptyList();
}
final List<TrackableLog> trackableLogs = new ArrayList<>();
final MatcherWrapper trackableMatcher = new MatcherWrapper(GCConstants.PATTERN_TRACKABLE, page);
while (trackableMatcher.find()) {
if (trackableMatcher.groupCount() > 0) {
final String trackCode = trackableMatcher.group(1);
final String name = Html.fromHtml(trackableMatcher.group(2)).toString();
try {
final Integer ctl = Integer.valueOf(trackableMatcher.group(3));
final Integer id = Integer.valueOf(trackableMatcher.group(5));
if (trackCode != null && ctl != null && id != null) {
final TrackableLog entry = new TrackableLog(trackCode, name, id, ctl);
Log.i("Trackable in inventory (#" + entry.ctl + "/" + entry.id + "): " + entry.trackCode + " - " + entry.name);
trackableLogs.add(entry);
}
} catch (final NumberFormatException e) {
Log.e("GCParser.parseTrackableLog", e);
}
}
}
return trackableLogs;
}
/**
* Insert the right cache type restriction in parameters
*
* @param params
* the parameters to insert the restriction into
* @param cacheType
* the type of cache, or null to include everything
*/
static private void insertCacheType(final Parameters params, final CacheType cacheType) {
params.put("tx", cacheType.guid);
}
private static void getExtraOnlineInfo(final Geocache cache, final String page, final CancellableHandler handler) {
// This method starts the page parsing for logs in the background, as well as retrieve the friends and own logs
// if requested. It merges them and stores them in the background, while the rating is retrieved if needed and
// stored. Then we wait for the log merging and saving to be completed before returning.
if (CancellableHandler.isCancelled(handler)) {
return;
}
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_logs);
final String userToken = parseUserToken(page);
final Observable<LogEntry> logs = getLogs(userToken, Logs.ALL);
final Observable<LogEntry> ownLogs = getLogs(userToken, Logs.OWN).cache();
final Observable<LogEntry> specialLogs = Settings.isFriendLogsWanted() ?
Observable.merge(getLogs(userToken, Logs.FRIENDS), ownLogs) : Observable.<LogEntry>empty();
final Observable<List<LogEntry>> mergedLogs = Observable.zip(logs.toList(), specialLogs.toList(),
new Func2<List<LogEntry>, List<LogEntry>, List<LogEntry>>() {
@Override
public List<LogEntry> call(final List<LogEntry> logEntries, final List<LogEntry> specialLogEntries) {
mergeFriendsLogs(logEntries, specialLogEntries);
return logEntries;
}
}).cache();
mergedLogs.subscribe(new Action1<List<LogEntry>>() {
@Override
public void call(final List<LogEntry> logEntries) {
DataStore.saveLogs(cache.getGeocode(), logEntries);
}
});
if (cache.isFound() && cache.getVisitedDate() == 0) {
ownLogs.subscribe(new Action1<LogEntry>() {
@Override
public void call(final LogEntry logEntry) {
if (logEntry.type == LogType.FOUND_IT) {
cache.setVisitedDate(logEntry.date);
}
}
});
}
if (Settings.isRatingWanted() && !CancellableHandler.isCancelled(handler)) {
CancellableHandler.sendLoadProgressDetail(handler, R.string.cache_dialog_loading_details_status_gcvote);
final GCVoteRating rating = GCVote.getRating(cache.getGuid(), cache.getGeocode());
if (rating != null) {
cache.setRating(rating.getRating());
cache.setVotes(rating.getVotes());
cache.setMyVote(rating.getMyVote());
}
}
// Wait for completion of logs parsing, retrieving and merging
RxUtils.waitForCompletion(mergedLogs);
}
/**
* Merge log entries and mark them as friends logs (personal and friends) to identify
* them on friends/personal logs tab.
*
* @param mergedLogs
* the list to merge logs with
* @param logsToMerge
* the list of logs to merge
*/
private static void mergeFriendsLogs(final List<LogEntry> mergedLogs, final Iterable<LogEntry> logsToMerge) {
for (final LogEntry log : logsToMerge) {
if (mergedLogs.contains(log)) {
mergedLogs.get(mergedLogs.indexOf(log)).friend = true;
} else {
mergedLogs.add(log);
}
}
}
public static boolean uploadModifiedCoordinates(final Geocache cache, final Geopoint wpt) {
return editModifiedCoordinates(cache, wpt);
}
public static boolean deleteModifiedCoordinates(final Geocache cache) {
return editModifiedCoordinates(cache, null);
}
public static boolean editModifiedCoordinates(final Geocache cache, final Geopoint wpt) {
final String userToken = getUserToken(cache);
if (StringUtils.isEmpty(userToken)) {
return false;
}
final ObjectNode jo = new ObjectNode(JsonUtils.factory);
final ObjectNode dto = jo.putObject("dto").put("ut", userToken);
if (wpt != null) {
dto.putObject("data").put("lat", wpt.getLatitudeE6() / 1E6).put("lng", wpt.getLongitudeE6() / 1E6);
}
final String uriSuffix = wpt != null ? "SetUserCoordinate" : "ResetUserCoordinate";
final String uriPrefix = "http://www.geocaching.com/seek/cache_details.aspx/";
final HttpResponse response = Network.postJsonRequest(uriPrefix + uriSuffix, jo);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
Log.i("GCParser.editModifiedCoordinates - edited on GC.com");
return true;
}
Log.e("GCParser.deleteModifiedCoordinates - cannot delete modified coords");
return false;
}
public static boolean uploadPersonalNote(final Geocache cache) {
final String userToken = getUserToken(cache);
if (StringUtils.isEmpty(userToken)) {
return false;
}
final ObjectNode jo = new ObjectNode(JsonUtils.factory);
jo.putObject("dto").put("et", StringUtils.defaultString(cache.getPersonalNote())).put("ut", userToken);
final String uriSuffix = "SetUserCacheNote";
final String uriPrefix = "http://www.geocaching.com/seek/cache_details.aspx/";
final HttpResponse response = Network.postJsonRequest(uriPrefix + uriSuffix, jo);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
Log.i("GCParser.uploadPersonalNote - uploaded to GC.com");
return true;
}
Log.e("GCParser.uploadPersonalNote - cannot upload personal note");
return false;
}
}
|