1 18 19 package org.apache.roller.ui.core.tasks; 20 21 import java.io.IOException ; 22 import java.util.Calendar ; 23 import java.util.Date ; 24 import java.util.Iterator ; 25 import java.util.TimerTask ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.roller.RollerException; 29 import org.apache.roller.config.RollerConfig; 30 import org.apache.roller.model.PlanetManager; 31 import org.apache.roller.model.Roller; 32 import org.apache.roller.model.RollerFactory; 33 import org.apache.roller.model.ScheduledTask; 34 import org.apache.roller.model.UserManager; 35 import org.apache.roller.pojos.PlanetConfigData; 36 import org.apache.roller.pojos.PlanetSubscriptionData; 37 import org.apache.roller.util.Technorati; 38 39 40 43 public class TechnoratiRankingsTask extends TimerTask implements ScheduledTask { 44 45 private static Log log = LogFactory.getLog(TechnoratiRankingsTask.class); 46 47 48 51 public static void main(String [] args) { 52 try { 53 RollerFactory.setRoller( 54 "org.apache.roller.business.hibernate.HibernateRollerImpl"); 55 TechnoratiRankingsTask task = new TechnoratiRankingsTask(); 56 task.init(RollerFactory.getRoller(), "dummy"); 57 task.run(); 58 System.exit(0); 59 } catch (Throwable t) { 60 t.printStackTrace(); 61 System.exit(-1); 62 } 63 } 64 65 66 public void init(Roller roller, String realPath) throws RollerException { 67 } 69 70 71 public void run() { 72 rankSubscriptions(); 73 } 74 75 76 79 private void rankSubscriptions() { 80 81 int count = 0; 82 int errorCount = 0; 83 try { 84 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 85 PlanetConfigData config = planet.getConfiguration(); 86 Technorati technorati = null; 87 try { 88 if (config.getProxyHost()!=null && config.getProxyPort() != -1) { 89 technorati = new Technorati( 90 config.getProxyHost(), config.getProxyPort()); 91 } else { 92 technorati = new Technorati(); 93 } 94 } catch (IOException e) { 95 log.error("Aborting collection of Technorati rankings.\n" 96 +"technorati.license not found at root of classpath.\n" 97 +"Get license at http://technorati.com/developers/apikey.html\n" 98 +"Put the license string in a file called technorati.license.\n" 99 +"And place that file at the root of Roller's classpath.\n" 100 +"For example, in the /WEB-INF/classes directory."); 101 return; 102 } 103 104 UserManager userManager = RollerFactory.getRoller().getUserManager(); 105 try { 106 int limit = RollerConfig.getIntProperty( 107 "planet.aggregator.technorati.limit", 500); 108 int userCount = planet.getSubscriptionCount(); 109 int mod = (userCount / limit) + 1; 110 111 Calendar cal = Calendar.getInstance(); 112 cal.setTime(new Date ()); 113 int day = cal.get(Calendar.DAY_OF_YEAR); 114 115 int start = (day % mod) * limit; 116 int end = start + limit; 117 end = end > userCount ? userCount : end; 118 log.info("Updating subscriptions ["+start+":"+end+"]"); 119 120 Iterator subs = planet.getAllSubscriptions(); 121 while (subs.hasNext()) { 122 PlanetSubscriptionData sub = 123 (PlanetSubscriptionData)subs.next(); 124 if (count >= start && count < end) { 125 try { 126 Technorati.Result result = 127 technorati.getBloginfo(sub.getSiteURL()); 128 if (result != null && result.getWeblog() != null) { 129 sub.setInboundblogs( 130 result.getWeblog().getInboundblogs()); 131 sub.setInboundlinks( 132 result.getWeblog().getInboundlinks()); 133 log.debug("Adding rank for " 134 +sub.getFeedURL()+" ["+count+"|" 135 +sub.getInboundblogs()+"|" 136 +sub.getInboundlinks()+"]"); 137 } else { 138 log.debug( 139 "No ranking available for " 140 +sub.getFeedURL()+" ["+count+"]"); 141 sub.setInboundlinks(0); 142 sub.setInboundblogs(0); 143 } 144 planet.saveSubscription(sub); 145 } catch (Exception e) { 146 log.warn("WARN ranking subscription [" 147 + count + "]: " + e.getMessage()); 148 if (errorCount++ > 5) { 149 log.warn( 150 " Stopping ranking, too many errors"); 151 break; 152 } 153 } 154 } 155 count++; 156 } 157 158 RollerFactory.getRoller().flush(); 160 161 } finally { 162 RollerFactory.getRoller().release(); 163 } 164 165 } catch (Exception e) { 166 log.error("ERROR ranking subscriptions", e); 167 } 168 } 169 170 } 171 | Popular Tags |