-
Notifications
You must be signed in to change notification settings - Fork 2
/
mikutter_update_with_media.rb
143 lines (127 loc) · 4.86 KB
/
mikutter_update_with_media.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
# -*- coding: utf-8 -*-
require 'twitter'
Plugin.create :update_with_media do
twitter_version_is_over_6 = Gem::Version.new('6.0.0') <= Gem::Version.new(Twitter::Version.to_s)
@clients = {}
unless UserConfig[:twitter_secret] # mikutter >= 3.0.0
@clients[Service.primary.idname] = Twitter::REST::Client.new do |c|
c.consumer_key = Service.primary.twitter.consumer_key
c.consumer_secret = Service.primary.twitter.consumer_secret
if twitter_version_is_over_6
c.access_token = Service.primary.twitter.a_token
c.access_token_secret = Service.primary.twitter.a_secret
else
c.oauth_token = Service.primary.twitter.a_token
c.oauth_token_secret = Service.primary.twitter.a_secret
end
end
else # mikutter < 3.0.0
if defined? Twitter::REST
@clients[Service.primary.idname] = Twitter::REST::Client.new do |c|
c.consumer_key = CHIConfig::TWITTER_CONSUMER_KEY
c.consumer_secret = CHIConfig::TWITTER_CONSUMER_SECRET
if twitter_version_is_over_6
c.access_token = UserConfig[:twitter_token]
c.access_token_secret = UserConfig[:twitter_secret]
else
c.oauth_token = UserConfig[:twitter_token]
c.oauth_token_secret = UserConfig[:twitter_secret]
end
end
else
Twitter.configure do |c|
c.consumer_key = CHIConfig::TWITTER_CONSUMER_KEY
c.consumer_secret = CHIConfig::TWITTER_CONSUMER_SECRET
if twitter_version_is_over_6
c.access_token = UserConfig[:twitter_token]
c.access_token_secret = UserConfig[:twitter_secret]
else
c.oauth_token = UserConfig[:twitter_token]
c.oauth_token_secret = UserConfig[:twitter_secret]
end
end
@clients[Service.primary.idname] = Twitter.client
end
end
command(:update_with_media,
name: '画像付きで投稿する',
condition: lambda{ |opt| true },
visible: true,
role: :postbox) do |opt|
begin
dialog = Gtk::FileChooserDialog.new("Select Upload Image",
nil,
Gtk::FileChooser::ACTION_OPEN,
nil,
[Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL],
[Gtk::Stock::OPEN, Gtk::Dialog::RESPONSE_ACCEPT])
filter = Gtk::FileFilter.new
filter.name = "Image Files"
filter.add_pattern('*.png')
filter.add_pattern('*.PNG')
filter.add_pattern('*.jpg')
filter.add_pattern('*.JPG')
filter.add_pattern('*.jpeg')
filter.add_pattern('*.JPEG')
filter.add_pattern('*.gif')
filter.add_pattern('*.GIF')
dialog.add_filter(filter)
preview = Gtk::Image.new
dialog.preview_widget = preview
dialog.signal_connect("update-preview") {
filename = dialog.preview_filename
if filename
unless File.directory?(filename)
pixbuf = Gdk::Pixbuf.new(filename, 128, 128)
preview.set_pixbuf(pixbuf)
dialog.set_preview_widget_active(true)
else
dialog.set_preview_widget_active(false)
end
else
dialog.set_preview_widget_active(false)
end
}
if dialog.run == Gtk::Dialog::RESPONSE_ACCEPT
filename = dialog.filename.to_s
puts filename
else
filename = nil
end
dialog.destroy
if filename
message = Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text
Thread.new {
@clients[Service.primary.idname].update_with_media(message, File.new(filename))
}
Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text = ''
end
rescue Exception => e
Plugin.call(:update, nil, [Message.new(message: e.to_s, system: true)])
end
end
command(:update_with_media_on_clipboard,
name: 'クリップボードの画像を追加して投稿する',
condition: lambda{ |opt| true },
visible: true,
role: :postbox) do |opt|
begin
filename = nil
pixbuf = Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD).wait_for_image
unless pixbuf.nil?
filename = File.join('/tmp', `uuidgen`.chomp + '.png')
pixbuf.save(filename, 'png')
end
if filename
message = Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text
Thread.new {
@clients[Service.primary.idname].update_with_media(message, File.new(filename))
File.delete(filename)
}
Plugin.create(:gtk).widgetof(opt.widget).widget_post.buffer.text = ''
end
rescue Exception => e
Plugin.call(:update, nil, [Message.new(message: e.to_s, system: true)])
end
end
end