ChatReader.java

This Java application shows the output of a "chat room", using the Python script chat.py.

Run this Java application in a console window, then use profile.py to update the list of comments.


01: import java.net.*;
02: import java.io.*;
03: import java.awt.event.ActionEvent;
04: import java.awt.event.ActionListener;
05: import javax.swing.Timer;
06: 
07: public class ChatReader {
08:     private Timer myTimer;
09:     String since;
10:     public void start() {
11:         myTimer = new Timer(2000,new TimerHandler() );
12:         since = showChat(null);
13:         myTimer.start();
14:     }
15:     private class TimerHandler implements ActionListener
16:     {
17:         public void actionPerformed(ActionEvent actionevent)
18:         {
19:             //System.out.println("timer triggered");
20:             String result = showChat(since);
21:             if (result!=null) since = result;
22:         }
23:     }
24:     public static String showChat(String since) {
25:         URL url;
26:         String name;
27:         int n, nlines;
28:         name = "https://johnloomis.org/python/db/chat.py";
29:         try {
30:                 if (since !=null) name = name + "?since=" + URLEncoder.encode(since,"UTF-8");
31:         }
32:         catch(UnsupportedEncodingException e) {
33:                 System.out.println(e);
34:         }
35:         char quote = '"';
36: 
37:         String inp, modified, nickname, comment;
38:         modified = null;
39:         try {
40:             url = new URL(name);
41:             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
42:             boolean done = false;
43:             while (!done) {
44:                 inp = in.readLine();
45:                 if (inp==null) break;
46:                 modified = inp.substring(1,20);
47:                 inp = inp.substring(21);
48:                 int n1, n2;
49:                 n1 = inp.indexOf(quote);
50:                 n2 = inp.substring(n1+1).indexOf(quote);
51:                 nickname = inp.substring(n1+1,n1+n2+1);
52:                 inp = inp.substring(n1+n2+2);
53:                 n1 = inp.indexOf(quote);
54:                 n2 = inp.substring(n1+1).indexOf(quote);
55:                 comment = inp.substring(n1+1,n1+n2+1);
56:                 /*
57:                 if (n1>=0) {
58:                     n2 = inp.substring(n1).indexOf(quote);
59:                     if (n2>=0) nickname = inp.substring(n1,n2);
60:                     else nickname = "";
61:                 }
62:                 else nickname = "";
63:                 if (n2>0) inp = inp.substring(n2);
64:                 */
65:                 System.out.printf("%s [%8s] %s\n",modified,nickname,comment);
66:             }
67:             in.close();     
68:         }
69:         catch (MalformedURLException e) {
70:             System.out.println(e);
71:         }
72:         catch (IOException e) {
73:             System.out.println(e);
74:         }
75:         return modified;
76:     }
77:     public static void main(String[] args) {
78:         ChatReader chat = new ChatReader();
79:         chat.since = "2008-03-03 15:02:08";
80:         chat.start();
81:         while (true);
82:     }
83: }


Maintained by John Loomis, updated Tue Mar 04 23:01:13 2008