Twitter command-line utility update v.03

Started by Lutz, March 02, 2009, 03:58:22 AM

Previous topic - Next topic

Lutz

Version 0.3 of the Twitter command-line interface can be found here:



http://www.newlisp.org/syntax.cgi?code/twitter.txt">http://www.newlisp.org/syntax.cgi?code/twitter.txt . Try 'twitter search newlisp'.







ps: also linked from the Tips&Tricks page

itistoday

#1
Lutz, is that you @http://twitter.com/newlisp">newlisp?



Edit: No, I assume http://twitter.com/lutz_mueller">this is you, but are you on as newlisp as well?  Greetings from @http://twitter.com/taoeffect">taoeffect! :-D
Get your Objective newLISP groove on.

Lutz

#2
... yes, thats me. @newlisp for newLISP announcements and @lutz_mueller for everything else (probably not used much).

itistoday

#3
Quote from: "Lutz"(probably not used much).


You'll give into it, sooner or later, everyone does. </cliche> :-D
Get your Objective newLISP groove on.

m i c h a e l

#4
Just wanted to mention that I deleted my Twitter account a few days ago. I did this because a couple of my entries were "disappeared," and I had lost confidence in Twitter.  



I mention this because I didn't want those individuals whom I was following to think I simply stopped following them :-)



m i c h a e l

axtens

#5
Lutz,



Here's my take on the twitter.lsp (v0.3ax) which uses my linked? and command-line stuff. This way the script can be executed either by newlisp.exe on the commandline or after use of link.lsp.



Kind regards,

Bruce.
#!/usr/bin/newlisp

; post, delete and see user and friends messages and followers on http://twitter.com
; February 12th, 2009, L.M.
; version 0.1
; version 0.2
;    added twitter seach
;    fields now separated by CR-LF
;    records now separated by CR-LF CR-LF
; version 0.3
;    eliminated spurious nil in search
;    added xml option to search
; version 0.4ax
; better main-args handling. Now using linked? and command-line
; not very graceful about failures, e.g. search without search term
; when no parameters are given, show help text

(define (linked?) (not (starts-with (lower-case (main-args 0)) "newlisp")))
(setq command-line (if (linked?) (main-args) (rest (main-args))))

(setq helptext "EXAMPLES:
    twitter userid:pass followers
    twitter userid:pass followers xml
    twitter userid:pass user
    twitter userid:pass user 10
    twitter userid:pass friends
    twitter userid:pass friends 10
    twitter userid:pass delete 1234567890
    twitter userid:pass post "this is a test"
    twitter search the-word-to-search

append "xml" to return results as XML"
)

(unless (>= (length command-line) 2)
(println helptext)
(exit)
)

