Last active
July 5, 2020 05:11
-
-
Save lnanase/0acb17547b323a6648dc4fe9be1e6a0e to your computer and use it in GitHub Desktop.
ミリシタ3周年イベント「CHALLENGE FOR GLOW-RY D@YS!!!」のランキングボーダー画像を送信する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# imastodonにミリシタ3周年イベントのランキング画像を送信する | |
# https://twitter.com/imas_ml_td_a | |
# | |
# usage: push_imastodon_ml_3rd_ranking.rb | |
require 'rubygems' | |
require 'bundler' | |
Bundler.require(:default) | |
Dotenv.load | |
# CONST | |
TARGET_USERNAME = 'imas_ml_td_a' | |
IMAGE_SAVE_PATH = '/tmp/3rd_ranking' | |
UA = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4200.0 Iron Safari/537.36' #Chrome | |
# cleaning | |
FileUtils.rm(Dir.glob("#{IMAGE_SAVE_PATH}/*")) | |
client = Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] | |
config.access_token = ENV['TWITTER_ACCESS_TOKEN'] | |
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET'] | |
end | |
# ミリシタ周年イベントランキングBotから取得 | |
body = '' | |
tweet_uri = '' | |
client.user_timeline(TARGET_USERNAME, { count: 1 } ).each do |timeline| | |
tweet = client.status(timeline.id) | |
body = tweet.text | |
tweet_uri = tweet.uri.to_s | |
unless tweet.media.empty? then | |
tweet.media.map {|m| m.media_url.to_s}.each_with_index do |img_url, i| | |
p img_url | |
if i > 3 then | |
break | |
end | |
# 40%にリサイズ | |
image = MiniMagick::Image.open(img_url) | |
image.resize "1152x1088" | |
image.write "#{IMAGE_SAVE_PATH}/#{i}.jpg" | |
end | |
end | |
end | |
p body | |
body_split = body.split('。') | |
# imastodon | |
agent = Mechanize.new | |
agent.user_agent = UA | |
agent.request_headers = {'accept-language' => 'ja, ja-JP', 'accept-encoding' => 'utf-8'} | |
agent.request_headers = {'Authorization' => 'Bearer ' + ENV['TB_RANKING_BEARER']} | |
# image | |
image_ids = [] | |
Dir.glob("#{IMAGE_SAVE_PATH}/{0,1,2,3}.jpg") {|f| | |
p f | |
res = agent.post(ENV['MASTODON_HOST'] + 'api/v1/media', { | |
'file': File.open(f) | |
}) | |
hash = JSON.parse(res.body) | |
image_ids << hash['id'] | |
} | |
p image_ids | |
# toot | |
res = agent.post(ENV['MASTODON_HOST'] + 'api/v1/statuses', { | |
'status': body_split[0] << "\n元ツイート: " << tweet_uri, | |
'spoiler_text': 'ミリシタ3周年イベントランキングです', | |
'media_ids[]': image_ids, | |
'visibility': 'public' | |
}) | |
p res.code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment