-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.rb
90 lines (82 loc) · 2.21 KB
/
tweet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'rubygems'
require 'twitter'
require 'bitly'
load("./hotentry_star.rb")
class Tweet
attr_accessor :tweets
def initialize
@cli = Twitter::REST::Client.new do |config|
config.consumer_key = '******************************'
config.consumer_secret = '******************************'
config.access_token = '******************************'
config.access_token_secret = '******************************'
end
@tweets = make_tweets
end
def get_short_url(page_url)
Bitly.use_api_version_3
bitly = Bitly.new('************', '************************************')
bitly.shorten(page_url).short_url
end
def get_comment(user,url)
enrty_url = URI.escape(url)
entry_json = nil
open('http://b.hatena.ne.jp/entry/jsonlite/?url='+enrty_url) do |uri|
entry_json = JSON.parse(uri.read)
end
entry_json["bookmarks"].each do |bookmark|
if bookmark["user"] == user
return bookmark["comment"]
end
end
end
def get_infos
ranking = Hotentry.new.ranking
infos = {}
num = 1
ranking.each do |ar|
infos[num] = {}
infos[num][:user] = ar[0].match(%r{http://b.hatena.ne.jp/(.+)/})[1]
infos[num][:score] = ar[1]
infos[num][:url] = ar[2]
infos[num][:comment] = get_comment(infos[num][:user],ar[2])
num += 1
end
infos
end
def make_tweets
tweets = []
hour = Time.now.hour
self.get_infos.each do |num,hash|
tweet = "#{hour}時の#{num}位(#{hash[:score]}pts)#{hash[:user]}:#{hash[:comment]}"
shorten = get_short_url(hash[:url])
if tweet.length+shorten.length+1 <= 140
tweet = tweet[0...(140-(shorten.length+1))]
else #"…… "の分
tweet = tweet[0...(140-(shorten.length+5))]
tweet << "……"
end
tweet << " " << shorten
tweets << tweet
end
tweets
end
def hourly_tweet
Tweet.new.tweets.reverse.each do |tweet| #10位からツイートしたいのでreverse
update(tweet)
end
end
def test
t = Time.now.to_s
update(t)
end
private
def update(tweet)
return nil unless tweet
begin
@cli.update(tweet.chomp)
rescue => ex
nil
end
end
end