[bknr-cvs] hans changed trunk/projects/planetwit/
BKNR Commits
bknr at bknr.net
Wed Aug 19 17:36:34 UTC 2009
Revision: 4440
Author: hans
URL: http://bknr.net/trac/changeset/4440
update twitter status once for every posting, include tiny url
U trunk/projects/planetwit/http-client.clj
U trunk/projects/planetwit/planetwit.clj
Modified: trunk/projects/planetwit/http-client.clj
===================================================================
--- trunk/projects/planetwit/http-client.clj 2009-07-22 18:21:41 UTC (rev 4439)
+++ trunk/projects/planetwit/http-client.clj 2009-08-19 17:36:33 UTC (rev 4440)
@@ -31,5 +31,5 @@
(String. (. (Base64.) (encode (. basic-authorization getBytes))))))))
(let [response (. client call request)]
{:status (. response getStatus)
- :content-type (. response getContentType)
+ :content-type (. response getHeader "Content-Type")
:body (. (. response getBlockingBody) readString)})))
\ No newline at end of file
Modified: trunk/projects/planetwit/planetwit.clj
===================================================================
--- trunk/projects/planetwit/planetwit.clj 2009-07-22 18:21:41 UTC (rev 4439)
+++ trunk/projects/planetwit/planetwit.clj 2009-08-19 17:36:33 UTC (rev 4440)
@@ -29,39 +29,42 @@
(defn feed-to-zip [url]
(zip/xml-zip (xml/parse url)))
-(defn update-twitter-status [auth-file status]
- (http/simple-http-request +twitter-url+
- {:method :post
- :basic-authorization (read-file auth-file)
- :content-type "application/x-www-form-urlencoded"
- :content (format "status=%s&source=planetlisp" (java.net.URLEncoder/encode status))}))
+(defn update-twitter-status [status]
+ (try
+ (http/simple-http-request +twitter-url+
+ {:method :post
+ :basic-authorization (read-file +twitter-auth-file+)
+ :content-type "application/x-www-form-urlencoded"
+ :content (format "status=%s&source=planetlisp" (java.net.URLEncoder/encode status))})
+ (catch java.io.FileNotFoundException _
+ (println (format "could not update twitter status (no auth file):\n%s\n" status)))))
-(defn maybe-post-twit [items]
- (let [twitter-status (cond
- (< 1 (count items))
- (format "%d new items posted" (count items))
- (= 1 (count items))
- (format "new: %s" (first items)))]
- (when twitter-status
- (update-twitter-status +twitter-auth-file+ twitter-status))))
+(defn post-twits [urls titles]
+ (when titles
+ (update-twitter-status (format "%s %s" (first titles)
+ (:body (http/simple-http-request
+ (format "http://tinyurl.com/api-create.php?url=%s"
+ (first urls))
+ nil))))
+ (recur (rest urls) (rest titles))))
(defn poll
"Poll planet lisp, check for new postings, update Twitter status when new postings have appeared"
[]
(save-data
- (let [old-data (load-data)
+ (let [old-urls (load-data)
process
- (fn [items new-data new-items]
+ (fn [items new-urls new-titles]
(if items
(let [item (first items)
guid (first (xml-> item :guid text)) ]
(recur (rest items)
- (conj new-data guid)
- (if (old-data guid)
- new-items
- (conj new-items (first (xml-> item :title text))))))
+ (conj new-urls guid)
+ (if (old-urls guid)
+ new-titles
+ (conj new-titles (first (xml-> item :title text))))))
(do
- (maybe-post-twit new-items)
- new-data)))]
+ (post-twits new-urls new-titles)
+ new-urls)))]
(process (xml-> (feed-to-zip "http://planet.lisp.org/rss20.xml") :channel :item)
#{} []))))
More information about the Bknr-cvs
mailing list