001: package org.methodize.nntprss.rss.publish;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import java.io.BufferedReader;
034: import java.io.InputStreamReader;
035: import java.util.Calendar;
036: import java.util.HashMap;
037: import java.util.Hashtable;
038: import java.util.Map;
039: import java.util.Vector;
040:
041: import org.apache.xmlrpc.XmlRpcClient;
042: import org.methodize.nntprss.rss.Item;
043: import org.methodize.nntprss.util.AppConstants;
044:
045: /**
046: * @author Jason Brome <jason@methodize.org>
047: * @version $Id: LiveJournalPublisher.java,v 1.2 2003/03/22 16:34:17 jasonbrome Exp $
048: */
049:
050: public class LiveJournalPublisher implements Publisher {
051:
052: private static final String METHOD_POSTEVENT = "LJ.XMLRPC.postevent";
053:
054: private static final String METHOD_LOGIN = "LJ.XMLRPC.login";
055:
056: private static final String STRUCT_USERNAME = "username";
057:
058: private static final String STRUCT_PASSWORD = "password";
059:
060: private static final String STRUCT_VER = "ver";
061:
062: private static final String STRUCT_EVENT = "event";
063:
064: private static final String STRUCT_LINEENDINGS = "lineendings";
065:
066: private static final String STRUCT_SUBJECT = "subject";
067:
068: private static final String STRUCT_YEAR = "year";
069:
070: private static final String STRUCT_MON = "mon";
071:
072: private static final String STRUCT_DAY = "day";
073:
074: private static final String STRUCT_HOUR = "hour";
075:
076: private static final String STRUCT_MIN = "min";
077:
078: private static final String STRUCT_CLIENTVERSION = "clientversion";
079:
080: /**
081: * @see org.methodize.nntprss.rss.publish.Publisher#publish(Map, String)
082: */
083: public void publish(Map profile, Item content)
084: throws PublisherException {
085:
086: // LJ.XMLRPC.postevent
087: //
088: try {
089: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
090: .get(PROP_PUBLISHER_URL));
091: Vector params = new Vector();
092:
093: Map struct = new Hashtable();
094:
095: // username (string): Login for a Blogger user who has permission to post to the blog.
096: struct.put(STRUCT_USERNAME, profile.get(PROP_USERNAME));
097: // password (string): Password for said username.
098: struct.put(STRUCT_PASSWORD, profile.get(PROP_PASSWORD));
099: // LiveJournal API version
100: struct.put(STRUCT_VER, "1");
101: // Event - content of the post
102: struct.put(STRUCT_EVENT, content.getDescription());
103: // Line Endings - set to PC (default \r\n)
104: struct.put(STRUCT_LINEENDINGS, "pc");
105: // Subject / Title
106: struct.put(STRUCT_SUBJECT, content.getTitle());
107:
108: Calendar cal = Calendar.getInstance();
109: cal.setTimeInMillis(System.currentTimeMillis());
110:
111: struct
112: .put(STRUCT_YEAR, new Integer(cal
113: .get(Calendar.YEAR)));
114: struct.put(STRUCT_MON, new Integer(
115: cal.get(Calendar.MONTH) + 1));
116: struct.put(STRUCT_DAY, new Integer(cal
117: .get(Calendar.DAY_OF_MONTH)));
118: struct.put(STRUCT_HOUR, new Integer(cal
119: .get(Calendar.HOUR_OF_DAY)));
120: struct.put(STRUCT_MIN,
121: new Integer(cal.get(Calendar.MINUTE)));
122:
123: params.add(struct);
124:
125: Map response = (Map) xmlrpc.execute(METHOD_POSTEVENT,
126: params);
127:
128: // Discard response
129: // anum - The key number used to calculate the public itemid ID number for URLs.
130: // itemid - The unique number the server assigned to the post.
131:
132: } catch (Exception e) {
133: throw new PublisherException(e);
134: }
135: }
136:
137: /**
138: * @see org.methodize.nntprss.rss.publish.Publisher#validate(Map)
139: */
140: public void validate(Map profile) throws PublisherException {
141: // LJ.XMLRPC.login takes the following parameters.
142: //
143: try {
144: XmlRpcClient xmlrpc = new XmlRpcClient((String) profile
145: .get(PROP_PUBLISHER_URL));
146: Vector params = new Vector();
147:
148: Map struct = new Hashtable();
149:
150: // username (string): Login for a Blogger user who has permission to post to the blog.
151: struct.put(STRUCT_USERNAME, profile.get(PROP_USERNAME));
152: // password (string): Password for said username.
153: struct.put(STRUCT_PASSWORD, profile.get(PROP_PASSWORD));
154: // LiveJournal API version
155: struct.put(STRUCT_VER, "1");
156:
157: struct.put(STRUCT_CLIENTVERSION, "Java-nntprss/"
158: + AppConstants.VERSION);
159:
160: params.add(struct);
161:
162: Map userInfo = (Map) xmlrpc.execute(METHOD_LOGIN, params);
163:
164: //TODO: think about message returned in userInfo
165:
166: } catch (Exception e) {
167: throw new PublisherException(e);
168: }
169:
170: }
171:
172: public static void main(String args[]) {
173: // Tester...
174: LiveJournalPublisher pub = new LiveJournalPublisher();
175:
176: Map profile = new HashMap();
177:
178: try {
179: BufferedReader reader = new BufferedReader(
180: new InputStreamReader(System.in));
181:
182: System.out.println("Username?");
183: String userName = reader.readLine();
184:
185: System.out.println("Password?");
186: String password = reader.readLine();
187:
188: System.out.println("Title?");
189: String title = reader.readLine();
190:
191: System.out.println("Content?");
192: String content = reader.readLine();
193:
194: profile.put(PROP_PASSWORD, password);
195: profile.put(PROP_USERNAME, userName);
196:
197: profile.put(Publisher.PROP_PUBLISHER_URL,
198: "http://www.livejournal.com/interface/xmlrpc");
199:
200: pub.validate(profile);
201:
202: Item newItem = new Item();
203: newItem.setDescription(content);
204: newItem.setTitle(title);
205:
206: pub.publish(profile, newItem);
207: } catch (Exception e) {
208: e.printStackTrace();
209: }
210:
211: }
212:
213: }
|