-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmfs.rb
executable file
·69 lines (58 loc) · 1.8 KB
/
dmfs.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
#!/usr/bin/env ruby
require 'twitter'
require 'yaml'
require 'json'
require 'base64'
require 'pry'
$config = YAML.load_file File.expand_path(".", "config.yml")
$LOAD_PATH.unshift File.expand_path './lib', File.dirname(__FILE__)
require 'fs'
# Twitter client configuration
$client = Twitter::REST::Client.new do |config|
config.consumer_key = $config['twitter']['consumer_key']
config.consumer_secret = $config['twitter']['consumer_secret']
config.access_token = $config['twitter']['access_token']
config.access_token_secret = $config['twitter']['access_token_secret']
end
# Twitter streaming configuration
$streamer = Twitter::Streaming::Client.new do |config|
config.consumer_key = $config['twitter']['consumer_key']
config.consumer_secret = $config['twitter']['consumer_secret']
config.access_token = $config['twitter']['access_token']
config.access_token_secret = $config['twitter']['access_token_secret']
end
# main process initialization
def main
# Interactive shell using pry
@shell ||= Thread.new do
commands = Pry::CommandSet.new do
command "send" do |file, name|
DMFS.create_message("#{$config['folders']['upload']}/#{file}").each do |message|
$client.create_direct_message name, message
end
end
desc "send", "send a file to an user"
end
Pry.start binding, :commands => commands
end
# Twitter streaming to fetch incoming direct messages
@twitter ||= Thread.new do
loop do
begin
$streamer.user do |obj|
case obj
when Twitter::DirectMessage
DMFS.receive_file_part(obj.id) if obj.text.start_with?("!!DMFS")
end
end
rescue => e
puts e.message
end
puts "Lost user stream connection"
sleep 1
end
end
@shell.join
@twitter.join
end
main