Last active
May 9, 2017 20:52
-
-
Save jage/4d09f0518ac56346e4e3d48df95ff1e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remember to set the environment variable key `TWINGLY_API_KEY` | |
require "net/http" | |
require "time" | |
require "rexml/document" | |
BASE_URL = "https://api.twingly.com/blog/livefeed/api/v5/GetData" | |
params = { | |
timestamp: (Time.now - 3600).utc.iso8601, # Start one hour ago | |
apikey: ENV["TWINGLY_API_KEY"], # API key to access Blog LiveFeed | |
} | |
loop do | |
uri = URI(BASE_URL) | |
uri.query = URI.encode_www_form(params) | |
Net::HTTP.get_response(uri) do |result| | |
# No error handling code in this example, just stop | |
break unless result.is_a?(Net::HTTPSuccess) | |
document = REXML::Document.new(result.body) | |
# Extract post URLs, print to stdout | |
document.elements.each("twinglydata/post/url") do |url_element| | |
puts url_element.text | |
end | |
# Extract helper attributes to control the polling | |
attributes = document.root.attributes | |
next_timestamp = attributes["nextTimestamp"].value | |
number_of_posts = attributes["numberOfPosts"].value.to_i | |
max_number_of_posts = attributes["maxNumberOfPosts"].value.to_i | |
# Update timestamp for next iteration | |
params[:timestamp] = next_timestamp | |
# Sleep a while if there's no more documents available | |
sleep 60 if number_of_posts < max_number_of_posts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment