diaspora-api 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/diaspora-api.rb +125 -0
  2. metadata +46 -0
@@ -0,0 +1,125 @@
1
+ #!/usr/bin/ruby
2
+ # encoding: utf-8
3
+ require "net/http"
4
+ require "net/https"
5
+ require "uri"
6
+ require "json"
7
+
8
+ class DiasporaApi
9
+ end
10
+
11
+ class DiasporaApi::Client
12
+ @providername
13
+ @attributes
14
+ @podhost
15
+ @cookie
16
+
17
+ def initialize
18
+ @providername = "d*-rubygem"
19
+ @cookie = nil
20
+ end
21
+
22
+ def login(podhost, username, password)
23
+ @podhost = podhost
24
+ uri = URI.parse(podhost + "/users/sign_in")
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+ http.use_ssl = true
27
+
28
+ request = Net::HTTP::Get.new(uri.request_uri)
29
+
30
+ response = http.request(request)
31
+ if response.code != "200"
32
+ puts "Server returned error " + response.code
33
+ end
34
+
35
+ scookie = /_diaspora_session=[[[:alnum:]]%-]+; /.match(response.response['set-cookie'])
36
+ atok = /\<input name="authenticity_token" type="hidden" value="([[[:alnum:]][[:punct:]]]+)" \/\>/.match(response.body)[1]
37
+
38
+ request = Net::HTTP::Post.new(uri.request_uri)
39
+ request.set_form_data('utf8' => '✓', 'user[username]' => username, 'user[password]' => password, 'user[remember_me]' => 1, 'commit' => 'Signin', 'authenticity_token' => atok)
40
+ request['Cookie'] = scookie
41
+
42
+ response = http.request(request)
43
+
44
+ if response.code.to_i >= 400
45
+ puts "Login failed. Server replied with code " + response.code
46
+ return false
47
+ else
48
+ @cookie = /remember_user_token=[[[:alnum:]]%-]+; /.match(response.response['set-cookie'])
49
+ get_attributes
50
+ return true
51
+ end
52
+ end
53
+
54
+ def post(msg, aspect)
55
+ if(aspect != "public")
56
+ for asp in @attributes["aspects"]
57
+ if(aspect == asp["name"])
58
+ aspect = asp["id"].to_s
59
+ end
60
+ end
61
+ end
62
+
63
+ uri = URI.parse(@podhost + "/status_messages")
64
+ http = Net::HTTP.new(uri.host, uri.port)
65
+ http.use_ssl = true
66
+
67
+ request = Net::HTTP::Post.new(uri.request_uri,initheader = {'Content-Type' =>'application/json'})
68
+ request['Cookie'] = @cookie
69
+
70
+ request.body = { "status_message" => { "text" => msg, "provider_display_name" => @providername }, "aspect_ids" => aspect}.to_json
71
+
72
+ return http.request(request)
73
+ end
74
+
75
+ def get_attributes
76
+ uri = URI.parse(@podhost + "/stream")
77
+ http = Net::HTTP.new(uri.host, uri.port)
78
+ http.use_ssl = true
79
+
80
+ request = Net::HTTP::Get.new(uri.request_uri)
81
+ request['Cookie'] = @cookie
82
+
83
+ response = http.request(request)
84
+ names = ["gon.user", "window.current_user_attributes"]
85
+ i = nil
86
+
87
+ for name in names
88
+ i = response.body.index(name)
89
+ break if i != nil
90
+ end
91
+
92
+
93
+ if i == nil
94
+ puts "Unexpected format"
95
+ else
96
+ start_json = response.body.index("{", i)
97
+ i = start_json
98
+ n = 0
99
+ begin
100
+ case response.body[i]
101
+ when "{" then n += 1
102
+ when "}" then n -= 1
103
+ end
104
+ i += 1
105
+ end until n == 0
106
+ end_json = i - 1
107
+
108
+ @attributes = JSON.parse(response.body[start_json..end_json])
109
+ end
110
+ end
111
+ end
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: diaspora-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - cmrd Senya
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-12-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: API Interface for Diaspora* social network
15
+ email: senya@riseup.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/diaspora-api.rb
21
+ homepage: http://rubygems.org/gems/diaspora-api
22
+ licenses:
23
+ - GPLv3
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Diaspora* API
46
+ test_files: []