1
22
23 package com.liferay.portlet.randombibleverse.util;
24
25 import com.liferay.portal.kernel.util.HtmlUtil;
26 import com.liferay.portal.kernel.util.HttpUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Time;
29 import com.liferay.portal.kernel.webcache.WebCacheException;
30 import com.liferay.portal.kernel.webcache.WebCacheItem;
31 import com.liferay.portlet.randombibleverse.model.Verse;
32
33
39 public class VerseWebCacheItem implements WebCacheItem {
40
41 public VerseWebCacheItem(String location, String versionId) {
42 _location = location;
43 _versionId = versionId;
44 }
45
46 public Object convert(String key) throws WebCacheException {
47 Verse verse = null;
48
49 try {
50 String url =
51 "http://www.biblegateway.com/passage/?search=" +
52 HttpUtil.encodeURL(_location) + "&version=" + _versionId;
53
54 String text = HttpUtil.URLtoString(url);
55
56 int x = text.indexOf("result-text-style");
57 x = text.indexOf(">", x);
58
59 int y = text.indexOf("</div>", x);
60
61 text = text.substring(x + 1, y);
62
63 y = text.indexOf("Footnotes:");
64
65 if (y != -1) {
66 text = text.substring(0, y);
67 }
68 else {
69 y = text.indexOf("Cross references:");
70
71 if (y != -1) {
72 text = text.substring(0, y);
73 }
74 }
75
76
78 text = HtmlUtil.stripBetween(text, "span");
79
80
82 text = HtmlUtil.stripBetween(text, "sup");
83
84
86 text = HtmlUtil.stripBetween(text, "h4");
87
88
90 text = HtmlUtil.stripBetween(text, "h5");
91
92
94 text = HtmlUtil.stripHtml(text).trim();
95
96
98 text = StringUtil.replace(text, " ", "");
99
100
102 text = StringUtil.replace(text, "\n", "");
103
104
106 while (text.indexOf(" ") != -1) {
107 text = StringUtil.replace(text, " ", " ");
108 }
109
110
112 text = StringUtil.replace(text, "\"", """);
113
114
116 text = text.trim();
117
118 verse = new Verse(_location, text);
119 }
120 catch (Exception e) {
121 throw new WebCacheException(
122 _location + " " + _versionId + " " + e.toString());
123 }
124
125 return verse;
126 }
127
128 public long getRefreshTime() {
129 return _REFRESH_TIME;
130 }
131
132 private static final long _REFRESH_TIME = Time.WEEK * 52;
133
134 private String _location;
135 private String _versionId;
136
137 }