-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeitanbot.rb
1397 lines (1270 loc) · 47.7 KB
/
meitanbot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#coding:utf-8
require 'net/https'
require 'oauth'
require 'json'
require 'psych'
require 'yaml'
require 'thread'
require 'rexml/document'
require 'kconv'
require 'sqlite3'
require 'date'
require 'MeCab'
class User
attr_reader :id, :screen_name
def initialize(id, screen_name)
@id = id
@screen_name = screen_name
end
end
class Tweet
attr_reader :id, :text, :user, :other
def initialize(id, text, user, other = nil)
@id = id
@text = text
@user = user
if other
raise ArgumentError unless other.is_a?(Hash)
@other = other
end
end
end
class CurrentWeather
attr_reader :condition, :temp, :humidity, :wind
def initialize(condition, temp, humidity, wind)
@condition = condition
@temp = temp
@humidity = humidity.gsub('湿度 : ', '')
@wind = wind.gsub('風: ', '')
end
end
class ForecastWeather
attr_reader :condition, :day_of_week, :low, :high
def initialize(day_of_week, condition, low, high)
@day_of_week = day_of_week
@condition = condition
@low = Integer(low)
@high = Integer(high)
end
end
class MeitanBot
class Config
def Config.load(path)
c = new()
c.instance_eval File.read(path)
c
end
attr_accessor :max_continuative_retry_count
attr_accessor :short_retry_interval, :long_retry_interval
attr_accessor :reply_limit_reset_time, :reply_limit_threshold
attr_accessor :sleep_when_forbidden_time
attr_accessor :stat_interval
attr_accessor :stat_file_prefix, :log_file_prefix
attr_accessor :tsukuba_time_table
attr_accessor :ignore_id_file
attr_accessor :random_post_interval, :word_replace_probability
attr_accessor :min_word_length, :min_status_length
attr_accessor :num_of_word, :num_of_users_word, :num_of_others_word
attr_accessor :max_pending_word, :word_remain_after_delete
end
class StatTypes
STARTUP = 'START'
STAT = 'STATS'
NORMAL = 'NORML'
ERROR = 'ERROR'
end
# DateTime format from Twitter API
TWITTER_DATETIME_FORMAT = '%a %b %d %H:%M:%S +0000 %Y'
# DateTime format to output to DB
DB_DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
# YAML-File including credential data
CREDENTIAL_FILE = 'credential.yaml'
# Text for replying "not-meitan"
NOTMEITAN_FILE = 'notmeitan.txt'
# Text for replying mentions
MENTION_FILE = 'reply_mention.txt'
# Text for replying "C#"
REPLY_CSHARP_FILE = 'reply_csharp.txt'
# Text for replying morning greeting
REPLY_MORNING_FILE = 'reply_morning.txt'
# Text for replying departure posts
REPLY_DEPARTURE_FILE = 'reply_departure.txt'
# Text for replying returning posts
REPLY_RETURN_FILE = 'reply_return.txt'
# Text for replying sleeping posts
REPLY_SLEEPING_FILE = 'reply_sleeping.txt'
# HTTPS Certificate file
HTTPS_CA_FILE = 'certificate.crt'
# Forecast API URL
FORECAST_API_URL = 'http://www.google.com/ig/api'
# Forecast location
FORECAST_LOCATION = 'tsukuba,ibaraki'
# Screen name of this bot
SCREEN_NAME = 'meitanbot'
# User-Agent
BOT_USER_AGENT = 'Nowhere-type Meitan bot 1.0 by @maytheplic'
# Twitter ID of the owner of this bot
OWNER_ID = 246793872
# Twitter ID of this bot
MY_ID = 323080975
# Twitter IDs to ignore
IGNORE_IDS = []
# Database for post
POST_DATABASE_FILE = 'posts.db'
# Regular-Expression that represents replying
REPLY_REGEX = /^@[a-zA-Z0-9_]+ /
# String to use when create random string (use in create_random_string)
CRS_STR_FOR_CREATE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Initialize this class.
def initialize
# Queue for threads
@received_queue = Queue.new
@post_queue = Queue.new
@reply_queue = Queue.new
@retweet_queue = Queue.new
@event_queue = Queue.new
@message_queue = Queue.new
@log_queue = Queue.new
@recorder_queue = Queue.new
@replied_count = Hash.new
@pending_word = Hash.new
## Statistics
# Required time for status update request.
@tweet_request_time = Array.new
@statistics = {
tweet_request_time_average: 0.0,
post_received_count: 0,
event_received_count: 0,
message_received_count: 0,
reply_received_count: 0,
post_count: 0,
reply_count: 0,
send_message_count: 0,
total_retry_count: 0
}
## fields
# Ignores owner's post
@is_ignore_owner = true
# Enables posting
@is_enabled_posting = true
# Reports by direct message when an error occured.
@report_by_message_on_error = true
# load credential
open(CREDENTIAL_FILE) do |file|
@credential = YAML.load(file)
end
@consumer = OAuth::Consumer.new(
@credential['consumer_key'],
@credential['consumer_secret']
)
@access_token = OAuth::AccessToken.new(
@consumer,
@credential['access_token'],
@credential['access_token_secret']
)
load_config
read_post_text_files
# load ignore list
open(@config.ignore_id_file, 'r:UTF-8') do |file|
IGNORE_IDS << Integer(file.readline)
end
IGNORE_IDS.uniq!
end
def load_config
@config = Config.load('config')
end
def config
@config
end
def update_config(key, value)
if @config.method_defined?(key + '=')
@config.send(key + '=', value)
else
log("config key:#{key} is undefined.", StatTypes::ERROR)
end
end
# Connect to Twitter UserStream
def connect
uri = URI.parse("https://userstream.twitter.com/2/user.json?track=#{SCREEN_NAME}")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.ca_file = HTTPS_CA_FILE
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.verify_depth = 5
https.start do |https|
request = Net::HTTP::Get.new(uri.request_uri)
request["User-Agent"] = BOT_USER_AGENT
request.oauth!(https, @consumer, @access_token)
buf = ""
https.request(request) do |response|
response.read_body do |chunk|
buf << chunk
while ((line = buf[/.+?(\r\n)+/m]) != nil)
begin
buf.sub!(line, "")
line.strip!
status = JSON.parse(line)
rescue
break
end
yield status
end
end
end
end
end
def get_usertimeline
res = @access_token.get('https://api.twitter.com/1/statuses/home_timeline.json?count=100&trim_user=true&include_rts=false&exclude_replies=false')
JSON.parse(res.body)
end
# Start bot
def run
log('run!', StatTypes::STARTUP)
log_thread = Thread.new do
log 'log thread start'
loop do
s = @log_queue.pop
write_log s
end
end
recorder_thread = Thread.new do
log('recorder thread start', StatTypes::STARTUP)
loop do
json = @recorder_queue.pop
db = nil
# opening DB
begin
db = SQLite3::Database.new(POST_DATABASE_FILE)
rescue
error_log 'error in recorder_thread when opening DB'
log("Failed recording because opening DB was failed. Skipped tweet: #{json}", StatTypes::ERROR)
next
end
# recording post / word
begin
text = create_cleared_text json['text']
unless text.length < @config.min_status_length
time = DateTime.parse(json['created_at'], TWITTER_DATETIME_FORMAT)
mecab = MeCab::Tagger.new
node = mecab.parseToNode(text)
db.transaction do
db.execute('INSERT INTO posts VALUES(?, ?, ?, ?, ?, ?)', json['id'], json['user']['id'], json['in_reply_to_status_id'], time.strftime(DB_DATETIME_FORMAT), text, 0) # last element is post class. classfy!
while node
if node.stat == 2 or node.stat == 3 or (node.posid < 36 or 67 < node.posid)
node = node.next
next
end
if node.surface.length < @config.min_word_length
node = node.next
next
end
type = 1 # Type 1 means this word is noun.
if @pending_word.include?(node.surface)
@pending_word[node.surface.to_sym][:count] += 1
if @pending_word[node.surface.to_sym] >= @config.word_record_threshold
if db.execute('SELECT word FROM words WHERE word = ?', node.surface).size == 0
db.execute('INSERT INTO words VALUES(NULL, ?, ?, ?, ?, ?)', json['user']['id'], time.strftime(DB_DATETIME_FORMAT), type, node.surface, 0) # last element is word class. classify!
@post_queue.push ".oO(#{node.surface}) #meitan_record"
end
end
else
if db.execute('SELECT word FROM words WHERE word = ?', node.surface).size == 0
@pending_word[node.surface.to_sym] = {recorded_at: DateTime.now, count: 1}
if @pending_word.size > @config.max_pending_word
min = @pending_word.sort_by{|a| a[1][:recorded_at]}.reverse.take(@config.word_remain_after_delete).min_by{|a| a[1][:recorded_at]}[1][:recorded_at]
@pending_word.reject!{|k, v| v[:recorded_at] < min}
end
end
end
node = node.next
end
end # transaction end
end # end unless statement
rescue
error_log "error when recording. json: #{json}"
ensure
db.close
end
end
end
response_thread = Thread.new do
log('response thread start', StatTypes::STARTUP)
loop do
json = @received_queue.pop
user = json['user']
if /(^#meitanbot | #meitanbot$)/ =~ json['text'] and user['id'] == OWNER_ID
log('Owner update the status including meitanbot hash-tag.')
@retweet_queue.push json
end
unless IGNORE_IDS.include?(user['id'])
if /^@#{SCREEN_NAME} (今|明日|あさって)の天気を教えて$/=~ json['text']
log "Inquiry of weather detected. reply to #{json['id']}"
p $1
ahead = 0
case $1
when '今'
ahead = 0
when '明日'
ahead = 1
when 'あさって'
ahead = 2
end
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :weather, :ahead => ahead}))
next
elsif /^@#{SCREEN_NAME} ([1-6123456一二三四五六壱弐参伍])時{0,1}限目{0,1}(の時間を教えて)?$/ =~ json['text']
log "Inquiry of timetable detected. reply to #{json['id']}"
time = 0
case $1
when '1', '1', '一', '壱'
time = 1
when '2', '2', '二', '弐'
time = 2
when '3', '3', '三', '参'
time = 3
when '4', '4', '四'
time = 4
when '5', '5', '五', '伍'
time = 5
when '6', '6', '六'
time = 6
end
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :time_inquiry, :time => time}))
next
end # end of checking for replying text
@recorder_queue.push json
unless (@is_ignore_owner and user['id'] == OWNER_ID)
if /め[ ーえぇ]*い[ ーいぃ]*た[ ーあぁ]*ん/ =~ json['text'] or json['text'].include?('#mei_tan')
log "meitan detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :meitan}))
elsif /^@#{SCREEN_NAME}/ =~ json['text']
@statistics[:reply_received_count] += 1
log "reply detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :normal_reply}))
elsif /.*C#.*/ =~ json['text']
log "C# detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :csharp}))
elsif /あ[あぁ][.、…]*?(.*)ってそういう/ =~ json['text']
log "metaphor detected. reply to #{json['id']}"
word = $1
word = $! if /[(「【『[〈《〔{\[({<](.*)[)」】』]〉》〕}\])}>]」/ =~ word
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :metaphor, :word => word}))
time = DateTime.parse(json['created_at'], TWITTER_DATETIME_FORMAT)
db = nil
begin
db = SQLite3::Database.new(POST_DATABASE_FILE)
rescue
error_log 'error when opening DB to record metaphor word.'
end
if (db != nil)
begin
db.execute('INSERT INTO words VALUES(NULL, ?, ?, ?, ?, ?)', json['user']['id'], time.strftime(DB_DATETIME_FORMAT), 0, word, 0) # last element is word class. classify!
rescue
error_log 'error when recording metaphor word.'
ensure
db.close
end
end
elsif not REPLY_REGEX.match(json['text'])
if /(おはよ[うー]{0,1}(ございます|ございました){0,1})|(^(むくり|mkr)$)/ =~ json['text']
log "morning greeting detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :morning}))
end
if /(おやすみ(なさい)ー?)|(寝る)/ =~ json['text']
log "sleeping detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :sleeping}))
end
if /((い|行)ってきまー?すー?)|(いてきまー)|(出発)|(でっぱつ)/ =~ json['text']
log "departure detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :departure}))
end
if /(ただいま|帰宅)/ =~ json['text']
log "returning detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :returning}))
end
elsif json['text'].include?('ぬるぽ')
log "nullpo detected. reply to #{json['id']}"
@reply_queue.push(Tweet.new(json['id'], json['text'], User.new(user['id'], user['screen_name']), {:reply_type => :nullpo}))
end # end of replying text checks
else
log 'owner ignored'
end # end of unless block (ignoring owner)
else
log "ignore list includes id:#{user['id']}. ignored."
end # end of unless block (ignoring IGNORE_IDS)
end # end of loopd
end # end of Thread
sleep 1
reply_thread = Thread.new do
log('reply thread start', StatTypes::STARTUP)
loop do
tweet = @reply_queue.pop
unless @replied_count[tweet.user.id]
@replied_count[tweet.user.id] = {reset_time: Time.now + @config.reply_limit_reset_time, count: 0}
end
@replied_count[tweet.user.id][:count] += 1
if @replied_count[tweet.user.id][:count] > @config.reply_limit_threshold
if @replied_count[tweet.user.id][:reset_time] < Time.now
@replied_count[tweet.user.id] = {reset_time: Time.now + @config.reply_limit_reset_time, count: 0}
else
log "user:#{tweet.user.name}(#{tweet.user.id}) exceeds reply limit! suspend!"
IGNORE_IDS << tweet.user.id
end
end
case tweet.other[:reply_type]
when :meitan
res = reply_meitan(tweet.user, tweet.id)
when :normal_reply
res = reply_mention(tweet.user, tweet.id)
when :csharp
res = reply_csharp(tweet.user, tweet.id)
when :morning
res = reply_morning(tweet.user, tweet.id)
when :sleeping
res = reply_sleeping(tweet.user, tweet.id)
when :returning
res = reply_return(tweet.user, tweet.id)
when :departure
res = reply_departure(tweet.user, tweet.id)
when :weather
res = reply_weather(tweet.user, tweet.id, tweet.other[:ahead])
when :nullpo
res = reply_nullpo(tweet.user, tweet.id)
when :time_inquiry
res = reply_time_inquiry(tweet.user, tweet.id, tweet.other[:time])
when :metaphor
res = reply_metaphor(tweet.user, tweet.id, tweet.other[:word])
else
log("undefined reply_type: #{tweet.other[:reply_type]}", StatTypes::ERROR)
end
if res === Net::HTTPForbidden
log("returned 403 Forbidden when reply. Considering status duplicate, or rate limit.", StatTypes::ERROR)
log "reply thread sleeps #{@config.sleep_when_forbidden_time} sec"
sleep @config.sleep_when_forbidden_time
end
end
end
sleep 1
retweet_thread = Thread.new do
log('retweet thread start', StatTypes::STARTUP)
loop do
json = @retweet_queue.pop
res = retweet(json['id'])
if res === Net::HTTPForbidden
log('returned 403 Forbidden when retweet. Considering status duplicate, or rate limit.', StatTypes::ERROR)
log "retweet thread sleeps #{SLEEP_WHEN_FORBIDDEN} sec"
sleep SLEEP_WHEN_FORBIDDEN
end
end
end
sleep 1
event_thread = Thread.new do
log('event thread start', StatTypes::STARTUP)
loop do
json = @event_queue.pop
case json['event'].to_sym
when :follow
log "new follower: id:#{json['source']['id']}, screen_name:@#{json['source']['screen_name']}"
follow_user json['source']['id']
when :unfollow
log "removed from: id:#{json['source']['id']}, screen_name:@#{json['source']['screen_name']}"
remove_user json['source']['id']
end
end
end
sleep 1
message_thread = Thread.new do
log('message thread start', StatTypes::STARTUP)
loop do
json = @message_queue.pop
log 'Message received.'
sender = json['direct_message']['sender']
text = json['direct_message']['text'].strip
if sender['id'] == OWNER_ID && text.start_with?('cmd ')
log "Received Command Message"
cmd_ary = text.split
cmd_ary.shift
cmd = cmd_ary.shift
control_command(cmd.to_sym, cmd_ary, true)
end
end # end of loop do ...
end # end of Thread.new do ...
sleep 1
post_thread = Thread.new do
log('post thread start', StatTypes::STARTUP)
loop do
s = @post_queue.pop
log 'post poped'
post(s)
end
end
sleep 1
random_post_thread = Thread.new do
log('random post thread start', StatTypes::STARTUP)
loop do
begin
log 'random post'
@post_queue.push random_post
sleep @config.random_post_interval
rescue
error_log 'error in random posting. Retry.'
retry
end
end
end
sleep 1
friendship_check_thread = Thread.new do
log('friendship check thread start', StatTypes::STARTUP)
loop do
log 'check friendship'
follow_unfollowing_user
remove_removed_user
sleep(60 * 60 * 12) # sleep half a day
end
end
sleep 1
receiver_thread = Thread.new do
begin
log('receiver thread start', StatTypes::STARTUP)
total_retry_count = 0
retry_count = 0
loop do
begin
connect do |json|
if json['text'] and not json['retweeted_status']
@statistics[:post_received_count] += 1
@received_queue.push json
elsif json['event']
@statistics[:event_received_count] += 1
@event_queue.push json
elsif json['direct_message']
@statistics[:message_received_count] += 1
@message_queue.push json
end
end
rescue Timeout::Error, StandardError
log('Connection to Twitter is disconnected or Application error was occured.', StatTypes::ERROR)
log($!, StatTypes::ERROR)
log('backtrace:', StatTypes::ERROR)
$@.each {|s| log(' ' + s, StatTypes::ERROR)}
@statistics[:total_retry_count] += 1
if (retry_count < @config.max_continuative_retry_count)
retry_count += 1
log("retry:#{retry_count}")
sleep @config.short_retry_interval
log 'retry!'
else
log('Continuative retry was failed. Receiver will sleep long...', StatTypes::ERROR)
sleep @config.long_retry_interval
retry_count = 0
log 'retry!'
end
end
end
ensure
# Termination post
post "Terminating meitan-bot Bye! #{Time.now.strftime("%X")}"
log 'receiver thread terminated.'
# Write ignore id list
update_ignore_list
end
end
sleep 1
statistics_thread = Thread.new do
begin
log('statistics thread start', StatTypes::STARTUP)
before_time = Time.now
log("Meitan-bot statistics thread started at #{Time.now.strftime("%X")}", StatTypes::STARTUP)
loop do
current_time = Time.now
log("Statistics at #{current_time.to_s}", StatTypes::STAT)
@tweet_request_time.compact!
total = 0.0
for t in @tweet_request_time
total += t
end
@statistics[:tweet_request_time_average] = total / @statistics[:post_count]
@statistics.to_s.lines do |line|
log(line, StatTypes::STAT)
end
if (current_time.min % 10) == 0
log('output stat', StatTypes::STAT)
out = { current_time.strftime('%F_%H:%M') => @statistics }
open(@config.stat_file_prefix + Time.now.strftime('%Y%m%d'), 'a:UTF-8') do |file|
file << out.to_yaml
end
end
sleep @config.stat_interval
end
ensure
log("Meitan-bot statistics thread terminated at #{Time.now.strftime("%X")}", StatTypes::STAT)
end
end
sleep 1
tweet_greeting
log('startup complete.', StatTypes::STARTUP)
end # end of run method
# Tweet the greeting post when bot is started.
def tweet_greeting
log "greeting"
post "Starting meitan-bot. Hello! #{Time.now.strftime('%X')}"
end
# Tweet "I'm not meitan!"
def reply_meitan(reply_to_user, in_reply_to_id)
log "replying to meitan"
post_reply(reply_to_user, in_reply_to_id, random_notmeitan)
end
# Reply to reply to me
def reply_mention(reply_to_user, in_reply_to_id)
log "replying to mention"
post_reply(reply_to_user, in_reply_to_id, random_mention(reply_to_user.id))
end
# Reply to the post containing "C#"
def reply_csharp(reply_to_user, in_reply_to_id)
log "replying to csharp"
post_reply(reply_to_user, in_reply_to_id, random_csharp)
end
def reply_morning(reply_to_user, in_reply_to_id)
log 'replying to morning greeting'
post_reply(reply_to_user, in_reply_to_id, random_morning)
end
def reply_sleeping(reply_to_user, in_reply_to_id)
log 'replying to sleeping'
post_reply(reply_to_user, in_reply_to_id, random_sleeping)
end
def reply_departure(reply_to_user, in_reply_to_id)
log 'replying to departure'
post_reply(reply_to_user, in_reply_to_id, random_departure)
end
def reply_return(reply_to_user, in_reply_to_id)
log 'replying to returning'
if reply_to_user.id == 252004214
post_reply(reply_to_user, in_reply_to_id, 'おかえりつぃん!')
else
post_reply(reply_to_user, in_reply_to_id, random_return)
end
end
def reply_weather(reply_to_user, in_reply_to_id, ahead)
raise ArgumentError if ahead < 0 or 4 < ahead
log 'replying to weather inquiry'
doc = REXML::Document.new(Net::HTTP.get(URI.parse(FORECAST_API_URL + '?weather=' + FORECAST_LOCATION + '&hl=ja')).toutf8)
log 'doc generated.'
if ahead == 0 # Get current condition
log 'get current conditions'
cond_element = doc.elements['/xml_api_reply/weather/current_conditions']
p cond_element
cond = CurrentWeather.new(
cond_element.elements['condition'].attributes['data'],
cond_element.elements['temp_c'].attributes['data'],
cond_element.elements['humidity'].attributes['data'],
cond_element.elements['wind'].attributes['data'])
log "cond: condition=#{cond.condition}, temp=#{cond.temp}, humidity=#{cond.humidity}, wind=#{cond.wind}"
post_reply(reply_to_user, in_reply_to_id, "今の天気は#{cond.condition}、気温#{cond.temp}℃、湿度#{cond.humidity}、風は#{cond.wind}だよ。")
else # Get forecast condition
log 'get forecast condition'
cond_element = doc.elements['/xml_api_reply/weather/forecast_conditions[' + String(ahead) + ']']
cond = ForecastWeather.new(
cond_element.elements['condition'].attributes['data'],
cond_element.elements['day_of_week'].attributes['data'],
cond_element.elements['low'].attributes['data'],
cond_element.elements['high'].attributes['data'])
case Integer(ahead)
when 1
target_day = '明日'
when 2
target_day = 'あさって'
else
log 'unknown ahead value'
raise ArgumentError
end
log "cond:"
log " condition=#{cond.condition}"
log " temp=#{String(cond.temp)}"
log " humidity=#{String(cond.humidity)}"
log " wind=#{String(cond.wind)}"
log "target_day: #{target_day}"
post_reply(reply_to_user, in_reply_to_id, "#{target_day}(#{cond.day_of_week}曜日)の天気は#{cond.condition}、気温は最高#{cond.high}℃、最低#{cond.low}℃だよ。")
end
end
def reply_nullpo(reply_to_user, in_reply_to_id)
post_reply(reply_to_user, in_reply_to_id, 'ガッ')
end
def reply_time_inquiry(reply_to_user, in_reply_to_id, time)
post_reply(reply_to_user, in_reply_to_id, "#{time}時限目は #{@config.tsukuba_time_table[time - 1][0]} から #{@config.tsukuba_time_table[time - 1][1]} までだよ。")
end
def reply_metaphor(reply_to_user, in_reply_to_id, word)
post_reply(reply_to_user, in_reply_to_id, "#{word}ってどういう?")
end
def random_post()
db = nil
# Opening DB
begin
db = SQLite3::Database.new(POST_DATABASE_FILE)
rescue
error_log 'error when opening DB to get status/words.'
return
end
# Get status/words from DB
begin
status = db.get_first_value('SELECT status FROM posts ORDER BY RANDOM() LIMIT 1')
words = db.execute('SELECT word FROM words ORDER BY RANDOM() LIMIT ?', @config.num_of_word)
rescue
error_log 'error in random_post when get status/words.'
ensure
db.close
end
# Initialize MeCab
mecab = MeCab::Tagger.new
node = mecab.parseToNode(status)
# Generate post
result = Array.new
while node
unless node.stat == 2 or node.stat == 3
if node.posid < 36 or 67 < node.posid
result << node.surface
else
if rand(100) < @config.word_replace_probability
result << words[rand(words.size)]
else
result << node.surface
end
end
end
node = node.next
end
result.join(nil)
end
# Reply
def post_reply(reply_to_user, in_reply_to_id, status)
if @is_enabled_posting
@statistics[:post_count] += 1
@statistics[:reply_count] += 1
log "replying to #{in_reply_to_id} by @#{reply_to_user.screen_name}"
req_start = Time.now
res = @access_token.post('https://api.twitter.com/1/statuses/update.json',
'status' => "@#{reply_to_user.screen_name} " + status,
'in_reply_to_status_id' => in_reply_to_id)
req_end = Time.now
@tweet_request_time << req_end - req_start
res
else
log "posting function is now disabled because @is_enabled_posting is false."
end
end
# Post
def post(status)
if @is_enabled_posting
@statistics[:post_count] += 1
log "posting"
req_start = Time.now
res = @access_token.post('https://api.twitter.com/1/statuses/update.json',
'status' => status)
req_end = Time.now
@tweet_request_time << req_end - req_start
res
else
log "posting function is now disabled because @is_enabled_posting is false."
end
end
# Retweet the status
def retweet(id)
@statistics[:post_count] += 1
log "retweeting status-id: #{id}"
req_start = Time.now
@access_token.post("https://api.twitter.com/1/statuses/retweet/#{id}.json")
req_end = Time.now
@tweet_request_time << req_end - req_start
end
# Send Direct Message
def send_direct_message(text, recipient_id)
@statistics[:send_message_count] += 1
log "Sending Direct Message"
@access_token.post('https://api.twitter.com/1/direct_messages/new.json',
'user_id' => recipient_id,
'text' => text)
end
# Follow the user
def follow_user(id)
unless id == MY_ID
log "following user: #{id}"
@access_token.post('https://api.twitter.com/1/friendships/create.json',
'user_id' => id)
end
end
# Remove the user
def remove_user(id)
unless id == MY_ID
log "removing user: #{id}"
@access_token.post('https://api.twitter.com/1/friendships/destroy.json',
'user_id' => id)
end
end
def record_tweet tweet
# stub
end
def record_word word
# stub
end
# Get "not-meitan" text
def random_notmeitan
@notmeitan_text.sample
end
# Get the replying text from text file.
def random_mention_from_text
@reply_mention_text.sample
end
# Get the replying text by generating from recorded status and words.
def random_mention(id)
db = nil
# Opening DB
begin
db = SQLite3::Database.new(POST_DATABASE_FILE)
rescue
error_log 'error in random_mention when opening DB to get status/words.'
log('Get replying text from text file instead.', StatTypes::ERROR)
return random_mention_from_text
end
# Get status/words from DB
begin
status = db.get_first_value('SELECT status FROM posts WHERE user_id = ? ORDER BY RANDOM() LIMIT 1', id)
user_words = db.execute('SELECT word FROM words WHERE user_id = ? ORDER BY RANDOM() LIMIT ?', id, @config.num_of_users_word)
other_words = db.execute('SELECT word FROM words ORDER BY RANDOM() LIMIT ?', @config.num_of_others_word)
rescue
error_log 'error in random_mention when get status/words.'
ensure
db.close
end
words = user_words + other_words
# Initialize MeCab
mecab = MeCab::Tagger.new
node = mecab.parseToNode(status)
# Generate post
result = Array.new
while node
unless node.stat == 2 or node.stat == 3
if node.posid < 36 or 67 < node.posid
result << node.surface
else
if rand(100) < @config.word_replace_probability
result << words[rand(words.size)]
else
result << node.surface
end
end
end
node = node.next
end
result.join(nil)
end
# Get the replying for the status containing "C#"
def random_csharp
@reply_csharp_text.sample
end
# Get the replying text for the status containing morning greeting
def random_morning
@reply_morning_text.sample
end
# Get the replying text for the status containing sleeping
def random_sleeping
@reply_sleeping_text.sample
end
# Get the replying text for the status containing departures
def random_departure
@reply_departure_text.sample
end
def random_return
@reply_return_text.sample
end
# Get followers
def get_followers(cursor = '-1')
log "get_followers: cursor=#{cursor}"
result = []
if (cursor != '0')
res = @access_token.get('https://api.twitter.com/1/followers/ids.json',
'cursor' => cursor,
'screen_name' => SCREEN_NAME)
json = JSON.parse(res.body)
result << json['ids']
if json['next_cursor_str']
result << get_followers(json['next_cursor_str'])
end
return result.flatten!
end
end
# Get followings
def get_followings(cursor = '-1')
log "get_followings: cursor=#{cursor}"
result = []
if (cursor != '0')
res = @access_token.get('https://api.twitter.com/1/friends/ids.json',
'cursor' => cursor,
'screen_name' => SCREEN_NAME)
json = JSON.parse(res.body)
result << json['ids']
if json['next_cursor_str']
result << get_followings(json['next_cursor_str'])
end
return result.flatten!