; (println "foo")
(when (= (command-line 1) "search")
  ; this is a search, no user authentication is required
  (setq xml (get-url (string "http://search.twitter.com/search.atom?q=" (command-line 2))))
  (when (= (command-line -1) "xml")
    (println xml)
(exit))
  (xml-type-tags nil nil nil nil) ; no extra tags
  (setq sxml (xml-parse xml 31)) ; turn on SXML options
  (setq entry-index (ref-all '(entry *) sxml match))
  (when (empty? entry-index)
    (println "No entries found")
    (exit))
  (dolist (idx entry-index)
    (setq entry (sxml idx))
    (println (lookup 'published entry) "rn"
             (lookup '(author name) entry ) "rn"
             (lookup 'title entry) "rnrn"))
  (exit))

(define (url-encode str)
  (replace {([^a-zA-Z0-9])} str (format "%%%2X" (char $1)) 0))

; authorization user id and password
(setq user-pass (command-line 1))
(setq auth (append "Authorization: Basic " (base64-enc user-pass) "rn"))

(when (= (command-line 2) "followers")
  (setq xml (get-url "http://twitter.com/statuses/followers.xml" 5000 auth))
  (if (= (command-line -1) "xml")
    (println xml)
    (println (join (find-all "<screen_name>(.*)</screen_name>" xml $1) "rn") "rn"))
  (exit))

(when (= (command-line 2) "user")
  (if (> (length command-line) 3) (setq cnt (command-line 3)) (setq cnt "1"))
  ;(if-not cnt (setq cnt "1")) ; return only the last post by default
  (setq url (append "http://twitter.com/statuses/user_timeline.xml?count=" cnt))
  (setq xml (get-url url 5000 auth))
  (if (= (command-line -1) "xml")
    (println xml)
    (begin
      (xml-type-tags nil nil nil nil)
      (setq sxml (xml-parse xml 31))
      (setq sxml (2 (sxml 0)))
      (dolist (post sxml)
        (println (0 19 (post 1 1)) "rn" (post 2 1) "rn" (post 3 1) "rnrn"))
    ))
  (exit))
   
(when (= (command-line 2) "friends")
  (if (> (length command-line) 3) (setq cnt (command-line 3)) (setq cnt "1"))
  ;(if-not cnt (set 'cnt "1")) ; return only the last post by default
  (setq url (append "http://twitter.com/statuses/friends_timeline.xml?count=" cnt))
  (setq xml (get-url url 5000 auth))
  (if (= (command-line -1) "xml")
    (println xml)
    (begin
      (xml-type-tags nil nil nil nil)
      (setq sxml (xml-parse xml 31))
      (setq sxml (2 (sxml 0)))
      (dolist (post sxml)
        (println (0 19 (post 1 1)) "rn" (post 10 2 1) "rn" (post 3 1) "rnrn"))
    ))
  (exit))
   
(when (= (command-line 2) "delete")
  (setq url (string "http://twitter.com/statuses/destroy/" (command-line 3) ".xml"))
  (setq xml (delete-url url 5000 auth))
  (if (= (command-line -1) "xml")
    (println xml)
    (begin
      (if (find "<text>(.*)</text>" xml 0)
        (println "deleted: " $1))
      (if (find "<error>(.*)</error>" xml 0)
        (println "error: " $1))
    ))
  (exit))

(when (= (command-line 2) "post")
  (setq url (string "http://twitter.com/statuses/update.xml"))
  (setq msg (join (3 command-line) " "))
  (setq text (append "status=" (url-encode msg)))
  (setq content-type "application/x-www-form-urlencoded")
  (setq xml (post-url url text content-type 5000 auth))
  (if (= (command-line -1) "xml")
    (println xml)
    (begin
      (if (find "<text>(.*)</text>" xml 0)
        (println "posted: " $1))
      (if (find "<error>(.*)</error>" xml 0)
        (println "error: " $1))
    ))
  (exit))

(println "wrong command")
(println helptext)
(exit)

;eof





P.S. I'm not exactly sure where to post this.

Lutz

#6
On Unix OSs you could see "/usr/bin/newlisp" or similar in (args 0), so the following might be safer:


(if (find "newlisp" (args 0) 1) ... )

although most Unix folks wouldn't use link.lsp, because they would have to compile newLISP anyway, if not running Mac OX X or Ubuntu Linux.



Also, isn't there a way on Windows to combine source with a batch file? There is something here: http://www.newlisp.org/index.cgi?page=Code_Snippets">http://www.newlisp.org/index.cgi?page=Code_Snippets but I am not sure if it still works. It might take care of the 'main-args' issue.

heifler

#7
What is the code to use a Windows OS Shell command to execute a script?

cormullion

#8
Open Command Prompt, and type:


newlisp "script"

where script is the name of the script.



To see more options, type:


newlisp -h

I've just found a Windows machine and tried this. Yay, my first time! :)



(The manual gets it spot on, too... :))

xytroxon

#9
Quote from: "cormullion"
I've just found a Windows machine and tried this. Yay, my first time! :)


Welcome to to dark side young Jedi...



Link: http://www.pcstats.com/articleview.cfm?articleID=1723">Beginners Guides: WindowsXP Command Prompt



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976