egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Not Working 100%
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Dominatez
Voice


Joined: 14 Jan 2019
Posts: 13
Location: United Kingdom

PostPosted: Sat Feb 09, 2019 7:33 pm    Post subject: Not Working 100% Reply with quote

Hi,

Updated this script from a friend who is no longer active, but the life of me, i cannot get it to print out the news properly. I think i am doing something wrong possibly with the sort by or latest. News starts with
!news
BBC News: -
-
-

And prints nothing out.

Any help would be appreciated.

Grab a key from newsapi.org

Code:

namespace eval newsy {
   namespace eval news {
    package require http
    package require json
    package require tls
        tls::init -tls1 true -ssl2 false -ssl3 false
    http::register https 443 tls::socket
    bind pub - !news newsy::news::search
   bind pub - !setnews newsy::news::source
    variable version "2.0"
    setudef flag news
    variable key "USE YOUR OWN KEY HERE... GET ONE FROM NEWSAPI.ORG"
    ::http::config -useragent "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

  # Allow a user to save their choice of news source
  proc source {nick uhost hand chan text} {
      putlog "newsy::news::source nick: $nick, uhost: $uhost, hand: $hand"
      if { [string length $text] <= 0 } {
          puthelp "PRIVMSG $chan :Your source name seemed very short? Pick one from https://newsapi.org/sources"
          return
      }
      set url "https://newsapi.org/v2/sources?&apiKey=$newsy::news::key"
      set ids [getinfo $url]
      set ids [lindex $ids 3]
      set result [lsearch -regexp $ids $text]
      if { $result <= 0 } {
          puthelp "PRIVMSG $chan :Couldn't find source $text. Pick one from https://newsapi.org/sources"
          return
      }

      putlog "RESSULT: $result"
      set spam [lindex $ids $result]
      putlog "Name: [lindex $ids $result 3], Description: [lindex $ids $result 7]"

      if {![validuser $hand]} {
          adduser $nick
          set mask [maskhost [getchanhost $nick $chan]]
          setuser $nick HOSTS $mask
          chattr $nick -hp
          putlog "newsy::news::source added user $nick with host $mask"
      }
      setuser $hand XTRA newsy:news.source $text
      setuser $hand XTRA newsy:news.name [lindex $ids $result 3]
      puthelp "PRIVMSG $chan :set default source to [lindex $ids $result 3]([lindex $ids $result 1]) - [lindex $ids $result 7]"
      putlog "newsy::news::source $nick set their default source to $text."
  }

proc getinfo { url } {
    for { set i 1 } { $i <= 5 } { incr i } {
            set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
            if {[string length rawpage] > 0} { break }
        }
        putlog "newsy::news::getinfo Rawpage length is: [string length $rawpage]"
        if {[string length $rawpage] == 0} { error "newsapi returned ZERO no data or we could not connect properly." }
        set ids [dict get [json::json2dict $rawpage]]
    return $ids

}

proc search {nick uhost hand chan text} {
        if {![channel get $chan news] } {
                return
        }
    putlog "newsy::news::search is running"
    set source [getuser $hand XTRA newsy:news.source]
  set name [getuser $hand XTRA newsy:news.name]
  if {([string length $source] <= 0) || ([string length $name] <= 0) } {
      puthelp "PRIVMSG $chan :No default news source found for you. Please set one using !setnews"
      return
  }
  putlog "Grabbing news for $nick, with source of $source and name of $name"
    set url "https://newsapi.org/v2/top-headlines?source=$source&sortBy=latest&apiKey=$newsy::news::key"
    set ids [getinfo $url]
    for {set i 0} {$i < 3} {incr i} {
    set title [encoding convertfrom [lindex $ids 7 $i 3]]
        set url [lindex $ids 7 $i 7]
    if { $i == 0 } {
            set output "$name: \002$title\002 - $url"
    } else {
        set output "\002$title\002 - $url"
    }
    puthelp "PRIVMSG $chan :$output"
    }
}
}
}
putlog "newsy::news $newsy::news::version loaded"
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Sun Feb 10, 2019 6:45 am    Post subject: Reply with quote

Code:

namespace eval news {
   set news(key) ""

   set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key"
   set news(timeout) 5000

   package require http
   package require json
   package require tls

   proc tls:socket args {
      set opts [lrange $args 0 end-2]
      set host [lindex $args end-1]
      set port [lindex $args end]
      ::tls::socket -servername $host {*}$opts $host $port
   }

   bind pub * !news [namespace current]::fetch

   proc fetch {nick uhost hand chan text} {
      variable news
      if {[scan $text {%s} source] != 1} {
         puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources"
         return
      }
      ::http::register https 443 [namespace current]::tls:socket
      set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)]
      set token [::http::geturl $url -timeout $news(timeout)]
      set data [::http::data $token]
      set json [::json::json2dict $data]
      ::http::cleanup $token
      ::http::unregister https
      if {[lsearch [dict get $json] "articles"] > -1} {
         foreach item [dict get $json articles] {
            dict with item {
               puthelp "PRIVMSG $chan :Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
            }
         }
      } else {
         puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources"
         # To see exact error message uncomment next line
         #puthelp "PRIVMSG $chan: Error: [dict get $json message]"
      }
   }
}

Haven't tested this on a bot, but I can confirm it works nicely on tclsh:
Code:

title: Venezuela's Guaido vows to open aid routes | desc: The opposition leader says he will "do everything possible", and calls for distribution volunteers. | url: http://www.bbc.co.uk/news/world-latin-america-47184755

title: May to ask MPs for more time on Brexit | desc: The PM is expected to promise MPs another vote if she has not secured a revised deal this month. | url: http://www.bbc.co.uk/news/uk-47187491

title: Was Ruskin the most important man of the last 200 years? | desc: In the bicentenary of his birth, itās time we looked again at the forward-thinking and influential ideas of the great Victorian, writes Daisy Dunn. | url: http://www.bbc.com/culture/story/20190207-was-ruskin-the-most-important-man-of-the-last-200-years

title: IS resists 'final push' in eastern Syria | desc: US-backed guerrillas target what they say is the last pocket of IS resistance near the Iraqi border. | url: http://www.bbc.co.uk/news/world-middle-east-47188355

title: Thousands flee New Zealand wildfire | desc: Thousands of people are evacuated as the blaze rages near Nelson in the country's South Island. | url: http://www.bbc.co.uk/news/world-asia-47187604

title: 'I went vegan to hide my eating disorder' | desc: Veganism is definitely having a moment, and for Rebecca Hills, 20, it was a way of hiding in plain sight. | url: http://www.bbc.co.uk/news/stories-47176759

title: How school yearbooks have the power to destroy lives | desc: It has been a tradition in the US for decades - but recent events have highlighted their dark side. | url: http://www.bbc.co.uk/news/world-us-canada-47145908

title: Shut detention camps, Turkey tells China | desc: The statement follows the reported death in a camp of a prominent musician from the Uighur minority. | url: http://www.bbc.co.uk/news/world-asia-47187170

title: Warren launches White House 2020 bid | desc: Mr Trump's campaign team responds by calling her a fraud and saying her ideas are socialist. | url: http://www.bbc.co.uk/news/world-us-canada-47185724

title: Virginia's painful 'blackface' past and present | desc: A scandal has engulfed the political leadership of the US state. What do the voters make of it? | url: http://www.bbc.co.uk/news/world-us-canada-47175239

Just put in your API key and try it with !news bbc-news for example.

This is just a proof-of-concept that I'm not going to take further, or at least not right now cos I'm a bit busy.

Edit: Fixed missing $ and added unregister for https.

Later edit: Fixed the missing space between $chan and :
_________________
I tawt I taw a puddy tat!


Last edited by caesar on Thu Feb 28, 2019 12:04 pm; edited 4 times in total
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Sun Feb 10, 2019 7:10 am    Post subject: Reply with quote

ive tried it out and it returned this error:

Quote:

12:17:45 (simo) : !news
12:17:46 (@Cappuccino) : No default news source found for you. Please set one using !setnews
12:17:51 (simo) : !setnews
12:17:52 (@Cappuccino) : Your source name seemed very short? Pick one from https://newsapi.org/sources


not sure how to add newsource i checked the link but its not clear what to add i tried copying a source link and setnews but it complained about this in PL

Quote:

<Cappuccino> [12:19:08] newsy::news::getinfo Rawpage length is: 0
<Cappuccino> [12:19:08] Tcl error [newsy::news::source]: newsapi returned ZERO no data or we could not connect properly


also the reason for it not printing anything out for you might be because u didnt enable it in partyline for the channel using .chanset #channel +news


i also tried :

