ayadn 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ayadn/api.rb ADDED
@@ -0,0 +1,302 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ class AyaDN
4
+ class API
5
+ def initialize(token)
6
+ @token = token
7
+ @endpoints = AyaDN::Endpoints.new(@token)
8
+ end
9
+ def makeAuthorizeURL
10
+ @endpoints.authorize_url
11
+ end
12
+
13
+ def getHash
14
+ JSON.parse(http_get(@url))
15
+ end
16
+
17
+ def checkLastPageID(last_page_id)
18
+ @url += "&since_id=#{last_page_id}" if last_page_id != nil
19
+ end
20
+
21
+ def getAPIConfig
22
+ @url = CONFIG_API_URL
23
+ getHash
24
+ end
25
+
26
+ def getGlobal(last_page_id)
27
+ @url = @endpoints.global
28
+ @url += @endpoints.light_params
29
+ @url += @endpoints.include_directed if $tools.config['timeline']['directed']
30
+ checkLastPageID(last_page_id)
31
+ getHash
32
+ end
33
+ def getUnified(last_page_id)
34
+ @url = @endpoints.unified
35
+ @url += @endpoints.base_params
36
+ @url += @endpoints.include_directed if $tools.config['timeline']['directed']
37
+ checkLastPageID(last_page_id)
38
+ getHash
39
+ end
40
+ def getSimpleUnified
41
+ @url = @endpoints.unified_streamback
42
+ @url += @endpoints.base_params
43
+ @url += @endpoints.include_directed if $tools.config['timeline']['directed']
44
+ getHash
45
+ end
46
+ def getInteractions
47
+ @url = @endpoints.interactions
48
+ #checkLastPageID(last_page_id)
49
+ getHash
50
+ end
51
+ def getHashtags(tag)
52
+ @url = @endpoints.hashtags(tag)
53
+ getHash
54
+ end
55
+ def getExplore(stream, last_page_id)
56
+ @url = @endpoints.explore(stream)
57
+ @url += @endpoints.base_params
58
+ checkLastPageID(last_page_id)
59
+ getHash
60
+ end
61
+ def getUserMentions(username, last_page_id)
62
+ @url = @endpoints.mentions(username)
63
+ @url += @endpoints.light_params
64
+ checkLastPageID(last_page_id)
65
+ getHash
66
+ end
67
+ def getUserPosts(username, last_page_id)
68
+ @url = @endpoints.posts(username)
69
+ @url += @endpoints.base_params
70
+ checkLastPageID(last_page_id)
71
+ getHash
72
+ end
73
+ def getUserInfos(username)
74
+ @url = @endpoints.user_info(username)
75
+ @url += @endpoints.base_params
76
+ getHash
77
+ end
78
+ def getWhoReposted(post_id)
79
+ @url = @endpoints.who_reposted(post_id)
80
+ @url += @endpoints.light_params
81
+ getHash
82
+ end
83
+ def getWhoStarred(post_id)
84
+ @url = @endpoints.who_starred(post_id)
85
+ @url += @endpoints.light_params
86
+ getHash
87
+ end
88
+ def getPostInfos(post_id)
89
+ @url = @endpoints.single_post(post_id)
90
+ @url += @endpoints.base_params
91
+ getHash
92
+ end
93
+ def getSinglePost(post_id)
94
+ @url = @endpoints.single_post(post_id)
95
+ @url += @endpoints.base_params
96
+ getHash
97
+ end
98
+ def getStarredPosts(username)
99
+ @url = @endpoints.starred_posts(username)
100
+ @url += @endpoints.light_params
101
+ getHash
102
+ end
103
+ def getPostReplies(post_id)
104
+ @url = @endpoints.replies(post_id)
105
+ @url += @endpoints.base_params
106
+ getHash
107
+ end
108
+ def getPostMentions(post_id)
109
+ @url = @endpoints.single_post(post_id)
110
+ @url += @endpoints.light_params
111
+ theHash = getHash
112
+ postInfo = theHash['data']
113
+ #rawText = postInfo['text']
114
+ postMentionsArray = []
115
+ postInfo['entities']['mentions'].each { |item| postMentionsArray.push(item['name']) }
116
+ return postMentionsArray, postInfo['user']['username'], postInfo['repost_of']
117
+ end
118
+ def getUserName(username)
119
+ @url = @endpoints.user_info(username)
120
+ @url += @endpoints.light_params
121
+ theHash = getHash
122
+ theHash['data']['username']
123
+ end
124
+ def goDelete(post_id)
125
+ @url = @endpoints.single_post(post_id)
126
+ @url += @endpoints.light_params
127
+ isTherePost, isYours = ifExists(post_id)
128
+ return isTherePost, isYours
129
+ end
130
+ def starPost(post_id)
131
+ @url = @endpoints.star(post_id)
132
+ @url += @endpoints.light_params
133
+ httpPost(@url)
134
+ end
135
+ def unstarPost(post_id)
136
+ @url = @endpoints.star(post_id)
137
+ @url += @endpoints.light_params
138
+ $tools.checkHTTPResp(http_delete())
139
+ end
140
+ def repostPost(post_id)
141
+ @url = @endpoints.repost(post_id)
142
+ @url += @endpoints.light_params
143
+ httpPost(@url)
144
+ end
145
+ def unrepostPost(post_id)
146
+ @url = @endpoints.repost(post_id)
147
+ @url += @endpoints.light_params
148
+ $tools.checkHTTPResp(http_delete())
149
+ end
150
+ def ifExists(post_id)
151
+ theHash = getHash
152
+ postInfo = theHash['data']
153
+ return postInfo['text'], postInfo['user']['username']
154
+ end
155
+ def getOriginalPost(post_id)
156
+ theHash = getHash
157
+ theHash['data']['repost_of']['id']
158
+ end
159
+ def getUserFollowInfo(username)
160
+ @url = @endpoints.user_info(username)
161
+ @url += @endpoints.light_params
162
+ theHash = getHash
163
+ {you_follow: theHash['data']['you_follow'], follows_you: theHash['data']['follows_you']}
164
+ end
165
+ def getUserMuteInfo(username)
166
+ @url = @endpoints.user_info(username)
167
+ @url += @endpoints.light_params
168
+ theHash = getHash
169
+ theHash['data']['you_muted']
170
+ end
171
+ def getUserBlockInfo(username)
172
+ @url = @endpoints.user_info(username)
173
+ @url += @endpoints.light_params
174
+ theHash = getHash
175
+ theHash['data']['you_blocked']
176
+ end
177
+ def muteUser(username)
178
+ @url = @endpoints.mute(username)
179
+ @url += @endpoints.light_params
180
+ httpPost(@url)
181
+ end
182
+ def unmuteUser(username)
183
+ @url = @endpoints.mute(username)
184
+ @url += @endpoints.light_params
185
+ $tools.checkHTTPResp(http_delete())
186
+ end
187
+ def blockUser(username)
188
+ @url = @endpoints.block(username)
189
+ @url += @endpoints.light_params
190
+ httpPost(@url)
191
+ end
192
+ def unblockUser(username)
193
+ @url = @endpoints.block(username)
194
+ @url += @endpoints.light_params
195
+ $tools.checkHTTPResp(http_delete())
196
+ end
197
+ def followUser(username)
198
+ @url = @endpoints.follow(username)
199
+ @url += @endpoints.light_params
200
+ httpPost(@url)
201
+ end
202
+ def unfollowUser(username)
203
+ @url = @endpoints.follow(username)
204
+ @url += @endpoints.light_params
205
+ $tools.checkHTTPResp(http_delete())
206
+ end
207
+ def getFollowings(username, beforeID)
208
+ @url = @endpoints.following(username)
209
+ @url += @endpoints.light_params
210
+ @url += "&count=200"
211
+ @url += "&before_id=#{beforeID}" if beforeID != nil
212
+ getHash
213
+ end
214
+ def getFollowers(username, beforeID)
215
+ @url = @endpoints.followers(username)
216
+ @url += @endpoints.light_params
217
+ @url += "&count=200"
218
+ @url += "&before_id=#{beforeID}" if beforeID != nil
219
+ getHash
220
+ end
221
+ def getMuted(username, beforeID)
222
+ @url = @endpoints.muted(username)
223
+ @url += @endpoints.light_params
224
+ @url += "&count=200"
225
+ @url += "&before_id=#{beforeID}" if beforeID != nil
226
+ getHash
227
+ end
228
+ def getBlocked(username, beforeID)
229
+ @url = @endpoints.blocked(username)
230
+ @url += @endpoints.light_params
231
+ @url += "&count=200"
232
+ @url += "&before_id=#{beforeID}" if beforeID != nil
233
+ getHash
234
+ end
235
+ def getSearch(words)
236
+ @url = @endpoints.search(words)
237
+ @url += @endpoints.base_params
238
+ getHash
239
+ end
240
+ def unique_message(channel_id, message_id)
241
+ @url = @endpoints.get_message(channel_id, message_id)
242
+ @url += @endpoints.base_params
243
+ end
244
+ def getUniqueMessage(channel_id, message_id)
245
+ @url = @endpoints.get_message(channel_id, message_id)
246
+ @url += @endpoints.base_params
247
+ getHash
248
+ end
249
+ def getMessages(channel, last_page_id)
250
+ @url = @endpoints.messages(channel)
251
+ @url += @endpoints.base_params
252
+ @url += "&include_machine=1"
253
+ checkLastPageID(last_page_id)
254
+ getHash
255
+ end
256
+ def get_pm_channels
257
+ @url = @endpoints.channels
258
+ @url += @endpoints.base_params
259
+ @url += "&channel_types=net.app.core.pm"
260
+ @url += "&include_recent_message=1"
261
+ getHash
262
+ end
263
+ def get_channels
264
+ @url = @endpoints.channels
265
+ @url += @endpoints.base_params
266
+ @url += "&include_recent_message=1"
267
+ getHash
268
+ end
269
+ def getFilesList(beforeID)
270
+ @url = @endpoints.files_list
271
+ @url += @endpoints.light_params
272
+ @url += "&before_id=#{beforeID}" if beforeID != nil
273
+ getHash
274
+ end
275
+ def getSingleFile(file_id)
276
+ @url = @endpoints.get_file(file_id)
277
+ @url += @endpoints.light_params
278
+ getHash
279
+ end
280
+ def getMultipleFiles(file_ids)
281
+ @url = @endpoints.get_multiple_files(file_ids)
282
+ @url += @endpoints.light_params
283
+ getHash
284
+ end
285
+ def deleteFile(file_id)
286
+ @url = @endpoints.get_file(file_id)
287
+ @url += @endpoints.light_params
288
+ $tools.checkHTTPResp(http_delete())
289
+ end
290
+ def deleteMessage(channel_id, message_id)
291
+ @url = @endpoints.get_message(channel_id, message_id)
292
+ @url += @endpoints.access_token
293
+ $tools.checkHTTPResp(http_delete())
294
+ end
295
+ # def deactivateChannel(channel_id)
296
+ # @url = CHANNELS_URL + "#{channel_id}?"
297
+ # @url += @endpoints.access_token
298
+ # resp = http_delete
299
+ # $tools.checkHTTPResp(resp)
300
+ # end
301
+ end
302
+ end
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ class AyaDN
4
+ def ayadnAuthorize(action)
5
+ $files.makedir($tools.ayadn_configuration[:authorization_path])
6
+ if action == "reset"
7
+ $files.reset_credentials
8
+ end
9
+ auth_token = $files.auth_read
10
+ if auth_token == nil
11
+ url = @api.makeAuthorizeURL
12
+ case $tools.ayadn_configuration[:platform]
13
+ when $tools.winplatforms
14
+ puts $status.launchAuthorization("win")
15
+ when /linux/
16
+ puts $status.launchAuthorization("linux")
17
+ else
18
+ puts $status.launchAuthorization("osx")
19
+ $tools.startBrowser(url)
20
+ end
21
+ auth_token = STDIN.gets.chomp()
22
+ $files.auth_write(auth_token)
23
+ puts $status.authorized
24
+ sleep 3
25
+ puts $tools.helpScreen
26
+ puts "Enjoy!\n".cyan
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,226 @@
1
+ #!/usr/bin/env ruby
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'openssl'
5
+
6
+ # encoding: utf-8
7
+ class AyaDN
8
+ class API
9
+ # WIP
10
+ # TESTING DIFFERENT WAYS
11
+ # TODO: DRY
12
+ def check_http_error_code(body)
13
+ code = JSON.parse(body)
14
+ case code['meta']['code']
15
+ when 204
16
+ puts "\nNo content (or incomplete).\n\n".red
17
+ exit
18
+ when 400
19
+ puts "\nBad request.\n\n".red
20
+ puts code['meta'].inspect
21
+ exit
22
+ when 401
23
+ puts "\nUnauthorized.\n\n".red
24
+ exit
25
+ when 403
26
+ puts "\nForbidden.\n\n".red
27
+ exit
28
+ when 404
29
+ puts "\nDoes not exist (or has been deleted).\n\n".red
30
+ exit
31
+ when 429
32
+ puts "\nToo many requests.\n\n".red
33
+ exit
34
+ when 507
35
+ puts "\nInsufficient storage.\n\n".red
36
+ exit
37
+ end
38
+ end
39
+ def http_get(target)
40
+ encoded_url = URI.encode("#{target}")
41
+ uri = URI.parse(encoded_url)
42
+ https = Net::HTTP.new(uri.host,uri.port)
43
+ https.use_ssl = true
44
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
+ request = Net::HTTP::Get.new(uri.request_uri)
46
+ request["Authorization"] = "Bearer #{@token}"
47
+ request["Content-Type"] = "application/json"
48
+ if @progress_indicator == false
49
+ #print "Connecting to #{uri.host}#{uri.path} ...\n\n".cyan
50
+ response = https.request(request)
51
+ check_http_error_code(response.body)
52
+ return response.body
53
+ else
54
+ #print "\rConnecting to #{uri.host}#{uri.path}".cyan
55
+ body = ''
56
+ https.request(request) do |res|
57
+ fileSize = res['Content-Length'].to_i
58
+ bytesTransferred = 0
59
+ res.read_body do |part|
60
+ bytesTransferred += part.length
61
+ rounded = bytesTransferred.percent_of(fileSize).round(2)
62
+ print "\rFetching data: ".cyan + "#{rounded} ".brown
63
+ # print "\rDownloading response from App.net\t#{bytesTransferred.to_filesize}"
64
+ body << part
65
+ end
66
+ end
67
+ print "\r "
68
+ #puts ""
69
+ check_http_error_code(body)
70
+ return body
71
+ end
72
+ end
73
+ def http_delete
74
+ uri = URI("#{@url}")
75
+ https = Net::HTTP.new(uri.host,uri.port)
76
+ https.use_ssl = true
77
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
78
+ request = Net::HTTP::Delete.new(uri.path)
79
+ request["Authorization"] = "Bearer #{@token}"
80
+ request["Content-Type"] = "application/json"
81
+ https.request(request)
82
+ end
83
+ def http_download(target=nil)
84
+ uri = URI("#{target}")
85
+ final_uri = ''
86
+ open(uri) do |h|
87
+ final_uri = h.base_uri.to_s
88
+ end
89
+ new_uri = URI.parse(final_uri)
90
+ https = Net::HTTP.new(new_uri.host,new_uri.port)
91
+ https.use_ssl = true
92
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
93
+ request = Net::HTTP::Get.new(new_uri.request_uri)
94
+ request["Authorization"] = "Bearer #{@token}"
95
+ request["Content-Type"] = "application/json"
96
+ https.request(request)
97
+ end
98
+ #####
99
+ # experimenting
100
+ # def createIncompleteFileUpload(file_name) #this part works
101
+ # https, request = connectWithHTTP(FILES_URL)
102
+ # payload = {
103
+ # "kind" => "image",
104
+ # "type" => "com.ayadn.files",
105
+ # "name" => File.basename(file_name),
106
+ # "public" => true
107
+ # }.to_json
108
+ # response = https.request(request, payload)
109
+ # return response.body
110
+ # end
111
+ # def setFileContentUpload(file_id, file_path, file_token) #this one doesn't
112
+ # url = FILES_URL + "#{file_id}/content?file_token=#{file_token}"
113
+ # uri = URI("#{url}")
114
+ # # ...
115
+ # end
116
+ #####
117
+
118
+ def httpPutFile(file, data) # data must be json
119
+ url = "https://alpha-api.app.net/stream/0/files/#{file}"
120
+ uri = URI("#{url}")
121
+ https = Net::HTTP.new(uri.host,uri.port)
122
+ https.use_ssl = true
123
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
124
+ request = Net::HTTP::Put.new(uri.path)
125
+ request["Authorization"] = "Bearer #{@token}"
126
+ request["Content-Type"] = "application/json"
127
+ return https.request(request, data)
128
+ end
129
+
130
+ def connectWithHTTP(url)
131
+ uri = URI("#{url}")
132
+ https = Net::HTTP.new(uri.host,uri.port)
133
+ https.use_ssl = true
134
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
135
+ request = Net::HTTP::Post.new(uri.path)
136
+ request["Authorization"] = "Bearer #{@token}"
137
+ request["Content-Type"] = "application/json"
138
+ return https, request
139
+ end
140
+ def httpPost(url)
141
+ https, request = connectWithHTTP(url)
142
+ return https.request(request)
143
+ end
144
+ def httpSendMessage(target, text)
145
+ url = PM_URL
146
+ url += "?include_annotations=1"
147
+ https, request = connectWithHTTP(url)
148
+ entities_content = {
149
+ "parse_markdown_links" => true,
150
+ "parse_links" => true
151
+ }
152
+ ayadnAnno = clientAnnotations
153
+ destinations = []
154
+ payload = {
155
+ "text" => "#{text}",
156
+ "destinations" => destinations.push(target),
157
+ "entities" => entities_content,
158
+ "annotations" => ayadnAnno
159
+ }.to_json
160
+ response = https.request(request, payload)
161
+ return response.body
162
+ end
163
+ def httpSendMessageToChannel(target, text)
164
+ url = CHANNELS_URL
165
+ url += "#{target}/messages"
166
+ #url += "&include_annotations=1"
167
+
168
+ https, request = connectWithHTTP(url)
169
+ entities_content = {
170
+ "parse_markdown_links" => true,
171
+ "parse_links" => true
172
+ }
173
+ ayadnAnno = clientAnnotations
174
+ #destinations = []
175
+ payload = {
176
+ "text" => "#{text}",
177
+ "entities" => entities_content,
178
+ "annotations" => ayadnAnno
179
+ }.to_json
180
+ response = https.request(request, payload)
181
+ return response.body
182
+ end
183
+ def httpSend(text, replyto = nil)
184
+ url = POSTS_URL
185
+ url += "?include_annotations=1"
186
+ https, request = connectWithHTTP(url)
187
+ entities_content = {
188
+ "parse_markdown_links" => true,
189
+ "parse_links" => true
190
+ }
191
+ ayadnAnno = clientAnnotations
192
+ if replyto == nil
193
+ payload = {
194
+ "text" => "#{text}",
195
+ "entities" => entities_content,
196
+ "annotations" => ayadnAnno
197
+ }.to_json
198
+ else
199
+ payload = {
200
+ "text" => "#{text}",
201
+ "reply_to" => "#{replyto}",
202
+ "entities" => entities_content,
203
+ "annotations" => ayadnAnno
204
+ }.to_json
205
+ end
206
+ response = https.request(request, payload)
207
+ return response.body
208
+ end
209
+
210
+ def clientAnnotations
211
+ ayadn_annotations = [{
212
+ "type" => "com.ayadn.client",
213
+ "value" => {
214
+ "+net.app.core.user" => {
215
+ "user_id" => "@ayadn",
216
+ "format" => "basic"
217
+ }
218
+ }
219
+ },{
220
+ "type" => "com.ayadn.client",
221
+ "value" => { "url" => "http://ayadn-app.net" }
222
+ }]
223
+ return ayadn_annotations
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,62 @@
1
+ class String
2
+ def reddish;
3
+ "\033[1;31m#{self}\033[0m"
4
+ end
5
+ def pink;
6
+ "\033[1;35m#{self}\033[0m"
7
+ end
8
+ def black;
9
+ "\033[30m#{self}\033[0m"
10
+ end
11
+ def red;
12
+ "\033[31m#{self}\033[0m"
13
+ end
14
+ def green;
15
+ "\033[32m#{self}\033[0m"
16
+ end
17
+ def brown;
18
+ "\033[33m#{self}\033[0m"
19
+ end
20
+ def blue;
21
+ "\033[34m#{self}\033[0m"
22
+ end
23
+ def magenta;
24
+ "\033[35m#{self}\033[0m"
25
+ end
26
+ def cyan;
27
+ "\033[36m#{self}\033[0m"
28
+ end
29
+ def gray;
30
+ "\033[37m#{self}\033[0m"
31
+ end
32
+ def bg_black;
33
+ "\033[40m#{self}\0330m"
34
+ end
35
+ def bg_red;
36
+ "\033[41m#{self}\033[0m"
37
+ end
38
+ def bg_green;
39
+ "\033[42m#{self}\033[0m"
40
+ end
41
+ def bg_brown;
42
+ "\033[43m#{self}\033[0m"
43
+ end
44
+ def bg_blue;
45
+ "\033[44m#{self}\033[0m"
46
+ end
47
+ def bg_magenta;
48
+ "\033[45m#{self}\033[0m"
49
+ end
50
+ def bg_cyan;
51
+ "\033[46m#{self}\033[0m"
52
+ end
53
+ def bg_gray;
54
+ "\033[47m#{self}\033[0m"
55
+ end
56
+ def bold;
57
+ "\033[1m#{self}\033[22m"
58
+ end
59
+ def reverse_color;
60
+ "\033[7m#{self}\033[27m"
61
+ end
62
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ class AyaDN
4
+ def debugStream
5
+ puts @view.new(@hash).showDebugStream
6
+ end
7
+ def ayadnDebugStream
8
+ @hash = @api.getUnified(nil)
9
+ debugStream
10
+ end
11
+ def ayadnDebugPost(postID)
12
+ @hash = @api.getPostInfos(postID)
13
+ debugStream
14
+ end
15
+ def ayadnDebugUser(username)
16
+ @hash = @api.getUserInfos(username)
17
+ debugStream
18
+ end
19
+ def ayadnDebugMessage(channel_id, message_id)
20
+ @hash = @api.getUniqueMessage(channel_id, message_id)
21
+ debugStream
22
+ end
23
+ def buildDebugStream(post_hash)
24
+ # ret_string = ""
25
+ # post_hash.each do |k, v|
26
+ # ret_string << "#{k}: #{v}\n\n"
27
+ # end
28
+ jj post_hash
29
+ #exit
30
+ #return ret_string
31
+ end
32
+ def ayadnDebugChannel(channel)
33
+ @hash = @api.getMessages(channel, nil)
34
+ jj @hash
35
+ end
36
+ end