This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Script to return Title & URL of Random Sub-Reddit Post

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
n
neocharles
Voice
Posts: 34
Joined: Tue Apr 23, 2013 4:29 pm

Script to return Title & URL of Random Sub-Reddit Post

Post by neocharles »

What I am looking to be able to do is have anyone in the room type something like

Code: Select all

!news
And it return the title of a random post, along with said URL.

Every subreddit has an rss feed available to them by adding .rss to the end of the URL

Code: Select all

http://www.reddit.com/r/news/.rss
I imagine the easiest way would be to query the rss feed to accomplish this.


I know pretty much zero on scripting at all, so I was curious if there is something that is already written to accomplish this, or if someone would be able to help me in getting something working for this. Simple enough that I can figure out how to add additional triggers for different URLs of rss feeds?


Thanks!
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

This what ive comed up for what you asked
Im not that good with XML pages or tdom but it does what you've asked for (atleast from my tests). I know it can be done different, maybe an easy way but thats how ive managed to make it with my knowledge.

The command !news works for everything if you want to change that to a particular acces post here and ill modify the script for you.

So it gets a random title and its link from that RSS page and displays it on the main channel.

Code: Select all

bind PUB - !news rss:info

package require http
package require tdom

proc erepublik:info {nick uhost hand chan arg} {
	global {my-ip} news

	set l1 ""
	set l2 ""

	## ++ INFO
	set token [http::config -useragent Mozilla]
	set token [http::geturl "http://www.reddit.com/r/news/.rss"]
	set data [::http::data $token]
	::http::cleanup $token

	set XML $data

	set doc [dom parse $XML]
	set root [$doc documentElement]

	foreach node [$root getElementsByTagName "title"] { if {![string match -nocase "news" $node]} { lappend l1 "[$node asText]" } }
	foreach node [$root getElementsByTagName "link"] { if {$node != "http://www.reddit.com/r/news/"} { lappend l2 "[$node asText]" } }

	set nr 0
	foreach n $l1 { incr nr }

	set nr [rand $nr]

	putserv "PRIVMSG $chan :Title: \00304[lindex $l1 $nr] --> \00312[lindex $l2 $nr]"
}
PS: Not proud of this code
n
neocharles
Voice
Posts: 34
Joined: Tue Apr 23, 2013 4:29 pm

Post by neocharles »

I haven't tired it yet, but had a few questions...

Is this storing the information in a 'database' of sorts for what is in the rss feed? (I'd probably suggest for anyone to use the /r/subreddit-name/new/.rss instead of the top for more variety...)

And if it is, how often is it updating that? Just on the trigger? If I trigger 5 times in a row, is it different stuff?


Also, I have quite a few subreddits I'd like to toy around with this with ... if it's not too much trouble, would you be able to show me how to do different ones with the script? Or multiple ones, etc?
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

It doesnt stores anything and at every trigger request it will display a different or the same as the last info. And to add more rss pages to this script i dont know if its the same XML version (maybe it will work) but i dont know
Post Reply