1
22
23 package com.liferay.portlet.reverendfun.util;
24
25 import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26 import com.liferay.portal.kernel.util.StringComparator;
27 import com.liferay.portal.util.WebCacheable;
28 import com.liferay.util.ConverterException;
29 import com.liferay.util.Http;
30 import com.liferay.util.Time;
31
32 import java.text.DateFormat;
33 import java.text.SimpleDateFormat;
34
35 import java.util.Calendar;
36 import java.util.Set;
37 import java.util.TreeSet;
38
39
45 public class ReverendFunConverter implements WebCacheable {
46
47 public ReverendFunConverter(String date) {
48 _date = date;
49 }
50
51 public Object convert(String id) throws ConverterException {
52 Set dates = new TreeSet(new StringComparator(false, true));
53
54 try {
55 DateFormat dateFormatYMD = new SimpleDateFormat("yyyyMMdd");
56 DateFormat dateFormatYM = new SimpleDateFormat("yyyyMM");
57
58 Calendar cal = CalendarFactoryUtil.getCalendar();
59
60 cal.setTime(dateFormatYMD.parse(_date));
61 cal.set(Calendar.DATE, 1);
62
63 Calendar now = CalendarFactoryUtil.getCalendar();
64
65 String url = "http://www.reverendfun.com/artchives/?search=";
66
67 while (cal.before(now)) {
68 String text = Http.URLtoString(
69 url + dateFormatYM.format(cal.getTime()));
70
71 int x = text.indexOf("date=");
72 int y = text.indexOf("\"", x);
73
74 while (x != -1 && y != -1) {
75 String fromDateString = text.substring(x + 5, y);
76
77 dates.add(fromDateString);
78
79 x = text.indexOf("date=", y);
80 y = text.indexOf("\"", x);
81 }
82
83 cal.add(Calendar.MONTH, 1);
84 }
85 }
86 catch (Exception e) {
87 throw new ConverterException(_date + " " + e.toString());
88 }
89
90 return dates;
91 }
92
93 public long getRefreshTime() {
94 return _REFRESH_TIME;
95 }
96
97 private static final long _REFRESH_TIME = Time.DAY;
98
99 private String _date;
100
101 }