Quote:
!setnews default https://newsapi.org/v2/top-headlines?sources=abc-news&apiKey=mykeyhere


didnt seem to add neither when doing !news abc-news gettin this on PL

[12:33:15] newsy::news::search is running

but no output on channel
Back to top
View user's profile Send private message
Dominatez
Voice


Joined: 14 Jan 2019
Posts: 13
Location: United Kingdom

PostPosted: Sun Feb 10, 2019 9:02 pm    Post subject: Reply with quote

Simo

You use !setnews then the source you want.. for instance

!setnews abc-news

And i did set the channel, and put that it prints the following to channel.

BBC News -
-
-

Where it should be listing the top 3 latest headlines.
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Mon Feb 11, 2019 7:16 am    Post subject: Reply with quote

i did just that

!setnews -abc-news
i get nothing in channel
but get this error in PL

Quote:

<Cappuccino> [12:12:02] newsy::news::getinfo Rawpage length is: 0

<Cappuccino> [12:12:02] Tcl error [newsy::news::source]: newsapi returned ZERO no data or we could not connect properly.

Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Mon Feb 11, 2019 9:06 am    Post subject: Reply with quote

@simo He said to try with !setnews abc-news, notice the missing - that's in front of abc in your example.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Mon Feb 11, 2019 10:00 am    Post subject: Reply with quote

it was a typo here but on irc i did !setnews abc-news

im not sure what i did wrong and why it keeps complaining

this is what i got so far

Code:
namespace eval newsy {
   namespace eval news {
    package require http
    package require json
    package require tls
        tls::init -tls1 true -ssl2 false -ssl3 false
    http::register https 443 tls::socket
    bind pub - !news newsy::news::search
   bind pub - !setnews newsy::news::source
    variable version "2.0"
    setudef flag news
    variable key "my-key"
    ::http::config -useragent "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"

  # Allow a user to save their choice of news source
  proc source {nick uhost hand chan text} {
      putlog "newsy::news::source nick: $nick, uhost: $uhost, hand: $hand"
      if { [string length $text] <= 0 } {
          puthelp "PRIVMSG $chan :Your source name seemed very short? Pick one from https://newsapi.org/sources"
          return
      }
      set url "https://newsapi.org/v2/sources?&apiKey=$newsy::news::key"
      set ids [getinfo $url]
      set ids [lindex $ids 3]
      set result [lsearch -regexp $ids $text]
      if { $result <= 0 } {
          puthelp "PRIVMSG $chan :Couldn't find source $text. Pick one from https://newsapi.org/sources"
          return
      }

      putlog "RESSULT: $result"
      set spam [lindex $ids $result]
      putlog "Name: [lindex $ids $result 3], Description: [lindex $ids $result 7]"

      if {![validuser $hand]} {
          adduser $nick
          set mask [maskhost [getchanhost $nick $chan]]
          setuser $nick HOSTS $mask
          chattr $nick -hp
          putlog "newsy::news::source added user $nick with host $mask"
      }
      setuser $hand XTRA newsy:news.source $text
      setuser $hand XTRA newsy:news.name [lindex $ids $result 3]
      puthelp "PRIVMSG $chan :set default source to [lindex $ids $result 3]([lindex $ids $result 1]) - [lindex $ids $result 7]"
      putlog "newsy::news::source $nick set their default source to $text."
  }

proc getinfo { url } {
    for { set i 1 } { $i <= 5 } { incr i } {
            set rawpage [::http::data [::http::geturl "$url" -timeout 5000]]
            if {[string length rawpage] > 0} { break }
        }
        putlog "newsy::news::getinfo Rawpage length is: [string length $rawpage]"
        if {[string length $rawpage] == 0} { error "newsapi returned ZERO no data or we could not connect properly." }
        set ids [dict get [json::json2dict $rawpage]]
    return $ids

}

proc search {nick uhost hand chan text} {
        if {![channel get $chan news] } {
                return
        }
    putlog "newsy::news::search is running"
    set source [getuser $hand XTRA newsy:news.source]
  set name [getuser $hand XTRA newsy:news.name]
  if {([string length $source] <= 0) || ([string length $name] <= 0) } {
      puthelp "PRIVMSG $chan :No default news source found for you. Please set one using !setnews"
      return
  }
  putlog "Grabbing news for $nick, with source of $source and name of $name"
    set url "https://newsapi.org/v2/top-headlines?source=$source&sortBy=latest&apiKey=$newsy::news::key"
    set ids [getinfo $url]
    for {set i 0} {$i < 3} {incr i} {
    set title [encoding convertfrom [lindex $ids 7 $i 3]]
        set url [lindex $ids 7 $i 7]
    if { $i == 0 } {
            set output "$name: \002$title\002 - $url"
    } else {
        set output "\002$title\002 - $url"
    }
    puthelp "PRIVMSG $chan :$output"
    }
}
}
}
putlog "newsy::news $newsy::news::version loaded"
Back to top
View user's profile Send private message
Dominatez
Voice


