instagram_uploader 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bf8161f4d7173d9f016fe092ca2bc7087581edf9
4
+ data.tar.gz: c3105ca98b51929ac115f7b4e737f824fc49880e
5
+ SHA512:
6
+ metadata.gz: fc666b8fb59b09661816db8b38b542bc11682b44f9b282b03ab1cf0a6ae93ff4f60f5e019003fe94e6ff05ad6c24fb8358044942fc9b767b8654918dd17ca007
7
+ data.tar.gz: 6d17cd570c20029a510b90d4f26ee57f080e360c42d992db3bb6eb5af771f75367d4a00443f8a9f3d7721eed5d3aeadd5a4381aaa5be84f124d6607087014346
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.rb
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in instagram_uploader.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 q3pp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # InstagramUploader
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'instagram_uploader'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install instagram_uploader
18
+
19
+ ## Usage
20
+
21
+
22
+ ## Development
23
+
24
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
+
26
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+
28
+ ## Contributing
29
+
30
+ Bug reports and pull requests are welcome on GitHub at https://github.com/q3pp/instagram_uploader.
31
+
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "instagram_uploader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'instagram_uploader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "instagram_uploader"
8
+ spec.version = InstagramUploader::VERSION
9
+ spec.authors = ["q3pp"]
10
+ spec.email = ["q3pp@yandex.ru"]
11
+
12
+ spec.summary = %q{Upload photo to instagram}
13
+ spec.description = %q{Gem allow upload photo to instagram}
14
+ spec.homepage = "https://github.com/q3pp/instagram_uploader"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_runtime_dependency "curb", "~> 0.9"
26
+ end
@@ -0,0 +1,117 @@
1
+ module InstagramUploader
2
+ class Uploader
3
+ def initialize(username, password)
4
+ @username = username
5
+ @password = password
6
+ @cookie = Tempfile.new('cookies').path
7
+ end
8
+
9
+ def upload(image_path, desc)
10
+ login
11
+ media_response = upload_photo(image_path)
12
+ commit_photo(media_response['media_id'], desc)
13
+ end
14
+
15
+ private
16
+
17
+ def login
18
+ url = 'https://instagram.com/api/v1/accounts/login/'
19
+
20
+ data = "{'username':'#{@username}','password':'#{@password}','guid':'#{generate_guid}','device_id':'#{generate_device_id}','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}".gsub("'", '"')
21
+ signature = generate_signature(data)
22
+ body = 'ig_sig_key_version=4&signed_body=' + signature + '.' + URI::encode(data, /\W/)
23
+
24
+ response = request(url, body, false, false)
25
+ raise Error, response['message'] if response['status'] != 'ok'
26
+ end
27
+
28
+ def upload_photo(image)
29
+ url = 'https://instagram.com/api/v1/media/upload/'
30
+ device_timestamp = Time.now.to_i.to_s
31
+
32
+ data = [
33
+ Curl::PostField.file('photo', image),
34
+ Curl::PostField.content('device_timestamp', device_timestamp)
35
+ ]
36
+
37
+ response = request(url, data, true, true)
38
+
39
+ if response['status'] != 'ok'
40
+ raise Error, response['message']
41
+ else
42
+ response
43
+ end
44
+ end
45
+
46
+ def commit_photo(media_id, desc)
47
+ url = 'https://instagram.com/api/v1/media/configure/'
48
+
49
+ device_timestamp = Time.now.to_i.to_s
50
+ data = "{'guid':'#{@guid}','device_id':'#{@device_id}','device_timestamp':'#{device_timestamp}','media_id':'#{media_id}','caption': '#{desc}','source_type':'5','filter_type':'0','extra':'{}','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}".gsub("'", '"')
51
+ signature = generate_signature(data)
52
+ body = 'ig_sig_key_version=4&signed_body=' + signature + '.' + URI::encode(data, /\W/)
53
+
54
+ response = request(url, body, false, true)
55
+ if response['status'] != 'ok'
56
+ raise Error, response['message']
57
+ else
58
+ response
59
+ end
60
+ end
61
+
62
+ def request(url, data, is_upload, cookies)
63
+ http = Curl::Easy.new(url)
64
+
65
+ http.headers["User-Agent"] = generate_user_agent
66
+ http.multipart_form_post = is_upload
67
+ http.verbose = true
68
+ http.follow_location = true
69
+ http.enable_cookies = true
70
+
71
+ if cookies
72
+ http.cookiefile = @cookie
73
+ else
74
+ http.cookiejar = @cookie
75
+ end
76
+
77
+ http.send('post', data)
78
+ response = JSON.parse(http.body)
79
+ http.close
80
+
81
+ response
82
+ end
83
+
84
+ def generate_guid
85
+ guid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
86
+ rand(0..65535),
87
+ rand(0..65535),
88
+ rand(0..65535),
89
+ rand(16384..20479),
90
+ rand(32768..49151),
91
+ rand(0..65535),
92
+ rand(0..65535),
93
+ rand(0..65535))
94
+ end
95
+
96
+ def generate_device_id
97
+ guid = generate_guid
98
+ "android-#{guid}"
99
+ end
100
+
101
+ def generate_signature(data)
102
+ return OpenSSL::HMAC.hexdigest('sha256', 'b4a23f5e39b5929e0666ac5de94c89d1618a2916', data)
103
+ end
104
+
105
+ def generate_user_agent
106
+ resolution = ['720x1280', '320x480', '480x800', '1024x768', '1280x720', '768x1024', '480x320'].sample
107
+ version = ['GT-N7000', 'SM-N9000', 'GT-I9220', 'GT-I9100'].sample
108
+ dpi = ['120', '160', '320', '240'].sample
109
+
110
+ 'Instagram 4.' + rand(1..2).to_s + '.' + rand(0..2).to_s + ' Android (' + rand(10..11).to_s + '/' + rand(1..3).to_s + '.' + rand(3..5).to_s + '.' + rand(0..5).to_s + "; #{dpi}; #{resolution}; samsung; #{version }; #{version}; smdkc210; ru_RU)"
111
+ end
112
+
113
+ class Error < RuntimeError
114
+ end
115
+ end
116
+
117
+ end
@@ -0,0 +1,3 @@
1
+ module InstagramUploader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "instagram_uploader/version"
2
+ require "instagram_uploader/uploader"
3
+
4
+ module InstagramUploader
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instagram_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - q3pp
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: curb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ description: Gem allow upload photo to instagram
56
+ email:
57
+ - q3pp@yandex.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - instagram_uploader.gemspec
70
+ - lib/instagram_uploader.rb
71
+ - lib/instagram_uploader/uploader.rb
72
+ - lib/instagram_uploader/version.rb
73
+ homepage: https://github.com/q3pp/instagram_uploader
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Upload photo to instagram
97
+ test_files: []