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
69
71 text = HtmlUtil.stripBetween(text, "span");
72
73
75 text = HtmlUtil.stripBetween(text, "sup");
76
77
79 text = HtmlUtil.stripBetween(text, "h4");
80
81
83 text = HtmlUtil.stripBetween(text, "h5");
84
85
87 text = HtmlUtil.stripHtml(text).trim();
88
89
91 text = StringUtil.replace(text, " ", "");
92
93
95 text = StringUtil.replace(text, "\n", "");
96
97
99 while (text.indexOf(" ") != -1) {
100 text = StringUtil.replace(text, " ", " ");
101 }
102
103
105 text = StringUtil.replace(text, "\"", """);
106
107
109 text = text.trim();
110
111 verse = new Verse(_location, text);
112 }
113 catch (Exception e) {
114 throw new WebCacheException(
115 _location + " " + _versionId + " " + e.toString());
116 }
117
118 return verse;
119 }
120
121 public long getRefreshTime() {
122 return _REFRESH_TIME;
123 }
124
125 private static final long _REFRESH_TIME = Time.WEEK * 52;
126
127 private String _location;
128 private String _versionId;
129
130 }