Joined: 14 Jan 2019
Posts: 13
Location: United Kingdom

PostPosted: Thu Feb 21, 2019 6:44 pm    Post subject: Reply with quote

Hi caeser,

i am getting the following error.

Tcl error [::news::fetch]: Bad value for -timeout (news(timeout)), must be integer
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Fri Feb 22, 2019 4:13 am    Post subject: Reply with quote

Missed an $ there. Find news(timeout) and make it $news(timeout) and should work.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Fri Feb 22, 2019 12:36 pm    Post subject: Reply with quote

hey there caesar i tried your version of the tcl and it returns this on !news abc-news

Quote:

Tcl error [::news::fetch]: wrong # args: should be "tls::socket ?options? host port"




Code:


namespace eval news {
   set news(key) "MY-KEY"

   set news(url) "https://newsapi.org/v2/top-headlines?sources=%source&sortBy=latest&apiKey=%key"
   set news(timeout) 5000

   package require http
   package require json
   package require tls

   proc tls:socket args {
      set opts [lrange $args 0 end-2]
      set host [lindex $args end-1]
      set port [lindex $args end]
      ::tls::socket -servername $host {*}$opts $host $port
   }

   bind pub * !news [namespace current]::fetch

   proc fetch {nick uhost hand chan text} {
      variable news
      if {[scan $text {%s} source] != 1} {
         puthelp "PRIVMSG $chan :Usage: !news <source> | Pick one from https://newsapi.org/sources"
         return
      }
      ::http::register https 443 [namespace current]::tls:socket
      set url [string map [list "%source" "$source" "%key" "$news(key)"] $news(url)]
      set token [::http::geturl $url -timeout $news(timeout)]
      set data [::http::data $token]
      set json [::json::json2dict $data]
      ::http::cleanup $token
      ::http::unregister https
      if {[lsearch [dict get $json] "articles"] > -1} {
         foreach item [dict get $json articles] {
            dict with item {
               puthelp "PRIVMSG $chan: Ttitle: [encoding convertfrom $title] | desc: [encoding convertfrom $description] | url: $url"
            }
         }
      } else {
         puthelp "PRIVMSG $chan :The specified source is invalid, pick one from https://newsapi.org/sources"
         # To see exact error message uncomment next line
         #puthelp "PRIVMSG $chan: Error: [dict get $json message]"
      }
   }
}
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Fri Feb 22, 2019 2:55 pm    Post subject: Reply with quote

By any chance you have the tls:socket defined in some other place? I'm asking cos the version in my code takes any or no arguments, yet the error "tls::socket ?options? host port" tells me that you do, so be sure you don't.
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Fri Feb 22, 2019 5:59 pm    Post subject: Reply with quote

i checked and unloaded all scripts and checked entire conf and made sure
yet still same error
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Sat Feb 23, 2019 4:12 am    Post subject: Reply with quote

Code:

% package require http
2.8.12
% package require tls
1.6.7
% proc tls:socket args {
        set opts [lrange $args 0 end-2]
        set host [lindex $args end-1]
        set port [lindex $args end]
        ::tls::socket -servername $host {*}$opts $host $port
}
% ::http::register https 443 tls:socket
443 tls:socket
%

It's still something on your side. Did you restart the bot after unloading scripts?
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
simo
Master


Joined: 22 Mar 2015
Posts: 303

PostPosted: Sat Feb 23, 2019 7:58 am    Post subject: Reply with quote

hm ok yes i restarted not sure where i went wrong as i used it as is
Back to top
View user's profile Send private message
caesar
Mint Rubber


Joined: 14 Oct 2001
Posts: 3578
Location: Mint Factory

PostPosted: Sat Feb 23, 2019 8:55 am    Post subject: Reply with quote

Should I take it that was right and now it's working?
_________________
I tawt I taw a puddy tat!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber