Twitter command-line utility update v.03

Featuring the Dragonfly web framework
Locked
Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Twitter command-line utility update v.03

Post by Lutz »

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

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



ps: also linked from the Tips&Tricks page

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Post by itistoday »

Lutz, is that you @newlisp?

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Post by itistoday »

Lutz wrote:(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
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

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
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Re: Twitter command-line utility update v.03

Post by axtens »

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.

Code: Select all

#!/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) "\r\n"
             (lookup '(author name) entry ) "\r\n" 
             (lookup 'title entry) "\r\n\r\n"))
  (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) "\r\n"))

(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) "\r\n") "\r\n"))
  (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)) "\r\n" (post 2 1) "\r\n" (post 3 1) "\r\n\r\n"))
    ))
  (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)) "\r\n" (post 10 2 1) "\r\n" (post 3 1) "\r\n\r\n"))
    ))
  (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Code: Select all

(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 but I am not sure if it still works. It might take care of the 'main-args' issue.

heifler
Posts: 3
Joined: Sun Aug 09, 2009 8:55 pm

Command Line

Post by heifler »

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Open Command Prompt, and type:

Code: Select all

newlisp "script"
where script is the name of the script.

To see more options, type:

Code: Select all

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

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

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

cormullion wrote: I've just found a Windows machine and tried this. Yay, my first time! :)
Welcome to to dark side young Jedi...

Link: 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

Locked