Getting Tweets from Twitter and displaying them in Discourse Topic without reposting the same again automated










0















i want to write an application, which achieves to pull tweets from twitter and sends them into a discourse topic.
So what i need:



A method which has a flex since datetime.
A method which pulls the tweets from twitter in an appropriate timeframe
A method which sends tweets to a selected Discourse Topic
A method which checks if the Tweets (or the Tweeturl) was posted before in the topic, to avoid getting the same tweets everytime
A possibility to automize the script running every 10 secs or something like that (probably sidekiq)



What i already got:






require 'bundler/setup'
require 'net/http'
require 'uri'
require 'twitter'
module GetTweet
class Error < StandardError
end

def self.date
date = Time.new
#set 'date' equal to the current date/time.
date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
#Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
end

$tweeturls = Array.new

def self.catch_tweets
client = Twitter::REST::Client.new do |config|
config.consumer_key = "KAKAKAKAKAKAKAA"
config.consumer_secret = "jKAKAKAKAKAKAKAA"
config.access_token = "9KAKAKAKAKAKAKAA"
config.access_token_secret = "KAKAKAKAKAKAKAA"
end
# getting all tweets in a hashmap with "mention" without retweet in option timeframe
tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
# put the hashmap into an sorted array
sorted_tweets = tweets.sort_by tweet
# give me every uniq tweet
sorted_tweets.uniq.each do |tweet|
tweet = tweet.url
if $tweeturls.include?(tweet)
else
$tweeturls.push(tweet)
end
end
end
catch_tweets
puts $tweeturls
end





So tweeturls is supposed to be an array which all tweet urls saved in it
and that seems to work fine!






require 'net/http'
require 'uri'
require_relative 'get_tweet'

INSTANCE_URL = 'XXXXX'
API_USERNAME = 'XXXX'
API_KEY = 'XXXX'

def self.send_request

def self.tweet_url
$tweeturls.each do |tweet|
@tweet = tweet.
end
end

url = URI.parse(INSTANCE_URL)
request = Net::HTTP::Post.new(url.path)
request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
response = http.request(request)

puts response.code

end

send_request





Thats working as well, okay i guess. At the moment i get more than only the URL of the tweet by calling the method, which is unfortunate, but i think fixable. My biggest concern is, that i have to automate the script (will be a plugin later) and make sure that its not getting the old tweets again and again which are in the timeframe. I could make something like a Time.now - but i think the TwitterApi isnt that accurate that this would work properly right? Or i create a third method to get all posts from discourse topic and compare the arrays and merge them into an uniq one. But i am pretty sure that wouldnt be a wise solution, for many reasons.
I would love to hear suggestions how i could make that better!










share|improve this question


























    0















    i want to write an application, which achieves to pull tweets from twitter and sends them into a discourse topic.
    So what i need:



    A method which has a flex since datetime.
    A method which pulls the tweets from twitter in an appropriate timeframe
    A method which sends tweets to a selected Discourse Topic
    A method which checks if the Tweets (or the Tweeturl) was posted before in the topic, to avoid getting the same tweets everytime
    A possibility to automize the script running every 10 secs or something like that (probably sidekiq)



    What i already got:






    require 'bundler/setup'
    require 'net/http'
    require 'uri'
    require 'twitter'
    module GetTweet
    class Error < StandardError
    end

    def self.date
    date = Time.new
    #set 'date' equal to the current date/time.
    date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
    #Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
    end

    $tweeturls = Array.new

    def self.catch_tweets
    client = Twitter::REST::Client.new do |config|
    config.consumer_key = "KAKAKAKAKAKAKAA"
    config.consumer_secret = "jKAKAKAKAKAKAKAA"
    config.access_token = "9KAKAKAKAKAKAKAA"
    config.access_token_secret = "KAKAKAKAKAKAKAA"
    end
    # getting all tweets in a hashmap with "mention" without retweet in option timeframe
    tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
    # put the hashmap into an sorted array
    sorted_tweets = tweets.sort_by tweet
    # give me every uniq tweet
    sorted_tweets.uniq.each do |tweet|
    tweet = tweet.url
    if $tweeturls.include?(tweet)
    else
    $tweeturls.push(tweet)
    end
    end
    end
    catch_tweets
    puts $tweeturls
    end





    So tweeturls is supposed to be an array which all tweet urls saved in it
    and that seems to work fine!






    require 'net/http'
    require 'uri'
    require_relative 'get_tweet'

    INSTANCE_URL = 'XXXXX'
    API_USERNAME = 'XXXX'
    API_KEY = 'XXXX'

    def self.send_request

    def self.tweet_url
    $tweeturls.each do |tweet|
    @tweet = tweet.
    end
    end

    url = URI.parse(INSTANCE_URL)
    request = Net::HTTP::Post.new(url.path)
    request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    response = http.request(request)

    puts response.code

    end

    send_request





    Thats working as well, okay i guess. At the moment i get more than only the URL of the tweet by calling the method, which is unfortunate, but i think fixable. My biggest concern is, that i have to automate the script (will be a plugin later) and make sure that its not getting the old tweets again and again which are in the timeframe. I could make something like a Time.now - but i think the TwitterApi isnt that accurate that this would work properly right? Or i create a third method to get all posts from discourse topic and compare the arrays and merge them into an uniq one. But i am pretty sure that wouldnt be a wise solution, for many reasons.
    I would love to hear suggestions how i could make that better!










    share|improve this question
























      0












      0








      0








      i want to write an application, which achieves to pull tweets from twitter and sends them into a discourse topic.
      So what i need:



      A method which has a flex since datetime.
      A method which pulls the tweets from twitter in an appropriate timeframe
      A method which sends tweets to a selected Discourse Topic
      A method which checks if the Tweets (or the Tweeturl) was posted before in the topic, to avoid getting the same tweets everytime
      A possibility to automize the script running every 10 secs or something like that (probably sidekiq)



      What i already got:






      require 'bundler/setup'
      require 'net/http'
      require 'uri'
      require 'twitter'
      module GetTweet
      class Error < StandardError
      end

      def self.date
      date = Time.new
      #set 'date' equal to the current date/time.
      date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
      #Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
      end

      $tweeturls = Array.new

      def self.catch_tweets
      client = Twitter::REST::Client.new do |config|
      config.consumer_key = "KAKAKAKAKAKAKAA"
      config.consumer_secret = "jKAKAKAKAKAKAKAA"
      config.access_token = "9KAKAKAKAKAKAKAA"
      config.access_token_secret = "KAKAKAKAKAKAKAA"
      end
      # getting all tweets in a hashmap with "mention" without retweet in option timeframe
      tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
      # put the hashmap into an sorted array
      sorted_tweets = tweets.sort_by tweet
      # give me every uniq tweet
      sorted_tweets.uniq.each do |tweet|
      tweet = tweet.url
      if $tweeturls.include?(tweet)
      else
      $tweeturls.push(tweet)
      end
      end
      end
      catch_tweets
      puts $tweeturls
      end





      So tweeturls is supposed to be an array which all tweet urls saved in it
      and that seems to work fine!






      require 'net/http'
      require 'uri'
      require_relative 'get_tweet'

      INSTANCE_URL = 'XXXXX'
      API_USERNAME = 'XXXX'
      API_KEY = 'XXXX'

      def self.send_request

      def self.tweet_url
      $tweeturls.each do |tweet|
      @tweet = tweet.
      end
      end

      url = URI.parse(INSTANCE_URL)
      request = Net::HTTP::Post.new(url.path)
      request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      response = http.request(request)

      puts response.code

      end

      send_request





      Thats working as well, okay i guess. At the moment i get more than only the URL of the tweet by calling the method, which is unfortunate, but i think fixable. My biggest concern is, that i have to automate the script (will be a plugin later) and make sure that its not getting the old tweets again and again which are in the timeframe. I could make something like a Time.now - but i think the TwitterApi isnt that accurate that this would work properly right? Or i create a third method to get all posts from discourse topic and compare the arrays and merge them into an uniq one. But i am pretty sure that wouldnt be a wise solution, for many reasons.
      I would love to hear suggestions how i could make that better!










      share|improve this question














      i want to write an application, which achieves to pull tweets from twitter and sends them into a discourse topic.
      So what i need:



      A method which has a flex since datetime.
      A method which pulls the tweets from twitter in an appropriate timeframe
      A method which sends tweets to a selected Discourse Topic
      A method which checks if the Tweets (or the Tweeturl) was posted before in the topic, to avoid getting the same tweets everytime
      A possibility to automize the script running every 10 secs or something like that (probably sidekiq)



      What i already got:






      require 'bundler/setup'
      require 'net/http'
      require 'uri'
      require 'twitter'
      module GetTweet
      class Error < StandardError
      end

      def self.date
      date = Time.new
      #set 'date' equal to the current date/time.
      date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
      #Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
      end

      $tweeturls = Array.new

      def self.catch_tweets
      client = Twitter::REST::Client.new do |config|
      config.consumer_key = "KAKAKAKAKAKAKAA"
      config.consumer_secret = "jKAKAKAKAKAKAKAA"
      config.access_token = "9KAKAKAKAKAKAKAA"
      config.access_token_secret = "KAKAKAKAKAKAKAA"
      end
      # getting all tweets in a hashmap with "mention" without retweet in option timeframe
      tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
      # put the hashmap into an sorted array
      sorted_tweets = tweets.sort_by tweet
      # give me every uniq tweet
      sorted_tweets.uniq.each do |tweet|
      tweet = tweet.url
      if $tweeturls.include?(tweet)
      else
      $tweeturls.push(tweet)
      end
      end
      end
      catch_tweets
      puts $tweeturls
      end





      So tweeturls is supposed to be an array which all tweet urls saved in it
      and that seems to work fine!






      require 'net/http'
      require 'uri'
      require_relative 'get_tweet'

      INSTANCE_URL = 'XXXXX'
      API_USERNAME = 'XXXX'
      API_KEY = 'XXXX'

      def self.send_request

      def self.tweet_url
      $tweeturls.each do |tweet|
      @tweet = tweet.
      end
      end

      url = URI.parse(INSTANCE_URL)
      request = Net::HTTP::Post.new(url.path)
      request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      response = http.request(request)

      puts response.code

      end

      send_request





      Thats working as well, okay i guess. At the moment i get more than only the URL of the tweet by calling the method, which is unfortunate, but i think fixable. My biggest concern is, that i have to automate the script (will be a plugin later) and make sure that its not getting the old tweets again and again which are in the timeframe. I could make something like a Time.now - but i think the TwitterApi isnt that accurate that this would work properly right? Or i create a third method to get all posts from discourse topic and compare the arrays and merge them into an uniq one. But i am pretty sure that wouldnt be a wise solution, for many reasons.
      I would love to hear suggestions how i could make that better!






      require 'bundler/setup'
      require 'net/http'
      require 'uri'
      require 'twitter'
      module GetTweet
      class Error < StandardError
      end

      def self.date
      date = Time.new
      #set 'date' equal to the current date/time.
      date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
      #Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
      end

      $tweeturls = Array.new

      def self.catch_tweets
      client = Twitter::REST::Client.new do |config|
      config.consumer_key = "KAKAKAKAKAKAKAA"
      config.consumer_secret = "jKAKAKAKAKAKAKAA"
      config.access_token = "9KAKAKAKAKAKAKAA"
      config.access_token_secret = "KAKAKAKAKAKAKAA"
      end
      # getting all tweets in a hashmap with "mention" without retweet in option timeframe
      tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
      # put the hashmap into an sorted array
      sorted_tweets = tweets.sort_by tweet
      # give me every uniq tweet
      sorted_tweets.uniq.each do |tweet|
      tweet = tweet.url
      if $tweeturls.include?(tweet)
      else
      $tweeturls.push(tweet)
      end
      end
      end
      catch_tweets
      puts $tweeturls
      end





      require 'bundler/setup'
      require 'net/http'
      require 'uri'
      require 'twitter'
      module GetTweet
      class Error < StandardError
      end

      def self.date
      date = Time.new
      #set 'date' equal to the current date/time.
      date = date.year.to_s + "-" + date.month.to_s + "-" + (date.day - 1).to_s
      #Without this it will output 2015-01-10 11:33:05 +0000; this formats it to display DD/MM/YYYY
      end

      $tweeturls = Array.new

      def self.catch_tweets
      client = Twitter::REST::Client.new do |config|
      config.consumer_key = "KAKAKAKAKAKAKAA"
      config.consumer_secret = "jKAKAKAKAKAKAKAA"
      config.access_token = "9KAKAKAKAKAKAKAA"
      config.access_token_secret = "KAKAKAKAKAKAKAA"
      end
      # getting all tweets in a hashmap with "mention" without retweet in option timeframe
      tweets = client.search("Bastiilange -rt", since: "#date", until: "2018-11-15")
      # put the hashmap into an sorted array
      sorted_tweets = tweets.sort_by tweet
      # give me every uniq tweet
      sorted_tweets.uniq.each do |tweet|
      tweet = tweet.url
      if $tweeturls.include?(tweet)
      else
      $tweeturls.push(tweet)
      end
      end
      end
      catch_tweets
      puts $tweeturls
      end





      require 'net/http'
      require 'uri'
      require_relative 'get_tweet'

      INSTANCE_URL = 'XXXXX'
      API_USERNAME = 'XXXX'
      API_KEY = 'XXXX'

      def self.send_request

      def self.tweet_url
      $tweeturls.each do |tweet|
      @tweet = tweet.
      end
      end

      url = URI.parse(INSTANCE_URL)
      request = Net::HTTP::Post.new(url.path)
      request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      response = http.request(request)

      puts response.code

      end

      send_request





      require 'net/http'
      require 'uri'
      require_relative 'get_tweet'

      INSTANCE_URL = 'XXXXX'
      API_USERNAME = 'XXXX'
      API_KEY = 'XXXX'

      def self.send_request

      def self.tweet_url
      $tweeturls.each do |tweet|
      @tweet = tweet.
      end
      end

      url = URI.parse(INSTANCE_URL)
      request = Net::HTTP::Post.new(url.path)
      request.set_form_data('api_username' => API_USERNAME, 'api_key' => API_KEY, 'title' => "Neuer Post", 'topic_id' => 8, 'raw' => "#tweet_url")
      http = Net::HTTP.new(url.host, url.port)
      http.use_ssl = true
      response = http.request(request)

      puts response.code

      end

      send_request






      ruby twitter discourse






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 23:12









      SebastianSebastian

      216




      216






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290883%2fgetting-tweets-from-twitter-and-displaying-them-in-discourse-topic-without-repos%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290883%2fgetting-tweets-from-twitter-and-displaying-them-in-discourse-topic-without-repos%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo