Skip to content

Commit

Permalink
Add log tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh0416 committed Nov 23, 2020
1 parent be074b5 commit e72c2fa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/tweet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ jobs:
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Build
- name: Tweet
run: |
ruby relay.rb
- name: Commit files
id: commit
run: |
git config --local user.email "[email protected]"
git config --local user.name "github-actions"
git add --all
if [-z "$(git status --porcelain)"]; then
echo "::set-output name=push::false"
else
git commit -m "Update Tweets" -a
echo "::set-output name=push::true"
fi
shell: bash
- name: Push changes
if: steps.commit.outputs.push == 'true'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: main
19 changes: 19 additions & 0 deletions finished.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
40583
40481
40471
40414
40354
40316
40300
40273
40201
40177
39925
39890
39859
39846
39820
39735
39711
39628
39574
20 changes: 7 additions & 13 deletions relay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "json"
require "date"

finished = File.readlines("finished.log").map(&:chomp).map(&:to_i)

client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
Expand All @@ -11,41 +13,33 @@
config.access_token_secret = ENV["ACCESS_TOKEN_SECRET"]
end

INTERVAL = 2 * 60 * 60 # every 2 hours

def get_user(login)
resp = Faraday.get("https://ruby-china.org/api/v3/users/#{login}.json")
JSON.parse(resp.body)["user"]
rescue StandardError
nil
end

def get_excellent_time(topic)
resp = Faraday.get("https://ruby-china.org/api/v3/topics/#{topic['id']}/replies.json")
actions = JSON.parse(resp.body)["replies"].select do |reply|
reply["action"] == "excellent"
end
return nil if actions.empty?
DateTime.parse(actions.first["created_at"]).to_time
end

resp = Faraday.get(
"https://ruby-china.org/api/v3/topics.json",
{ type: "excellent" }
)

topics = JSON.parse(resp.body)["topics"]

filtered = topics.select do |topic|
Time.now - get_excellent_time(topic) < INTERVAL
filtered = topics.reject do |topic|
finished.include? topic["id"]
end

formatted = filtered.map do |topic|
finished << topic["id"]
user = get_user(topic["user"]["login"]) || {}
user_name = user["twitter"].to_s != "" ? "@#{user['twitter']}" : topic["user"]["login"]
"#{topic['title']} by #{user_name}\n\nhttps://ruby-china.org/topics/#{topic['id']}"
end

File.write("finished.log", finished.join("\n"), mode: 'w')

formatted.each do |tweet|
client.update(tweet)
end
Expand Down

0 comments on commit e72c2fa

Please sign in to comment.