Recoding My RSS Twitter Bot

My last blog post about my RSS Twitter Bot made realize a lot of flaws with my bot, not only in the code I wrote but the choices I made about what to code. This led me to re-code the entire bot, but I think the code is much cleaner now.

Recoded RSS Twitter Bot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'rss'
require 'open-uri'
require 'twitter'
require 'yaml'

class FeedParser
  def initialize(*feeds)
      @feeds = feeds
      @tweet = ""
      @pub_date = ""
      @tweet_length = Proc.new{
          if @tweet.length > 140
              next
          else
              tweeter(@tweet)
              puts "tweeted"

              sleep(15)
          end
      }

      config = YAML.load_file('config.yml')

      @client = Twitter::REST::Client.new({
              consumer_key: config['consumer_key'],
              consumer_secret: config['consumer_secret'],
              access_token: config['access_token'],
              access_token_secret: config['access_token_secret']
      })
      
  end

  def run
      feed_sorter
  end

# method that sorts feeds
  def feed_sorter
      @feeds.each do |url|
          open(url) do |rss|
              feed = RSS::Parser.parse(rss)
          
              case
              when feed.feed_type == "rss"
                  rss_parser(feed)
              when feed.feed_type == "atom"
                  atom_parser(feed)
              end
          end
      end
  end

  def rss_parser(rss)
      rss.items.each do |item|
          title = item.title
          link = item.link
          @tweet = "#{title} #{link}"
          @tweet_length.call
          
      end
  end


  def atom_parser(atom)
      atom.items.each do |item|
          title = item.title.content
          link = item.link.href
          @tweet = "#{title} #{link}"
          @tweet_length.call

      end
  end

  def tweeter(tweet)
      @client.update(tweet)
  end
end


parser = FeedParser.new('https://gdata.youtube.com/feeds/api/users/goaztecscom/uploads', 'http://www.utsandiego.com/rss/headlines/sports/sdsu-aztecs/')
parser.run

I Switched to the RSS Module

During and after writing the first post about my Twitter Bot I did more research into the Ruby RSS Module and found that it has .feed_type method that allows you to easily sort the types of feeds so you can use the correct methods to parse them. Sorting the feeds this way is infinitely better than the weird/incorrect way I was doing it before albeit it was good practice with hashes.

Sorting Feeds with the RSS Module
1
2
3
4
5
6
7
8
9
10
11
12
@feeds.each do |url|
  open(url) do |rss|
      feed = RSS::Parser.parse(rss)
          
      case
      when feed.feed_type == "rss"
          rss_parser(feed)
      when feed.feed_type == "atom"
          atom_parser(feed)
      end
  end
end

I Finally Used a Proc in my Code

I remember learning about Procs and Lamdas on Codecademy and thinking they were pretty cool…ok really cool. Since that time I haven’t used them in my code, accept when trying to code FizzBuzz in as many different ways as possible, which sort of makes sense since everything I’m doing is super simple, but still…I want to use a some damn Procs.

Finally, I found a reason. When re-coding my bot I decided there was no reason to try and adjust the length of the feed titles because eventually my bot will hopefully use a URL shortener, meaning 99.99% of the time the titles will never make the tweet go over 140 characters. Still I needed an easy way to keep @tweets that are over 140 characters from being sent to the .tweeter method and causing an error. Obviously, I could stick an if/else statement in a method and call it from the <.rss> and <.atom> methods, but using a Proc sounded way more fun :).

Using a Proc
1
2
3
4
5
6
7
8
9
@tweet_length = Proc.new{
if @tweet.length > 140
      next
  else
      tweeter(@tweet)
      puts "tweeted"
      sleep(15)
  end
}

Next Steps

  1. Check for new articles, videos, etc. in the feeds and only tweet the new ones
  2. Learn Sinatra and turn this into a Sinatra App
  3. Add a url shortner like bit.ly or roll my own

Copyright © 2015 - Kyle Doherty. Powered by Octopress