diff --git a/.github/workflows/tweet.yml b/.github/workflows/tweet.yml new file mode 100644 index 0000000..6b168be --- /dev/null +++ b/.github/workflows/tweet.yml @@ -0,0 +1,26 @@ +name: tweet + +on: + schedule: + - cron: '42 10 * * *' # Tweet every 18:42 (GMT +8) + +jobs: + build-deploy: + runs-on: ubuntu-18.04 + strategy: + matrix: + node-version: [8] + + steps: + - uses: actions/checkout@v1 + - name: Setup ruby + uses: eregon/use-ruby-action@master + with: + ruby-version: 2.7 + - name: Install Dependencies + run: | + gem install bundler + bundle install --jobs 4 --retry 3 + - name: Build + run: | + ruby relay.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..63f9948 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'twitter', '~> 7.0.0' +gem 'faraday', '~> 1.0.0' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..098e038 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,58 @@ +GEM + remote: https://rubygems.org/ + specs: + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + buftok (0.2.0) + domain_name (0.5.20190701) + unf (>= 0.0.5, < 1.0.0) + equalizer (0.0.11) + faraday (1.0.1) + multipart-post (>= 1.2, < 3) + ffi (1.13.1) + ffi-compiler (1.0.1) + ffi (>= 1.0.0) + rake + http (4.4.1) + addressable (~> 2.3) + http-cookie (~> 1.0) + http-form_data (~> 2.2) + http-parser (~> 1.2.0) + http-cookie (1.0.3) + domain_name (~> 0.5) + http-form_data (2.3.0) + http-parser (1.2.2) + ffi-compiler + http_parser.rb (0.6.0) + memoizable (0.4.2) + thread_safe (~> 0.3, >= 0.3.1) + multipart-post (2.1.1) + naught (1.1.0) + public_suffix (4.0.6) + rake (13.0.1) + simple_oauth (0.3.1) + thread_safe (0.3.6) + twitter (7.0.0) + addressable (~> 2.3) + buftok (~> 0.2.0) + equalizer (~> 0.0.11) + http (~> 4.0) + http-form_data (~> 2.0) + http_parser.rb (~> 0.6.0) + memoizable (~> 0.4.0) + multipart-post (~> 2.0) + naught (~> 1.0) + simple_oauth (~> 0.3.0) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.7) + +PLATFORMS + ruby + +DEPENDENCIES + faraday (~> 1.0.0) + twitter (~> 7.0.0) + +BUNDLED WITH + 2.1.4 diff --git a/relay.rb b/relay.rb new file mode 100644 index 0000000..c4fac13 --- /dev/null +++ b/relay.rb @@ -0,0 +1,36 @@ +require 'faraday' +require 'twitter' +require 'json' +require 'date' + +client = Twitter::REST::Client.new do |config| + config.consumer_key = ENV['CONSUMER_KEY'] + config.consumer_secret = ENV['CONSUMER_SECRET'] + config.bearer_token = ENV['BEARER_TOKEN'] + config.access_token = ENV['ACCESS_TOKEN'] + config.access_token_secret = ENV['ACCESS_TOKEN_SECRET'] +end + +INTERVAL = 24 * 60 * 60 # 1 day + +resp = Faraday.get( + 'https://ruby-china.org/api/v3/topics', + { type: 'excellent' }, + { 'Accept': 'application/json' } +) + +topics = JSON.parse(resp.body)['topics'] + +filtered = topics.reject do |topic| + Time.now - DateTime.parse(topic['created_at']).to_time > INTERVAL +end + +formatted = filtered.map do |topic| + "《#{topic['title']}》作者:#{topic['user']['name']}\n\nhttps://ruby-china.org/topics/#{topic['id']}\n\n(本消息由 Ruby China 机器人自动发送)" +end + +formatted.each do |tweet| + client.update(tweet) +end + +puts 'OK!'