Twitter (out dated)

By eqrunner on Dec 16, 2009

Screenshots

2013-06-19 UPDATE: This code used Twitters old RSS feed data. Twitter has now swtiched to API based script and I have not had a chance to update this code to reflect this API change. Will update when it has been fixed. ETA 2 months. (I am busy with work sorry)

!twitter and will display most recent post for requested name. modifiable.

Currently it works with any TWITTER screen name. And irc users can assign a twitter name to their $nick. This will be added to a hast table according to the network this occurred on. (this is for bots that live on multiple networks, allowing $nick to twitter links separate)

See commands located in the code for basic command instructions.

It is setup right now to grab all the data from the xml page and set them to all to corresponding variables.(long list of if statements) To view what it returns, just comment out the unset %t_* at the bottom of the script. execute the script (ex: !twitter eqrunner) and then look at your variables.

Updated 2009-12-17 11:44est
Rewrote it. It pulls from the www.twitter.com/statuses/user_timeline.xml?
It also has an error string in case a user is not found/entered in wrong.

Updated 2009-12-17 12:34est: Added more features.
Updated 2009-12-17 14:48est: Added registration feature. Final version posted. v1.0
Updated 2011-01-19 23:31est: Added the ablity to view last 5 twitter posts. !twitter [twittername] [1 to 5]
Updated 2011-02-04: v1.3 Fixed special character issues.
Updated 2011-12-10 v1.4: Adapted it to the newer twitter api
Updated 2012-03-16 v1.5: Revamped sockread to read correctly from new XML layout. Added comments to script.. Faster response.
Updated 2012-11-26 v1.7: Modified: Now Pulls from https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=eqrunner
See notes in the bottom of the code.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; twitter ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; by eqrunner ;;;v1.7;;
;; http://www.hawkee.com/snippet/7033/ ;;;;;;;;;;;;;;;

/*
!twitter $null                           Will return default name for said network - see: if (%t_2 == $null)
!twitter [twittername|irc nick]          Will return said users posting(s) (if not already assigned to a irc $nick)
!twitter [twittername] [s|stats|status]  Will return following/follower data
!twitter [twittername] [last|time]       Will return with date and time of last post (GMT) time
!twitter [twittername] [#]               Will return current with up to 5 previous posts. (!twitter eqrunner 5)

!twitter Register [twittername]          Will assign said twitter name with that $nick that send this command in that $network 
!twitter Remove [twittername]            Will remove the nick from the hash tag registry

!twitter [on/off]                        Allows ops to turn on or off twitter, per $network and $chan
*/

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;-- Following will both create and save the twitter to hash table upon exit, and then use that data to create 
;;;-- twitter hash table on a connect. ---

on *:CONNECT: {
  var %twitter_file  $+(twitter_,$network)
  hmake %twitter_file 100
  if ($isfile($+(%twitter_file,.dat))) { hload %twitter_file $+(%twitter_file,.dat) }
}

