// Slashdot Your Printer
// Sends an rss feed to the lcd on your printer
package com.mike;
import java.net.*;
import java.io.*;
import java.util.*;
/**
*
*/
public class PrinterNews {
static String rss=/*"http://slashdot.org/index.rss";//*/"http://rss.news.yahoo.com/rss/topstories";
static String printer="";
static int updateFrequency=30*1000*60;
static int scrollDelay=300;//ms
static String msg="";
static String blank=" ";
static OutputStream out;
static ArrayList headlines;
static boolean nice=true;//otherwise, it locks the printer
public static void main(String[] args){
try{
if(!init(args))
return;
readHeadlines();
connect();
long startTime=new Date().getTime();
while(true){
scroll();
if(new Date().getTime()-startTime>updateFrequency){
readHeadlines();
startTime=new Date().getTime();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public static boolean init(String[] args){
if(args.length < 2){
usage();
return false;
}
for(int i=0; i<args.length; i=i+2){
System.out.println("param "+i+" ="+args[i]);
String parm=args[i];
if(!parm.startsWith("-") || parm.length()!=2){
usage();
return false;
}
switch(parm.charAt(1)){
case 'p':
printer=args[i+1];
System.out.println("set printer to "+printer);
break;
case 'u':
rss=args[i+1];
System.out.println("set rss to "+rss);
break;
case 'f':
try{
updateFrequency=Integer.parseInt(args[i+1])*1000*60;
System.out.println("set frequency to "+updateFrequency);
}catch(Exception e){
usage();
return false;
}
case 'd':
try{
scrollDelay=Integer.parseInt(args[i+1]);
System.out.println("set delay to "+scrollDelay);
break;
}catch(Exception e){
usage();
return false;
}
default:
usage();
return false;
}
}
return true;
}
public static void usage(){
System.out.println("\nUsage:\n");
System.out.println("java com.mike.PrinterNews -p printer");
System.out.println(" [-u rss_url] url of rss feed");
System.out.println(" [-f rssUpdateFrequency] minutes between rss refresh");
System.out.println(" [-d scrollDelayMS] ms between lcd update");
System.out.println("defaults to -u http://rss.news.yahoo.com/rss/topstories -f 30 -d 300");
System.out.println("NOTE: slashdot will blacklist you for -f<30 !\n");
}
public static void connect() throws Exception{
Socket s = new Socket(printer, 9100);
out =s.getOutputStream();
}
public static void scroll() throws Exception{
for(Iterator it=headlines.iterator(); it.hasNext();){
sendMsg((String)(it.next()));
if(nice){
out.flush();
out.close();
Thread.sleep(1000);
connect();
}
}
}
public static void sendMsg(String msg) throws Exception{
msg=blank+msg+blank;
//OPMSG, RDYMSG, STMSG
for(int i=0; i<msg.length(); i++){
byte[] b;
if(msg.length()> i +15){
b=msg.substring(i, i+15).getBytes();
}else{
b=msg.substring(i).getBytes();
}
out.write("\033%-12345X@PJL RDYMSG DISPLAY = \"".getBytes());
out.write(b);
out.write("\"\r\n\033%-12345X\r\n".getBytes());
Thread.sleep(scrollDelay);
}
}
public static void readHeadlines() throws Exception{
headlines= new ArrayList();
BufferedReader i = new BufferedReader(new InputStreamReader(new URL(rss).openStream()));
while(true){
String nextLine=i.readLine();
if(nextLine==null)
break;
int start=nextLine.toLowerCase().indexOf("<description>");//<title>
if(start!=-1)
headlines.add(scrub(nextLine.substring(start+13, nextLine.indexOf("</")>start+13?nextLine.indexOf("</"):nextLine.length()))); //+7
}
}
private static String scrub(String s) throws Exception{
int start=s.indexOf("&");
int end=s.indexOf(";",start>0?start:0);
if(start!= -1 && end != -1)
return scrub(s.substring(0, start) +s.substring(end+1));
else
return s;
}
}
No comments:
Post a Comment