youtube_parser 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +35 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/youtube_parser.rb +23 -0
- data/lib/youtube_parser/base_parser.rb +47 -0
- data/lib/youtube_parser/channel.rb +32 -0
- data/lib/youtube_parser/channels/about_section.rb +89 -0
- data/lib/youtube_parser/channels/videos_section.rb +46 -0
- data/lib/youtube_parser/client.rb +42 -0
- data/lib/youtube_parser/config/keys.yml +24 -0
- data/lib/youtube_parser/resource.rb +28 -0
- data/lib/youtube_parser/version.rb +5 -0
- data/youtube_parser.gemspec +30 -0
- metadata +135 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2787d7682c42086c3586dd86a774760b0ee78f314ab297cc06e669cd1c5b8167
|
4
|
+
data.tar.gz: 537a9cdf529e9b5760661aa75188bfc0990d82402de42776f9ebd6786394bc8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c131bee117f67f0ad2735d6b5ef82ccd68265db53564fc98f1cbb6f090c954983c7de7470628992757be9ab50b76fa6549d511157441e689322477d7d2d4dbd2
|
7
|
+
data.tar.gz: b31abce91b7e2a2773f162143aabf96dd71563c24f5e4350e40750e95089491ca4f496c7d44e2a0bea85d4fd0ec51b1088a38ce472864e5d6beaec226cb63b4a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Exclude:
|
5
|
+
- 'Rakefile'
|
6
|
+
- 'Gemfile'
|
7
|
+
- '*.gemspec'
|
8
|
+
- '*/*/version.rb'
|
9
|
+
- 'bin/*'
|
10
|
+
- 'spec/spec_helper.rb'
|
11
|
+
|
12
|
+
Style/Documentation:
|
13
|
+
Enabled: false
|
14
|
+
Layout/DotPosition:
|
15
|
+
Enabled: false
|
16
|
+
Metrics/BlockLength:
|
17
|
+
Enabled: false
|
18
|
+
Layout/MultilineOperationIndentation:
|
19
|
+
EnforcedStyle: indented
|
20
|
+
Layout/CaseIndentation:
|
21
|
+
EnforcedStyle: end
|
22
|
+
Layout/EndAlignment:
|
23
|
+
EnforcedStyleAlignWith: start_of_line
|
24
|
+
Metrics/CyclomaticComplexity:
|
25
|
+
Enabled: false
|
26
|
+
Metrics/MethodLength:
|
27
|
+
Enabled: false
|
28
|
+
Style/FrozenStringLiteralComment:
|
29
|
+
Enabled: true
|
30
|
+
RSpec/MultipleExpectations:
|
31
|
+
Max: 2
|
32
|
+
RSpec/ExampleLength:
|
33
|
+
Max: 10
|
34
|
+
Style/ClassCheck:
|
35
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 o.vykhor@ukr.net. 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
data/Gemfile.lock
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
youtube_parser (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (6.0.1)
|
10
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
|
+
i18n (>= 0.7, < 2)
|
12
|
+
minitest (~> 5.1)
|
13
|
+
tzinfo (~> 1.1)
|
14
|
+
zeitwerk (~> 2.2)
|
15
|
+
addressable (2.6.0)
|
16
|
+
public_suffix (>= 2.0.2, < 4.0)
|
17
|
+
byebug (11.0.1)
|
18
|
+
concurrent-ruby (1.1.5)
|
19
|
+
crack (0.4.3)
|
20
|
+
safe_yaml (~> 1.0.0)
|
21
|
+
diff-lcs (1.3)
|
22
|
+
faraday (0.17.1)
|
23
|
+
multipart-post (>= 1.2, < 3)
|
24
|
+
hashdiff (0.3.7)
|
25
|
+
i18n (1.7.0)
|
26
|
+
concurrent-ruby (~> 1.0)
|
27
|
+
minitest (5.13.0)
|
28
|
+
multipart-post (2.1.1)
|
29
|
+
oj (3.9.2)
|
30
|
+
public_suffix (3.0.3)
|
31
|
+
rake (10.5.0)
|
32
|
+
rspec (3.9.0)
|
33
|
+
rspec-core (~> 3.9.0)
|
34
|
+
rspec-expectations (~> 3.9.0)
|
35
|
+
rspec-mocks (~> 3.9.0)
|
36
|
+
rspec-core (3.9.0)
|
37
|
+
rspec-support (~> 3.9.0)
|
38
|
+
rspec-expectations (3.9.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.9.0)
|
41
|
+
rspec-mocks (3.9.0)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.9.0)
|
44
|
+
rspec-support (3.9.0)
|
45
|
+
safe_yaml (1.0.4)
|
46
|
+
thread_safe (0.3.6)
|
47
|
+
tzinfo (1.2.5)
|
48
|
+
thread_safe (~> 0.1)
|
49
|
+
webmock (3.0.1)
|
50
|
+
addressable (>= 2.3.6)
|
51
|
+
crack (>= 0.3.2)
|
52
|
+
hashdiff
|
53
|
+
zeitwerk (2.2.2)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
|
58
|
+
DEPENDENCIES
|
59
|
+
activesupport
|
60
|
+
bundler (~> 2.0)
|
61
|
+
byebug
|
62
|
+
faraday
|
63
|
+
oj
|
64
|
+
rake (~> 10.0)
|
65
|
+
rspec (~> 3.0)
|
66
|
+
webmock
|
67
|
+
youtube_parser!
|
68
|
+
|
69
|
+
BUNDLED WITH
|
70
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 o.vykhor
|
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,48 @@
|
|
1
|
+
# YoutubeParser
|
2
|
+
## Installation
|
3
|
+
|
4
|
+
Add this line to your application's Gemfile:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
gem 'youtube_parser'
|
8
|
+
```
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install youtube_parser
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
To fetch youtube channel info:
|
21
|
+
```ruby
|
22
|
+
require 'youtube_parser'
|
23
|
+
|
24
|
+
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 '\
|
25
|
+
'(KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
|
26
|
+
options = { channel_url: 'https://www.youtube.com/channel/channel_id',
|
27
|
+
user_agent: user_agent }
|
28
|
+
|
29
|
+
YoutubeParser::Channel.new(options).info
|
30
|
+
```
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
|
+
|
36
|
+
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).
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/youtube_parser. 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.
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
45
|
+
|
46
|
+
## Code of Conduct
|
47
|
+
|
48
|
+
Everyone interacting in the YoutubeParser project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/youtube_parser/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "youtube_parser"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'uri'
|
5
|
+
require 'oj'
|
6
|
+
require 'faraday'
|
7
|
+
require 'ostruct'
|
8
|
+
require 'yaml'
|
9
|
+
require 'pathname'
|
10
|
+
|
11
|
+
require 'youtube_parser/version'
|
12
|
+
require 'youtube_parser/resource'
|
13
|
+
require 'youtube_parser/client'
|
14
|
+
require 'youtube_parser/base_parser'
|
15
|
+
require 'youtube_parser/channel'
|
16
|
+
require 'youtube_parser/channels/about_section'
|
17
|
+
require 'youtube_parser/channels/videos_section'
|
18
|
+
|
19
|
+
module YoutubeParser
|
20
|
+
def self.root
|
21
|
+
Pathname.new(Dir.pwd)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
class BaseParser
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@options = Resource.new(options)
|
9
|
+
|
10
|
+
required_options.each do |option|
|
11
|
+
message = "missing keyword: #{option}"
|
12
|
+
|
13
|
+
raise ArgumentError, message unless @options.public_send(option)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def resource
|
18
|
+
@resource ||= Resource.new(info)
|
19
|
+
end
|
20
|
+
|
21
|
+
def info
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.options(*opts)
|
26
|
+
define_method(:required_options) { opts }
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def keys
|
32
|
+
return @keys if @keys
|
33
|
+
|
34
|
+
path = YoutubeParser.root.join('lib/youtube_parser/config/keys.yml')
|
35
|
+
@keys = Resource.new(YAML.load_file(path).merge(default_method_value: []))
|
36
|
+
end
|
37
|
+
|
38
|
+
def client
|
39
|
+
@client ||= options.client || Client.new(options.attributes)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Default definition
|
43
|
+
def required_options
|
44
|
+
[]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
class Channel < BaseParser
|
5
|
+
options :channel_url
|
6
|
+
|
7
|
+
def info
|
8
|
+
@info ||= Resource.new(collect_channel_info)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def collect_channel_info
|
14
|
+
data = about_section_info.merge(video_section_info)
|
15
|
+
data.select! { |_, v| v.present? }
|
16
|
+
end
|
17
|
+
|
18
|
+
def about_section_info
|
19
|
+
@about_section_info ||= section(:about).info
|
20
|
+
end
|
21
|
+
|
22
|
+
def video_section_info
|
23
|
+
@video_section_info ||= section(:videos).info
|
24
|
+
end
|
25
|
+
|
26
|
+
def section(title)
|
27
|
+
constant_name = "YoutubeParser::Channels::#{title.capitalize}Section"
|
28
|
+
opts = { channel_url: options.channel_url, client: client }
|
29
|
+
Object.const_get(constant_name).new(opts)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
module Channels
|
5
|
+
class AboutSection < BaseParser
|
6
|
+
options :channel_url
|
7
|
+
|
8
|
+
def info
|
9
|
+
{
|
10
|
+
title: title,
|
11
|
+
description: description,
|
12
|
+
country: country,
|
13
|
+
tags: tags,
|
14
|
+
views_count: views_count,
|
15
|
+
followers_count: followers_count,
|
16
|
+
videos_count: videos_count,
|
17
|
+
avatar_url: avatar_url
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def title
|
24
|
+
section.dig(*keys.title)
|
25
|
+
end
|
26
|
+
|
27
|
+
def email
|
28
|
+
regex = /#{URI::MailTo::EMAIL_REGEXP.source.gsub(/\\A|\\z/, '')}/
|
29
|
+
description[regex]
|
30
|
+
end
|
31
|
+
|
32
|
+
def description
|
33
|
+
descriptions = [section.dig(*keys.description_first),
|
34
|
+
section.dig(*keys.description_second)]
|
35
|
+
descriptions.compact.join(' ')
|
36
|
+
end
|
37
|
+
|
38
|
+
def country
|
39
|
+
section.dig(*keys.country)
|
40
|
+
end
|
41
|
+
|
42
|
+
def tags
|
43
|
+
section.dig(*keys.keywords)
|
44
|
+
end
|
45
|
+
|
46
|
+
def avatar_url
|
47
|
+
results = section.dig(*keys.avatar) || section.dig(*keys.meta_avatar)
|
48
|
+
return if results.nil? || results.empty?
|
49
|
+
|
50
|
+
results.detect { |t| t&.dig('url') }&.dig('url')
|
51
|
+
end
|
52
|
+
|
53
|
+
def views_count
|
54
|
+
views_str = section.dig(*keys.views)&.map(&:values)&.join
|
55
|
+
return unless views_str
|
56
|
+
|
57
|
+
(views_str.gsub(/\D+/, '') || statistics(:views_count))
|
58
|
+
end
|
59
|
+
|
60
|
+
def followers_count
|
61
|
+
statistics :followers_count
|
62
|
+
end
|
63
|
+
|
64
|
+
def videos_count
|
65
|
+
statistics :videos_count
|
66
|
+
end
|
67
|
+
|
68
|
+
def statistics(title)
|
69
|
+
indexes = { videos_count: 1, views_count: 2, followers_count: 3 }
|
70
|
+
stats = section.dig(*keys.statistics) || []
|
71
|
+
|
72
|
+
stats[indexes[title.to_sym].to_i].to_s.gsub(/\D+/, '')
|
73
|
+
end
|
74
|
+
|
75
|
+
def section
|
76
|
+
return @section if @section
|
77
|
+
|
78
|
+
@section = json.dig(*keys.section_tabs)&.detect do |tab|
|
79
|
+
tab.dig(*keys.about_section_tab)
|
80
|
+
end
|
81
|
+
@section = @section&.dig(*keys.about_section_tab) || {}
|
82
|
+
end
|
83
|
+
|
84
|
+
def json
|
85
|
+
@json ||= client.get("#{options.channel_url}/about")
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
module Channels
|
5
|
+
class VideosSection < BaseParser
|
6
|
+
options :channel_url
|
7
|
+
|
8
|
+
def info
|
9
|
+
{
|
10
|
+
video_ids: video_ids,
|
11
|
+
playlist_id: playlist_id
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def video_ids
|
18
|
+
return @video_ids if @video_ids&.any?
|
19
|
+
|
20
|
+
sections.each do |section|
|
21
|
+
videos = section.dig(*keys.video_section_tabs)
|
22
|
+
next unless videos.is_a? Array
|
23
|
+
|
24
|
+
@video_ids = videos.map { |v| v.dig(*keys.video_ids) }.compact
|
25
|
+
|
26
|
+
return @video_ids if @video_ids.any?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def playlist_id
|
31
|
+
return @playlist_id if @playlist_id
|
32
|
+
|
33
|
+
section = sections.detect { |s| s.dig(*keys.playlist_id) }
|
34
|
+
@playlist_id = section&.dig(*keys.playlist_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
def sections
|
38
|
+
@sections ||= json.dig(*keys.section_tabs) || []
|
39
|
+
end
|
40
|
+
|
41
|
+
def json
|
42
|
+
@json ||= client.get("#{options.channel_url}/videos")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
class Client
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
BASE_URL = 'https://www.youtube.com/'
|
8
|
+
DATA_REGEX = /window\[\"ytInitialData\"\]\s*\=\s*(\{[\w\W]+?\})\;/.freeze
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@options = Resource.new(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(endpoint, options = {})
|
15
|
+
response = client.get(endpoint, options)
|
16
|
+
json_str = response.body[DATA_REGEX, 1]
|
17
|
+
|
18
|
+
get_json json_str
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def client
|
24
|
+
opts = { ssl: { verify: false }, request: { timeout: 10 } }
|
25
|
+
@client ||= Faraday.new(BASE_URL, opts) do |request|
|
26
|
+
request.adapter Faraday.default_adapter
|
27
|
+
request.headers['User-Agent'] = options.user_agent if options.user_agent
|
28
|
+
request.proxy = proxy if options.proxy
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def proxy
|
33
|
+
URI(options.proxy)
|
34
|
+
rescue URI::InvalidURIError
|
35
|
+
URI("//#{options.proxy}")
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_json(json_str)
|
39
|
+
Oj.load(json_str)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
channel_url: ['navigationEndpoint', 'browseEndpoint', 'canonicalBaseUrl']
|
2
|
+
channel_title: ['title', 'simpleText']
|
3
|
+
subscribers_count: ['subscriberCountText', 'simpleText']
|
4
|
+
section_tabs: ['contents', 'twoColumnBrowseResultsRenderer', 'tabs']
|
5
|
+
about_section_tab: ['tabRenderer', 'content', 'sectionListRenderer',
|
6
|
+
'contents', 0, 'itemSectionRenderer', 'contents', 0,
|
7
|
+
'channelAboutFullMetadataRenderer']
|
8
|
+
video_section_tabs: ['tabRenderer', 'content', 'sectionListRenderer',
|
9
|
+
'contents', 0, 'itemSectionRenderer', 'contents', 0, 'gridRenderer', 'items']
|
10
|
+
playlist_id: ['tabRenderer', 'content', 'sectionListRenderer', 'subMenu',
|
11
|
+
'channelSubMenuRenderer', 'playAllButton', 'buttonRenderer',
|
12
|
+
'navigationEndpoint', 'watchPlaylistEndpoint', 'playlistId']
|
13
|
+
video_ids: ['gridVideoRenderer', 'videoId']
|
14
|
+
country: ['country', 'simpleText']
|
15
|
+
description_first: ['description', 'simpleText']
|
16
|
+
description_second: ['artistBio', 'simpleText']
|
17
|
+
meta_avatar: ['metadata', 'channelMetadataRenderer', 'avatar', 'thumbnails']
|
18
|
+
avatar: ['avatar', 'thumbnails']
|
19
|
+
tags: ['microformat', 'microformatDataRenderer', 'tags']
|
20
|
+
views: ['viewCountText', 'runs']
|
21
|
+
keywords: ['metadata', 'channelMetadataRenderer', 'keywords']
|
22
|
+
tab_renderer: ['tabRenderer']
|
23
|
+
title: ['title', 'simpleText']
|
24
|
+
statistics: ['networkStatistics', 'statistics']
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module YoutubeParser
|
4
|
+
class Resource < OpenStruct
|
5
|
+
def initialize(hash = {})
|
6
|
+
@hash = hash
|
7
|
+
@default_method_value = hash.delete(:default_method_value)
|
8
|
+
|
9
|
+
super(hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
def attributes
|
13
|
+
@attributes ||= @hash.transform_keys(&:to_sym)
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method, *args, &block)
|
17
|
+
super_method = super
|
18
|
+
return super_method if super_method
|
19
|
+
|
20
|
+
@default_method_value
|
21
|
+
end
|
22
|
+
|
23
|
+
def respond_to_missing?(method_name, include_private = false)
|
24
|
+
@hash.keys.include?(method_name) ||
|
25
|
+
@hash.keys.map(&:to_s).include?(method_name) || super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'youtube_parser/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'youtube_parser'
|
8
|
+
spec.version = YoutubeParser::VERSION
|
9
|
+
spec.authors = ['o.vykhor']
|
10
|
+
spec.email = ['o.vykhor@ukr.net']
|
11
|
+
|
12
|
+
spec.summary = %q{Gem for parsing youtube data}
|
13
|
+
spec.homepage = 'https://github.com/oleksiivykhor/youtube_parser'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
spec.add_development_dependency 'byebug'
|
29
|
+
spec.add_development_dependency 'webmock'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: youtube_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- o.vykhor
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-01 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- o.vykhor@ukr.net
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CODE_OF_CONDUCT.md
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/youtube_parser.rb
|
103
|
+
- lib/youtube_parser/base_parser.rb
|
104
|
+
- lib/youtube_parser/channel.rb
|
105
|
+
- lib/youtube_parser/channels/about_section.rb
|
106
|
+
- lib/youtube_parser/channels/videos_section.rb
|
107
|
+
- lib/youtube_parser/client.rb
|
108
|
+
- lib/youtube_parser/config/keys.yml
|
109
|
+
- lib/youtube_parser/resource.rb
|
110
|
+
- lib/youtube_parser/version.rb
|
111
|
+
- youtube_parser.gemspec
|
112
|
+
homepage: https://github.com/oleksiivykhor/youtube_parser
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata: {}
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
requirements: []
|
131
|
+
rubygems_version: 3.0.3
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Gem for parsing youtube data
|
135
|
+
test_files: []
|