Skip to content

Commit

Permalink
Init the project
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh0416 committed Nov 18, 2020
1 parent d57a2a6 commit 9e1e7cd
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tweet.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'twitter', '~> 7.0.0'
gem 'faraday', '~> 1.0.0'
58 changes: 58 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions relay.rb
Original file line number Diff line number Diff line change
@@ -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!'

0 comments on commit 9e1e7cd

Please sign in to comment.