;;; will save twitter hash table to a local file twitter_file.dat
ON *:DISCONNECT: {
  var %twitter_file  $+(twitter_,$network)
  if ($hget(%twitter_file)) { hsave -o %twitter_file $+(%twitter_file,.dat) }
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;-- MAIN CODE ---

on *:TEXT:!twitter*:#: {
  botabuse 
  unset %t_*
  sockclose twitter
  set %t_nick $nick
  set %t_chan $chan
  set %t_network $network
  set %t_2 $strip($2)
  set %t_3 $strip($3)
  if ($3 == s) || ($3 == stats) || ($3 == status) || ($3 == count) || ($3 == c) { set %t_3 stats | set %t_info_req ON | set %t_count 1 }
  if ($3 == last) || ($3 == time) { set %t_3 last | set %t_info_req ON | set %t_count 1 }
  if ($3 isnum 1-5) { set %t_3 lines | set %t_lines $3 | set %t_count $3 }
  if ($3 isnum) && ($3 > 5) { set %t_3 lines | set %t_lines 5 | set %t_count 5 }
  if ($3 == $null) set %t_count 1

  if (%t_2 == $null) {
    ;;; -- set a default for a network and/or a chan so anyone can callup the primary for that chan --
    ;;; -- Network only example --
    if (%t_network == EFnet) set %t_2 michaelgroff
    if (%t_network == iPocalypse) set %t_2 ShoutDRIVE

    ;;; -- Network and Channel ---
    if (%t_network == Hawkee) && (%t_chan == #bots) set %t_2 HawkeeBot
    if (%t_network == WebChat) && (%t_chan == #thehome) set %t_2 HawkeeBot
  }

  if (%t_2 != $null) {
    ;;; -- If a name has been added --

    var %twitter_reg $($+(twitter_,$network))

    ;;; -- Register Twitter name to channel $nick ---
    if (%t_2 == register) && (%t_3 != $null) {
      hadd -m %twitter_reg %t_nick %t_3
      .msg %t_nick You are now linked to %t_3
      set %t_2 $hget(%twitter_reg,%t_nick)
    }
    ;;; -- Removes twitter name connected to $nick ---
    if (%t_2 == remove) && (%t_3 != $null) {
      hdel %twitter_reg %t_nick
      .msg %t_nick You twitter association was removed
      twitter_cleanup
    }
    ;;; -- Checks to see if $nick is listed in the twitter hash table ---
    if ($hget(%twitter_reg,%t_2) != $null ) {
      set %t_2 $hget(%twitter_reg,%t_2)
    }

    ;;; --- Takes info provided and creates the sock's url ---
    set %t_url $+(/1/statuses/user_timeline.xml?screen_name=,%t_2)
    ;set %t_url $+(/statuses/user_timeline.xml?screen_name=,%t_2,&count=,%t_count)
    ;set %t_url $+(/statuses/user_timeline.rss?screen_name=,%t_2,&count=,%t_count)
    ;;; -- Looks like this: /statuses/user_timeline.xml?screen_name=eqrunner&count=1 ---

    ;;;---Below is used to turn the script on or off, but only if user is an op of a channel ---
    if (%t_2 == on) || (%t_2 == off) && (%t_nick isop %t_chan) {
      if (%t_2 == on) {
        set $+(%,twitter_,%t_network,_,%t_chan) ON
        .notice %t_nick !twitter script is now ON
        twitter_cleanup
        halt   
      }
      if (%t_2 == off) {
        set $+(%,twitter_,%t_network,_,%t_chan) OFF
        .notice %t_nick !twitter script is now OFF
        twitter_cleanup
        halt     
      }
    } ;end of %t_on/off
    ;--- Checks if twitter is suppost to be OFF for the channel --
    if ($($+(%,twitter_,%t_network,_,%t_chan),2) == OFF) {
      twitter_cleanup
      halt
    }

  }

  sockopen twitter twitter.com 80
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; --- Twitter Socket ---

on *:sockopen:twitter: {
  ;sockwrite -n $sockname GET /1/statuses/user_timeline.xml?screen_name=eqrunner HTTP/1.1
  sockwrite -n $sockname GET %t_url HTTP/1.1
  sockwrite -n $sockname HOST: api.twitter.com
  sockwrite $sockname $crlf
}

on *:sockread:twitter: {
  sockread %t_temp
  ;echo -s Line: %t_temp

  ;;; --- Subsitutions and Replacment for common characters ---
  var %t_temp $replacex(%t_temp,&quot;,",&amp;,&,&amp;lt;,<,&amp;rt;,>)
  var %t_temp $regsubex(%t_temp,/&#(\d+)\;/g,$chr(\1))

  ;;; --- Below is all the variables that are created. comment out what you don't want. ---

  if (<status> isin %t_temp) inc %t_i
  if (<created_at> isin %t_temp) set $+(%,t_created_at_,%t_i) $remove(%t_temp,<created_at>,</created_at>,$chr(9))
  if (<text> isin %t_temp) set $+(%,t_text_,%t_i) $remove(%t_temp,<text>,</text>,$chr(9))

  if (<source> isin %t_temp) set $+(%,t_source,%t_i) $remove(%t_temp,<source>,</source>,$chr(9))
  if (<truncated> isin %t_temp) set $+(%,t_truncated_,%t_i) $remove(%t_temp,<truncated>,</truncated>,$chr(9))
  if (<favorited> isin %t_temp) set $+(%,t_favorited_,%t_i) $remove(%t_temp,<favorited>,</favorited>,$chr(9))
  if (<in_reply_to_screen_name> isin %t_temp) set $+(%,t_in_reply_to_screen_name,%t_i) $remove(%t_temp,<in_reply_to_screen_name>,</in_reply_to_screen_name>,$chr(9))
  if (<retweet_count> isin %t_temp) set $+(%,t_retweet_count_,%t_i) $remove(%t_temp,<retweet_count>,</retweet_count>,$chr(9))       
  if (<retweeted> isin %t_temp) set $+(%,t_retweeted_,%t_i) $remove(%t_temp,<retweeted>,</retweeted>,$chr(9))

  ;;; --- Unrepeated variables. Below items are always the same between each entry ---
  ;;; --- I have commented out ones I do not need/use ---

  if (<name> isin %t_temp) && (%t_name == $null) set $+(%,t_name) $remove(%t_temp,<name>,</name>,$chr(9))  
  if (<screen_name> isin %t_temp) && (%t_screen_name == $null)  set $+(%,t_screen_name) $remove(%t_temp,<screen_name>,</screen_name>,$chr(9))  
  if (<location> isin %t_temp) && (%t_location == $null)  set $+(%,t_location) $remove(%t_temp,<location>,</location>,$chr(9))  
  if (<description> isin %t_temp)  && (%t_description == $null) set $+(%,t_description) $remove(%t_temp,<description>,</description>,$chr(9))  

  if (<url> isin %t_temp)  && (%t_url == $null) set $+(%,t_url) $remove(%t_temp,<url>,</url>,$chr(9))  
  if (<protected> isin %t_temp)  && (%t_protected == $null) set $+(%,t_text) $remove(%t_temp,<protected>,</protected>,$chr(9))  
  if (<utc_offset> isin %t_temp)  && (%t_utc_offset == $null) set $+(%,t_utc_offset) $remove(%t_temp,<utc_offset>,</utc_offset>,$chr(9))
  if (<time_zone> isin %t_temp) && (%t_time_zone == $null)  set $+(%,t_time_zone) $remove(%t_temp,<time_zone>,</time_zone>,$chr(9))
  if (<verified> isin %t_temp) && (%t_verified == $null)  set $+(%,t_verified) $remove(%t_temp,<verified>,</verified>,$chr(9))
  if (<created_at> isin %t_temp)  && (%t_created_at == $null) set $+(%,t_created_at) $remove(%t_temp,<created_at>,</created_at>,$chr(9))

  if (<followers_count> isin %t_temp) && (%t_followers_count == $null) set $+(%,t_followers_count) $remove(%t_temp,<followers_count>,</followers_count>,$chr(9))
  if (<friends_count> isin %t_temp)  && (%t_friends_count == $null) set $+(%,t_friends_count) $remove(%t_temp,<friends_count>,</friends_count>,$chr(9))
  if (<favourites_count> isin %t_temp)  && (%t_favourites_count == $null) set $+(%,t_favourites_count) $remove(%t_temp,<favourites_count>,</favourites_count>,$chr(9))
  if (<statuses_count> isin %t_temp) && (%t_statuses_count == $null)  set $+(%,t_statuses_count) $remove(%t_temp,<statuses_count>,</statuses_count>,$chr(9))
  if (<listed_count> isin %t_temp) && (%t_listed_count == $null)  set $+(%,t_listed_count) $remove(%t_temp,<listed_count>,</listed_count>,$chr(9))     

  ;;; --- Errors and end of sockread ---
  if (<error code isin %t_temp) {
    set %t_error ON
    set %t_error_temp $replace(%t_temp,$chr(60),$chr(167),$chr(62),$chr(167))
    set %t_error_line $gettok(%t_error_temp,3,167)
    sockclose $sockname
    twitter_close
  }

  if (%t_i > %t_count) {
    ; this IF added so that it will react faster. Prevents it from going through 20+ entries before responding back
    sockclose $sockname
    twitter_close
  }

  if (</statuses> isin %t_temp) {
    sockclose $sockname
    twitter_close
    echo -s END --------
  }

}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; --- The Result. ---

alias twitter_close {

  ;on *:sockclose:twitter: {

  if (%t_error == $null) {
    if (%t_info_req != ON) {
      ;;; --- Above Checks to see if you are wanting info on a user, and not just their status ---
      ;;; --- Messages chan the first/most recent tweet ---
      msg %t_chan %t_screen_name $+ : %t_text_1

      ;;; --- if the user has requested multiple tweets, then the below occurs (!twitter eqrunner 4) ---
      if (%t_3 == lines) {
        var %t_i 2
        var %t_timer 1
        while (%t_lines >= 2) {
          .timer 1 %t_timer msg %t_chan %t_screen_name %t_i $+ :  $($+(%,t_text_,%t_i),2)
          dec %t_lines
          inc %t_i
          inc %t_timer
        }
      }
    }

    ;;; --- Twitter stats on a user ---
    if (%t_3 == stats) msg %t_chan %t_screen_name $+ : Following: %t_friends_count Followers: %t_followers_count Updates: %t_statuses_count
    if (%t_3 == last) msg %t_chan %t_screen_name Last updated: %t_created_at 
  }

  ;;; --- If there was an error, or user was not found ---
  if (%t_error == ON) {
    msg %t_chan ERROR: %t_2 $chr(124) %t_error_line
    ;twitter_cleanup
  }

  twitter_cleanup
}

alias twitter_abuse {
  sockopen twitter_abuse twitter.com 80
}

on *:sockopen:twitter_abuse: {
  sockwrite -n $sockname GET %t_url
  sockwrite -n $sockname www.twitter.com
}

alias twitter_cleanup {
  ;unset %t_*

}

;;;;;;;;;;;;;;;;;;;;;;;;;
/*
Version History
1.0: Inital Launch
1.1: Added multiple lines previous tweets.
1.2: Corrected text characters (&quote; &amp; )
1.3: Corrected text characters (&#8217; etc)
1.4: Updated slightly for twitter api changes
1.5: Revamped sockread to read correctly from new XML layout. Added comments to script.
1.5.5: Corrected empty tweets. <Bot> :   (This due to twitter api not always understanding the &count=1 of the url)
1.6: Added Ablity to turn twitter on or off by $chan op. Per channel.
1.7: Updated to link to api.twitter.com URL.
Added sockread limit, So if a person requests 2 tweets. It will only read the first 2 tweets. Instead of all 20 every time.
Fixed Errors. Will now show the error.
*/
;;;;;;;;;;;;;;;;;;;;;;;;;

Comments

Sign in to comment.
eqrunner   -  Nov 26, 2012

All Better now. Went faster than expected. v1.7 is posted.

jasonh  -  Aug 11, 2013

I hope this script is updated to the twitter API. it is really a great script and does what i want it to do.

Sign in to comment

eqrunner   -  Nov 26, 2012

FYI, Twitter has closed off the xml page this script was referencing. Working on fixing this. Will post modified script when I have it up and running

 Respond  
ryan7461035   -  Jun 18, 2012

Wow works perfectly, Thanks a whole lot Jethro. And eqrunner =] !

 Respond  
Jethro   -  Jun 18, 2012

Ryan, try replacing my modification in place of the original and see how it goes:

if (%t_nick isop %t_chan) && ($regex(%t_2,/^o(n|ff)$/iS)) {
      if ($regml(1) == n) {
        set $+(%,twitter_,%t_chan) ON
        .notice %t_nick !twitter script is now ON
        twitter_cleanup 
      }
      else {
        set $+(%,twitter_,%t_chan) OFF
        .notice %t_nick !twitter script is now OFF
        twitter_cleanup     
      }
    } ;end of %t_on/off
  ;--- Checks if twitter is suppost to be OFF for the channel --
  if ($($+(%,twitter_,%t_chan),2) == OFF) {
    twitter_cleanup
    halt
  }
 Respond  
ryan7461035   -  Jun 18, 2012

I am an OP. i am using this Snippet On Justin.Tv on my irc bot.

 Respond  
eqrunner   -  Jun 18, 2012

You have to be an OP of the channel in order to use the ON / OFF feature. If your only voice or normal user, you will get the result you mentioned.

 Respond  
ryan7461035   -  Jun 17, 2012

It Does this for me. Hmmm

Me: !Twitter On

on: @liampayne_mybf aww I‘m honoured!! Me: !Twitter Off OFF: Quelques images du new clip de @Juvenilesband intitulé "Through The Night" ..
 Respond  
eqrunner   -  Jun 17, 2012

@ryan7461035 !twitter v1.6 released. It includes the on/off function.

My bad for giving you bad direction. It needed to be placed in the "if (%t_2 != $null)" Section. I had copied/pasted from my wunderground script and didn't test it.
The v1.6 should work. It is set up for each individual channel of each network. (Aka you can be on irc.hawkee.net with twitter off on #bestchanever. But still have twitter on and working on irc.efnet.net's #bestchanever)
Hope this helps.

 Respond  
ryan7461035   -  Jun 16, 2012

i did !twitter on & !twitter off and nothing happen i post it in Main Code section like you said.. Can You Post it within the whole code and put it on like Pastebin if you don't mind. Thanks

 Respond  
eqrunner   -  Jun 16, 2012

@ryan7461035 Below is code you can add into the 'Main code' section to give you the option to turn it off per channel.

    ;;;---Below is used to turn the script on or off, but only if user is an op of a channel ---

    if (%t_2 == on) || (%t_2 == off) && (%t_nick isop %t_chan) {
      if (%t_2 == on) {
        set $+(%,twitter_,%t_chan) ON
        .notice %t_nick !twitter script is now ON
        twitter_cleanup
        halt   
      }
      if (%t_2 == off) {
        set $+(%,twitter_,%t_chan) OFF
        .notice %t_nick !twitter script is now OFF
        twitter_cleanup
        halt     
      }
    } ;end of %t_on/off

  ;--- Checks if twitter is suppost to be OFF for the channel --
  if ($($+(%,twitter_,%t_chan),2) == OFF) {
    twitter_cleanup
    halt
  }
 Respond  
ryan7461035   -  Jun 10, 2012

Ok ty, Yes Only Ops turns it on or off

 Respond  
eqrunner   -  Jun 09, 2012

@ryan7461035 In the top area in the ;;; Network line. you could add a simular line for the chan and network. and have the line say simply t_cleanup | halt.
as for a switch function, who would you want to be able to control it? ops only of said chan? I will follow up next week when i am back near a computer. cant edit well on a smart phone :p

 Respond  
ryan7461035   -  Jun 07, 2012

Can You Add like a On/off function to it for me? To Use on Justin.tv . Plus it make so it dont also affect other channels since i have it Multi to other channels.

 Respond  
ZerOne01   -  May 24, 2012

Bot doesn't work well when I want to show the status of people who retweeted other people's post

 Respond  
eqrunner   -  Apr 09, 2012

I did dig deeper and have made needed corrections. version 1.5 was released 2012-03-16

 Respond  
eqrunner   -  Dec 10, 2011

Twitter has changed their API around and I have gotten it to work with it. But I can't promise 100%. When I do get some free time, I will dive deeper into it.

 Respond  
jasonh   -  Dec 08, 2011

no longer works

 Respond  
eqrunner   -  Jan 19, 2011

@ Exterus, it's still a glitch with the script. I need to make a tweak to get it to render those kinds of commands correctly. It's on my to-do list when I get some free time.

@ ericfx Just fixed it. with a number from 1 to 5, will post the previous posts, (up to 4 + current post)

 Respond  
Exterus   -  Dec 30, 2010

The script doesn't appear to be able to handle special characters at all. Quote marks become " and any foreign letters turn into gibberish. Is that fixable on the script side or is it a serverside issue?

 Respond  
ericfx   -  Nov 09, 2010

Hello eqrunner,

Thanks for this. Is there any way to make it display more than one line (past tweets)?

 Respond  
lone0001   -  Sep 19, 2010

I was using this on one instance of mIRC it worked fine with it and then I started another one (I unloaded the script from the first one, so it's only being used in this new one) and loaded the script in it and it doesn't seem to work, it just returns ":", any idea why it isn't working?

I copied it directly from here when I loaded it in the new one.

 Respond  
eqrunner   -  Mar 26, 2010

@ Alpha32. As far as I know its a twitter glitch. As I get similar errors when browsing twitter on my browser. I have gone weeks with no problem, then have a string of No finds in a block of 3 hrs. And hadn't changed the code at all. Knowing twitter and the many glitches. I would say its a twitter api fail.
But if some one sees a glitch that could cause that in my code, do tell, cause I don't see anything that would cause it.

 Respond  
Alpha32   -  Mar 21, 2010

I use this for my bot but when I or someone else does !twitter it says 'Sorry, name was not found' even tho it does, but then say about 20 mins later it works again for a few mins then does it again :/

 Respond  
diamondandy   -  Feb 02, 2010

Btw, if I get stuck with some other mirc related issues, could I give you a PM or something?

Don't feel obliged to say yes haha :D

 Respond  
FordLawnmower   -  Feb 02, 2010

No Problem diamondandy :)

 Respond  
diamondandy   -  Feb 02, 2010

FordLawnmower, thank you very much :D

 Respond  
FordLawnmower   -  Feb 02, 2010

If you load the script into your notebook then you will need to type !twitter diamondandy from your desktop or some other computer.

 Respond  
diamondandy   -  Feb 02, 2010

ah right i see, so if i load it into my netbook which has mirc on it, and follow the same instructions i typed, it should work when i type !twitter diamondandy (or whatever i type)

do i need to change anything in the script at all, or is it good to go?

 Respond  
FordLawnmower   -  Feb 02, 2010

This script requires a second client to trigger the script diamondandy. You can't trigger it with the same client that the script is loaded into.

 Respond  
diamondandy   -  Feb 02, 2010

when i open up the script editor i'm a bit unsure as to where i should place this coding.

i clicked on the remote tab and clicked file > new, then i pasted the coding into the new page. i then saved it and typed !twitter username and nothing happens.

what am i doing wrong?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.