Skip to content

Instantly share code, notes, and snippets.

@jeroenbegyn
Last active September 11, 2021 19:28
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeroenbegyn/5419267 to your computer and use it in GitHub Desktop.
Save jeroenbegyn/5419267 to your computer and use it in GitHub Desktop.
Twitter Search with API v1.1

Description

Simple Dashing widget (and associated job) to display a Twitter search. Uses Twitter API v1.1.

##Dependencies

twitter

Add it to dashing's gemfile:

gem 'twitter'

and run bundle install. Everything should work now :)

##Usage

Put the twitter-search.rb file in your /jobs folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="twitter_mentions" data-view="Comments" data-title="Search Tweets" style="background-color: #71388a"></div>
</li>

##Settings

You'll need to add the twitter configs for your desired account to the job.

Tweets are fetched every 10 minutes, but you can change that by editing the job schedule.

require 'twitter'
Twitter.configure do |config|
config.consumer_key = 'YOUR_CONSUMER_KEY'
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
config.oauth_token = 'YOUR_OAUTH_TOKEN'
config.oauth_token_secret = 'YOUR_OAUTH_TOKEN_SECRET'
end
search_term = URI::encode('#todayilearned')
SCHEDULER.every '10m', :first_in => 0 do |job|
tweets = Twitter.search("#{search_term}").results
if tweets
tweets.map! do |tweet|
{ name: tweet.user.name, body: tweet.text, avatar: tweet.user.profile_image_url_https }
end
send_event('twitter_mentions', comments: tweets)
end
end
@allisonmin
Copy link

I've followed your directions but nothing is being pulled into the widget when I start the dashboard. Was there anything else I needed to add?

@YoDK
Copy link

YoDK commented Jul 17, 2013

@allisonmin did you gem install twitter ?

@gordhawkins
Copy link

I'm getting a weird SSL error whenever I launch dashing.. suspect it is coming from the twitter gem.

"SSL_connect SYSCALL returned=5 errno=0 state=unknown state"

Could be coming from the fact that I'm using this behind a crappy proxy that has a tendency to mangle SSL...

Any ideas?

@willwhitney
Copy link

The issue seems to be some kind of jank around Twitter's endpoint/gem. If you throw a OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE in the top of the file, everything works fine.

@zimbatm
Copy link

zimbatm commented Nov 21, 2013

The right solution is to bundle the cacert.pem, not to disable SSL...

Here is an update version of the gist with support for the twitter gem v5.0.0 and a fix for SSL: https://gist.github.com/zimbatm/7573436

@TimSmith714
Copy link

Don't know if this solves your problem @allisonmin but I got "undefined" when I ran the widget searching for @pittisea because no one had retweeted or @'d us. I retweeted a tweet from my personal account and that showed in the widget.
I removed the @ and the search started showing our tweets as well.

@paulbz
Copy link

paulbz commented Mar 13, 2014

Hi everyone, working well for me but images aren't being shown. Do you know how we can get images to appear?

@arvindsankaran
Copy link

First of all thanks jeroenbegyn for sharing this script. It works great for me.

I also wanted to display the time of the tweet and was able to get this from 'tweet.created_at' in the .rb file but didn't know the exact steps to display this within the dashing dashboard (.erb file).
Can someone kindly suggest the exact solution for this please?

Thanks

@Cornellio
Copy link

Works perfectly for me. Thank you for sharing this information.

@sansepic
Copy link

Works well for me but I can't figure out how to change the search topic. Currently it only finds #todayilearned. I tried changing this to line in the "twitter.rb" file but I still get the same twitter stuff.
search_term = URI::encode('#bettercallsaul'))
TIA.

@mkaag
Copy link

mkaag commented Jun 29, 2015

Hi @paulbz, it appears that the content of tweet.user.profile_image_url_https has changed and does now contains more data. I have modified the script like that:

avatar: tweet.user.profile_image_url_https.scheme + "://" + tweet.user.profile_image_url_https.host + tweet.user.profile_image_url_https.path

Hope it helps.

@OliPassey
Copy link

Has something changed recently?
Was originally getting Cannot authenticate errors on the Twitter Widget that came with dashing, switched to your version. now getting this error:

twitter-search.rb:3:in <top (required)>': undefined methodconfigure' for Twitter:Module (NoMethodError)

And dashing fails to start - any ideas?

@herdir
Copy link

herdir commented Sep 16, 2016

I've the same problem as @OliPassey and can't find where it come from

@sdkibb
Copy link

sdkibb commented Mar 1, 2017

gem 'twitter' doesn't work. Is this project outdated and not functional or did it ever work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment