85 lines
2.8 KiB
Ruby
85 lines
2.8 KiB
Ruby
require 'erb' # for url_encode()
|
|
require 'net/http'
|
|
require 'uri'
|
|
|
|
class RepostsController < ApplicationController
|
|
def index
|
|
config = YAML.load_file('config/client.yml')
|
|
client_id = config["client_id"]
|
|
client_secret = config["client_secret"]
|
|
|
|
# connect to ADN via a user token
|
|
uri = URI.parse("https://account.app.net/oauth/authenticate")
|
|
http = Net::HTTP.new(uri.host, uri.port)
|
|
http.use_ssl = true
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
|
|
response = Net::HTTP::post_form( uri, {"client_id" => client_id, "response_type" => "code", "redirect_uri" => "http://localhost:3000/", "scope" => "stream"})
|
|
|
|
# [temp] status
|
|
@res_code = response.code
|
|
@res_message = response.message
|
|
@res_class = response.class.name
|
|
|
|
if not response.is_a?(Net::HTTPSuccess)
|
|
flash[:error] = "Could not connect to ADN."
|
|
return
|
|
end
|
|
|
|
# ...
|
|
end
|
|
|
|
# TODO: to secure the cronjob call test for 'InvokationPassword' header.
|
|
# store this password in client.yml for comparison
|
|
# crontab: curl -H 'InvokationPassword: ...' http://localhost/reposts/new
|
|
def new
|
|
# get user auth token
|
|
access_token = Repost.obtain_user_token
|
|
|
|
# retrieve latest reposts. this uses the real ADN stream.
|
|
puts "===< retrieve latest post >==="
|
|
res = Repost.get_most_recent_user_post(access_token)
|
|
api_res = ApiResponse.new(:raw_json => res.body)
|
|
latest_post = PostData.most_recent_post(api_res.data)
|
|
puts "=== latest postid: " + latest_post.post_id.to_s + ", created at: " + latest_post.created_at.to_s
|
|
|
|
# retrieve new mentions.
|
|
# TODO restrict to: fetch( since_id=previous max_id )
|
|
puts "===< retrieve new mentions >==="
|
|
res = Repost.get_mentions_stream(access_token)
|
|
api_res = ApiResponse.new(:raw_json => res.body)
|
|
todo_posts_array = PostData.extract_not_yet_reposted(api_res.data, latest_post.post_id)
|
|
|
|
# tmp
|
|
puts "===> " + todo_posts_array.length.to_s + " noch offene posts."
|
|
todo_posts_array.each do |post_id|
|
|
puts "--- reposten: " + post_id.to_s
|
|
end
|
|
|
|
# TODO: res.body contains the mentions in JSON. Proceed from there.
|
|
#new_mentions.each do | post |
|
|
## API - repost(post) # http://developers.app.net/docs/resources/post/reposts/
|
|
#repost_uri = "https://alpha-api.app.net/stream/0/posts/" + post.id + "/repost"
|
|
#latestid = post.id
|
|
#end
|
|
|
|
# store all reposts locally for later reuse.
|
|
# TODO insert into reposts values("karlsruher", post.id, latestid)
|
|
end
|
|
|
|
private
|
|
|
|
def dump_api_response(api_response)
|
|
puts "====> meta:"
|
|
api_response.meta.each do |mline|
|
|
puts "--- meta line: " + mline.to_s
|
|
puts ""
|
|
end
|
|
puts ""
|
|
puts "====> data:"
|
|
api_response.data.each do |dline|
|
|
puts "--- data line: " + dline.to_s
|
|
puts ""
|
|
end
|
|
end
|
|
end
|