nine-gag 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be29168f757ce5a2a52c4bf0a35d59d44f8b41a1
4
+ data.tar.gz: 4c43610a54bfad17665c652904865603fa06a745
5
+ SHA512:
6
+ metadata.gz: 7315715f60bb55c37493239922556c0e16c41bc44528eb26daf0d30b80be581f650179acea7c56b2aa79babf8dc27e9bb277f73441ff588588bccfbec80f7306
7
+ data.tar.gz: 5df8b22e79ebb591e8862980670299f2f1db7c68806e09736350f7c29251afc6ef5889383a7c3c43a30b46cf47fd8e82b38ea6a3c1b03590813f90d245a32c5b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dimas.taniawan@bocistudio.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nine-gag.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Dimas J. Taniawan
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.
@@ -0,0 +1,51 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'nine-gag'
7
+ ```
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install nine-gag
16
+
17
+ ## Usage
18
+
19
+ * Get data from /section/type
20
+ ```ruby
21
+ # http://9gag.com/gif/hot
22
+ NineGag::Scraper.index('gif/hot')
23
+ ```
24
+
25
+ * Load more data
26
+ ```ruby
27
+ NineGag::Scraper.index('gif/hot', :last_id_post)
28
+
29
+ # example
30
+ # page 1
31
+ posts = NineGag::Scraper.index('gif/hot')
32
+
33
+ # page 2
34
+ last_id = posts.last.id
35
+ posts = NineGag::Scraper.index('gif/hot', last_id)
36
+ ```
37
+
38
+ * Detail post
39
+ ```ruby
40
+ post = NineGag::Scraper.show(:post_id)
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dimasjt/nine-gag. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
51
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nine/gag"
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
@@ -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,12 @@
1
+ require 'open-uri'
2
+ require 'nokogiri'
3
+ require 'rest-client'
4
+ require 'json'
5
+ require 'ostruct'
6
+
7
+ require 'nine_gag/version'
8
+ require 'nine_gag/scraper'
9
+ require 'nine_gag/generator'
10
+
11
+ module NineGag
12
+ end
@@ -0,0 +1,4 @@
1
+ module NineGag
2
+ module Generator
3
+ end
4
+ end
@@ -0,0 +1,103 @@
1
+ module NineGag
2
+ class Scraper
3
+ # path = ":section/:type"
4
+ # next_page = "last_id"
5
+ def self.index(path, next_page = nil)
6
+ if next_page.nil?
7
+ data = scrape_html(path)
8
+ else
9
+ data = generate_json_posts(path, next_page)
10
+ end
11
+
12
+ generate_index_data(data, next_page)
13
+ end
14
+
15
+ # path = "gag/:id"
16
+ def self.show(path)
17
+ path = "gag/#{path}"
18
+ generate_show_data(scrape_html(path).search('article').first)
19
+ end
20
+
21
+ private
22
+
23
+ def self.full_url(path)
24
+ "http://9gag.com/#{path}"
25
+ end
26
+
27
+ # will return Array of Nokogiri
28
+ def self.generate_json_posts(path, next_page)
29
+ items = JSON.parse(scrape_json(path, next_page).body)["items"]
30
+
31
+ items.map { |item| scrape_html(item.last).search('article').first }
32
+ end
33
+
34
+ def self.scrape_json(path, next_page)
35
+ RestClient.get(full_url(path),
36
+ { Accept: 'application/json', "X-Requested-With": 'XMLHttpRequest',
37
+ params: { id: next_page, c: 10 }
38
+ }
39
+ )
40
+ end
41
+
42
+ # scrape html or from path
43
+ def self.scrape_html(path)
44
+ Nokogiri::HTML(path_or_html(path))
45
+ end
46
+
47
+ # check parameter if path or html
48
+ def self.path_or_html(data)
49
+ # if path
50
+ if data.length < 50
51
+ open(full_url(data))
52
+ else
53
+ data
54
+ end
55
+ end
56
+
57
+ def self.generate_show_data(post, index = false)
58
+ if index
59
+ title = post.search('h2.badge-item-title .badge-evt')
60
+ else
61
+ title = post.search('h2.badge-item-title')
62
+ end
63
+
64
+ post_meta = post.search('p.post-meta').first
65
+
66
+ post_data = {
67
+ id: post.attribute('data-entry-id').value,
68
+ title: title.text.strip,
69
+ url: post.attribute('data-entry-url').value,
70
+ image: image_data(post.search('div.badge-post-container a img').first),
71
+ comments_count: post_meta.search('a.comment').first.text.sub(' comments', '').sub(',', '').strip.to_i,
72
+ points: post_meta.search('a.point').first.text.sub(' points', '').sub(',', '').strip,
73
+ media: media_data(post.search('video').first)
74
+ }
75
+
76
+ OpenStruct.new(post_data)
77
+ end
78
+
79
+ def self.generate_index_data(scrape, next_page)
80
+ data = next_page.nil? ? scrape.search('article') : scrape
81
+
82
+ data.map do |post|
83
+ generate_show_data(post)
84
+ end
85
+ end
86
+
87
+ def self.image_data(image)
88
+ return nil if image.nil?
89
+
90
+ image.attribute('src').value
91
+ end
92
+
93
+ def self.media_data(media)
94
+ return nil if media.nil?
95
+ videos = media.search('source')
96
+ {
97
+ poster: media.attribute('poster').value,
98
+ mp4: videos.first.attribute('src').value,
99
+ webm: videos.last.attribute('src').value
100
+ }
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module NineGag
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nine_gag/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'nine-gag'
8
+ spec.version = NineGag::VERSION
9
+ spec.authors = ['Dimas J. Taniawan']
10
+ spec.email = ['dimazniawan@gmail.com']
11
+
12
+ spec.summary = '9Gag API'
13
+ spec.description = 'Unofficial 9Gag gem'
14
+ spec.homepage = 'https://github.com/dimasjt/nine-gag'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'nokogiri', '~> 1.6.0', '>= 1.6.0'
25
+ spec.add_dependency 'rest-client', '~> 2.0.0', '>= 2.0.0'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.13'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'minitest', '~> 5.0'
30
+ spec.add_development_dependency 'minitest-reporters', '~> 1.1.0', '>= 1.1.0'
31
+ spec.add_development_dependency 'pry', '~> 0.10.0', '>= 0.10.0'
32
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nine-gag
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dimas J. Taniawan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.0
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.6.0
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rest-client
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 2.0.0
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 2.0.0
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.13'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.13'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: minitest
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '5.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '5.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: minitest-reporters
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 1.1.0
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.1.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: 1.1.0
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: 1.1.0
115
+ - !ruby/object:Gem::Dependency
116
+ name: pry
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: 0.10.0
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 0.10.0
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.10.0
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 0.10.0
135
+ description: Unofficial 9Gag gem
136
+ email:
137
+ - dimazniawan@gmail.com
138
+ executables: []
139
+ extensions: []
140
+ extra_rdoc_files: []
141
+ files:
142
+ - ".gitignore"
143
+ - ".travis.yml"
144
+ - CODE_OF_CONDUCT.md
145
+ - Gemfile
146
+ - LICENSE.txt
147
+ - README.md
148
+ - Rakefile
149
+ - bin/console
150
+ - bin/setup
151
+ - lib/nine_gag.rb
152
+ - lib/nine_gag/generator.rb
153
+ - lib/nine_gag/scraper.rb
154
+ - lib/nine_gag/version.rb
155
+ - nine-gag.gemspec
156
+ homepage: https://github.com/dimasjt/nine-gag
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubyforge_project:
176
+ rubygems_version: 2.5.1
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: 9Gag API
180
+ test_files: []