finviz 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 00af63d425682ee661876980893de15c459f6d78d9394bc660c2af3d5608db1c
4
+ data.tar.gz: 93bcb27731deb01bd96eced8484dd54d4719fd68bee2c6a1fa04a9dc90fde7e2
5
+ SHA512:
6
+ metadata.gz: c18f0295ffcc25de97cfb2f6c659805348a71a6ee5e8dcec131e0777094b378043b701a8f55d09ccd923af58557742c7dc00b14c5fb8eb72200b2d8a9dc53c21
7
+ data.tar.gz: 2f0c1ba99a6016fdef6c79998018ce043993c06c0846bac51ae6688bfcb6e997d6c9e6c57082766354397b7897e40c44ff4eb44defa7bedbf5c66b13ed7a828e
@@ -0,0 +1,70 @@
1
+ version: 2.1
2
+ defaults: &defaults
3
+ docker:
4
+ - image: circleci/ruby:2.6.3-stretch-node
5
+ environment:
6
+ BUNDLE_PATH: vendor/bundle
7
+ # executor: ruby/default
8
+ commands:
9
+ prepare:
10
+ description: "Common preparation steps"
11
+ steps:
12
+ - checkout
13
+ - restore_cache:
14
+ keys:
15
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
16
+ # fallback to using the latest cache if no exact match is found
17
+ - v1-dependencies-
18
+ - run:
19
+ name: install bundler
20
+ command: |
21
+ gem install bundler -v 2.2.16
22
+
23
+ - run:
24
+ name: install dependencies
25
+ command: |
26
+ bundle config set path 'vendor/bundle'
27
+ bundle install --jobs=4 --retry=3
28
+ - save_cache:
29
+ paths:
30
+ - ./vendor/bundle
31
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
32
+
33
+ jobs:
34
+ rspec:
35
+ <<: *defaults
36
+ steps:
37
+ - prepare
38
+ - run:
39
+ name: Rspec
40
+ command: |
41
+ mkdir /tmp/test-results
42
+
43
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
44
+
45
+ bundle exec rspec --format progress \
46
+ --format RspecJunitFormatter \
47
+ --out /tmp/test-results/rspec.xml \
48
+ --tag ~type:performance \
49
+ $TEST_FILES
50
+ # collect reports
51
+ - store_test_results:
52
+ path: /tmp/test-results
53
+ - store_artifacts:
54
+ path: /tmp/test-results
55
+ destination: test-results
56
+
57
+ rubocop:
58
+ <<: *defaults
59
+ steps:
60
+ - prepare
61
+ - run:
62
+ name: Rubocop
63
+ command: bundle exec rubocop
64
+
65
+ workflows:
66
+ version: 2
67
+ pipeline:
68
+ jobs:
69
+ - rspec
70
+ - rubocop
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - spec/**/*
19
+
20
+ Metrics/AbcSize:
21
+ Max: 18
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-05-16
4
+
5
+ - Initial release
6
+
7
+ ## [0.2.0] - 2021-05-17
8
+
9
+ - Added tickers fetcher
10
+ - Added quotes fetcher
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kvokka@yahoo.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in finviz.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "pry", "~> 0.14.0"
11
+ gem "rubocop", "~> 1.14.0", require: false
12
+ gem "rubocop-rake", "~> 0.5.0", require: false
13
+ gem "rubocop-rspec", "~> 2.3.0", require: false
14
+
15
+ group :test do
16
+ gem "rspec", ">= 3.10.0", "< 4.0"
17
+ gem "rspec_junit_formatter"
18
+ gem "vcr", "~> 6.0.0"
19
+ gem "webmock", "~> 3.13.0"
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 kvokka
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,71 @@
1
+ [![CircleCI](https://circleci.com/gh/kvokka/finviz.svg?style=svg&circle-token=804e72e82fd4ccedc8f06cb332cc53d21e83535c)](https://circleci.com/gh/kvokka/finviz)
2
+ [![Gem Version](https://img.shields.io/gem/v/finviz.svg)](https://rubygems.org/gems/finviz)
3
+ [![Reviewed by Hound](https://img.shields.io/badge/Reviewed_by-Hound-8E64B0.svg)](https://houndci.com)
4
+
5
+ # Finviz
6
+
7
+ Unofficial finviz.com API
8
+
9
+ All requests are concurrent and lighting fast (using
10
+ [async-http](https://github.com/socketry/async-http) under the hood).
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'finviz'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle install
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install finviz
27
+
28
+ ## Usage
29
+
30
+ ### Fetch tickers list from user filter
31
+
32
+ Since it is more convenient to use web based interface to set the filters, there
33
+ are 2 options to filter tickers.
34
+
35
+ ```ruby
36
+ # with raw url
37
+ Finviz.tickers uri: 'https://finviz.com/screener.ashx?v=211&f=fa_pe_u5,sh_price_u3&ft=4'
38
+ => ["BHAT", "CEPU", "CIH", "CMCM", "DLNG", "EDTK", "FENG", "JOB", "QD", "SND", "SUPV"]
39
+
40
+ # with manual filters cherry-picking
41
+ Finviz.tickers filters: %w[fa_pe_u5 sh_price_u3]
42
+ => ["BHAT", "CEPU", "CIH", "CMCM", "DLNG", "EDTK", "FENG", "JOB", "QD", "SND", "SUPV"]
43
+ ```
44
+
45
+ ### Fetch quotes
46
+
47
+ Each quote contain the Hash with ticker stats and link to the chart picture, see
48
+ example:
49
+
50
+ ```ruby
51
+ Finviz.quotes tickers: %w[a c]
52
+ => [#<OpenStruct ticker="A", chart="https://charts2.finviz.com/chart.ashx?t=C&ty=c&ta=1&p=d&s=l", stats={"Index"=>"S&P 500", "P/E"=>"50.35", "EPS (ttm)"=>"2.60", "Insider Own"=>"0.30%", "Shs Outstand"=>"306.00M", "Perf Week"=>"-2.05%", "Market Cap"=>"39.62B", "Forward P/E"=>"30.11", "EPS next Y"=>"11.78%", "Insider Trans"=>"-21.60%", "Shs Float"=>"303.84M", "Perf Month"=>"-1.60%", "Income"=>"810.00M", "PEG"=>"4.66", "EPS next Q"=>"0.83", "Inst Own"=>"91.60%", "Short Float"=>"0.70%", "Perf Quarter"=>"2.48%", "Sales"=>"5.53B", "P/S"=>"7.16", "EPS this Y"=>"-31.60%", "Inst Trans"=>"0.76%", "Short Ratio"=>"1.29", "Perf Half Y"=>"19.87%", "Book/sh"=>"15.70", "P/B"=>"8.35", "ROA"=>"8.50%", "Target Price"=>"140.23", "Perf Year"=>"60.64%", "Cash/sh"=>"4.40", "P/C"=>"29.81", "EPS next 5Y"=>"10.80%", "ROE"=>"16.70%", "52W Range"=>"78.72 - 137.83", "Perf YTD"=>"10.68%", "Dividend"=>"0.78", "P/FCF"=>"45.69", "EPS past 5Y"=>"12.00%", "ROI"=>"10.00%", "52W High"=>"-4.84%", "Beta"=>"1.01", "Dividend %"=>"0.59%", "Quick Ratio"=>"1.60", "Sales past 5Y"=>"5.70%", "Gross Margin"=>"53.30%", "52W Low"=>"66.60%", "ATR"=>"2.09", "Employees"=>"16400", "Current Ratio"=>"2.10", "Sales Q/Q"=>"14.10%", "Oper. Margin"=>"17.30%", "RSI (14)"=>"49.02", "Volatility"=>"1.56% 1.40%", "Optionable"=>"Yes", "Debt/Eq"=>"0.52", "EPS Q/Q"=>"48.10%", "Profit Margin"=>"14.60%", "Rel Volume"=>"0.47", "Prev Close"=>"130.02", "Shortable"=>"Yes", "LT Debt/Eq"=>"0.45", "Earnings"=>"May 25 AMC", "Payout"=>"27.40%", "Avg Volume"=>"1.65M", "Price"=>"131.15", "Recom"=>"1.90", "SMA20"=>"-1.68%", "SMA50"=>"2.23%", "SMA200"=>"13.91%", "Volume"=>"770,838", "Change"=>"0.87%"}>, #<OpenStruct ticker="C", chart="https://charts2.finviz.com/chart.ashx?t=C&ty=c&ta=1&p=d&s=l", stats={"Index"=>"S&P 500", "P/E"=>"10.50", "EPS (ttm)"=>"7.29", "Insider Own"=>"0.20%", "Shs Outstand"=>"2.08B", "Perf Week"=>"1.97%", "Market Cap"=>"155.63B", "Forward P/E"=>"9.32", "EPS next Y"=>"-8.81%", "Insider Trans"=>"-1.30%", "Shs Float"=>"2.04B", "Perf Month"=>"5.54%", "Income"=>"15.26B", "PEG"=>"0.96", "EPS next Q"=>"2.00", "Inst Own"=>"78.80%", "Short Float"=>"1.20%", "Perf Quarter"=>"20.32%", "Sales"=>"53.48B", "P/S"=>"2.91", "EPS this Y"=>"-41.10%", "Inst Trans"=>"0.52%", "Short Ratio"=>"1.26", "Perf Half Y"=>"56.47%", "Book/sh"=>"87.55", "P/B"=>"0.87", "ROA"=>"0.70%", "Target Price"=>"85.23", "Perf Year"=>"82.03%", "Cash/sh"=>"486.62", "P/C"=>"0.16", "EPS next 5Y"=>"10.91%", "ROE"=>"8.60%", "52W Range"=>"38.76 - 76.84", "Perf YTD"=>"24.16%", "Dividend"=>"2.04", "P/FCF"=>"7.76", "EPS past 5Y"=>"-2.70%", "ROI"=>"6.80%", "52W High"=>"-0.36%", "Beta"=>"1.90", "Dividend %"=>"2.66%", "Quick Ratio"=>"-", "Sales past 5Y"=>"-0.20%", "Gross Margin"=>"-", "52W Low"=>"97.52%", "ATR"=>"1.72", "Employees"=>"211000", "Current Ratio"=>"-", "Sales Q/Q"=>"-26.90%", "Oper. Margin"=>"64.00%", "RSI (14)"=>"65.40", "Volatility"=>"2.44% 2.21%", "Optionable"=>"Yes", "Debt/Eq"=>"2.70", "EPS Q/Q"=>"240.40%", "Profit Margin"=>"28.50%", "Rel Volume"=>"0.72", "Prev Close"=>"75.29", "Shortable"=>"Yes", "LT Debt/Eq"=>"1.35", "Earnings"=>"Apr 15 BMO", "Payout"=>"27.80%", "Avg Volume"=>"19.50M", "Price"=>"76.56", "Recom"=>"2.10", "SMA20"=>"5.13%", "SMA50"=>"5.42%", "SMA200"=>"30.60%", "Volume"=>"14,074,177", "Change"=>"1.69%"}>]
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kvokka/finviz. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/finviz/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Finviz project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/kvokka/finviz/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "finviz"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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
data/finviz.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/finviz/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "finviz"
7
+ spec.version = Finviz::VERSION
8
+ spec.authors = ["kvokka"]
9
+ spec.email = ["kvokka@yahoo.com"]
10
+
11
+ spec.summary = "Unofficial Finviz API."
12
+ spec.description = "Easy way tp access data from finviz.com."
13
+ spec.homepage = "https://github.com/kvokka/finviz"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/kvokka/finviz"
19
+ spec.metadata["changelog_uri"] = "https://github.com/kvokka/finviz/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_dependency "activesupport", "~> 6.1.0"
32
+ spec.add_dependency "async-http", "~> 0.56.0"
33
+ spec.add_dependency "dry-configurable", "~> 0.12.0"
34
+ spec.add_dependency "nokogiri", "~> 1.11.0"
35
+ spec.add_dependency "zeitwerk", "~> 2.4.0"
36
+
37
+ # For more information and examples about making a new gem, checkout our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
39
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "async"
4
+ require "async/barrier"
5
+ require "async/semaphore"
6
+ require "async/http/internet"
7
+
8
+ module Finviz
9
+ # Async way to fetch the data
10
+ class Crawler
11
+ class << self
12
+ def call(*args)
13
+ new(*args).tap(&:fetch).result
14
+ end
15
+ end
16
+
17
+ def initialize(paths: [])
18
+ @paths = Array(paths)
19
+ end
20
+
21
+ attr_reader :paths
22
+
23
+ def fetch
24
+ async do
25
+ paths.each do |path|
26
+ response = internet.get(path.to_s, headers)
27
+ result << OpenStruct.new(path: path, html: Nokogiri::HTML(response.read))
28
+ end
29
+ end
30
+ end
31
+
32
+ def result
33
+ @result ||= []
34
+ end
35
+
36
+ private
37
+
38
+ def async
39
+ Async do |task|
40
+ # Spawn an asynchronous task for each topic:
41
+ task.with_timeout(Finviz.config.timeout) do
42
+ yield task
43
+
44
+ # Ensure we wait for all requests to complete before continuing:
45
+ barrier.wait
46
+ end
47
+ ensure
48
+ internet&.close
49
+ end
50
+ end
51
+
52
+ def internet
53
+ @internet ||= Async::HTTP::Internet.new
54
+ end
55
+
56
+ def barrier
57
+ @barrier ||= Async::Barrier.new
58
+ end
59
+
60
+ def headers
61
+ [
62
+ [
63
+ "User-Agent",
64
+ "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:#{rand(47..80)}.0) Gecko/20100101 Firefox/#{rand(47..80)}.0"
65
+ ]
66
+ ]
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Finviz
4
+ # Fetch the details of the security
5
+ class QuotesFetcher
6
+ def initialize(tickers: [])
7
+ @tickers = Array(tickers)
8
+ end
9
+
10
+ attr_reader :tickers
11
+
12
+ def call
13
+ add_stats_to_results
14
+ add_charts_to_results
15
+ results.flatten!
16
+ end
17
+
18
+ private
19
+
20
+ def add_stats_to_results
21
+ all_pages.each_with_index do |page, page_number|
22
+ page.html.css(".snapshot-table2").each_with_index do |xpath, selector_offset|
23
+ current = results[page_number][selector_offset]
24
+ current.stats = xpath.css(".table-dark-row").map { |row| row.css("td").map(&:text) }
25
+ .flatten.each_slice(2).to_h
26
+ end
27
+ end
28
+ end
29
+
30
+ def add_charts_to_results
31
+ all_pages.each_with_index do |page, page_number|
32
+ page.html.css("#chart1").each_with_index do |xpath, selector_offset|
33
+ current = results[page_number][selector_offset]
34
+ current.chart = xpath.attributes["src"].text
35
+ end
36
+ end
37
+ end
38
+
39
+ def results
40
+ @results ||= all_pages.map do |page|
41
+ page.html.css("#ticker").map do |xpath|
42
+ ticker = xpath.children.text
43
+ OpenStruct.new ticker: ticker, chart: "https://charts2.finviz.com/chart.ashx?t=#{ticker}&ty=c&ta=1&p=d&s=l"
44
+ end
45
+ end
46
+ end
47
+
48
+ def all_pages
49
+ @all_pages ||= Crawler.call(paths: paths)
50
+ end
51
+
52
+ def paths
53
+ @paths ||= tickers
54
+ .each_slice(Finviz.config.quotes_fetcher.max_tickers_per_page)
55
+ .map { |slice| uri slice }
56
+ end
57
+
58
+ def uri(tickers_array)
59
+ query = CGI.unescape(URI.encode_www_form({ t: tickers_array.join(",") }))
60
+ URI::HTTPS.build(host: "finviz.com", path: "/quote.ashx", query: query)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Finviz
4
+ # Fetch the list of tickers basing on the provided uri or array of filters
5
+ class TickersFetcher
6
+ attr_reader :uri
7
+
8
+ def initialize(uri: nil, filters: [], **rest)
9
+ raise("You should provide either uri or filters") if uri.present? && filters.present?
10
+ raise("Do not support option #{rest.keys}") unless rest.empty?
11
+
12
+ normalize_uri(uri, filters)
13
+ end
14
+
15
+ def call
16
+ all_pages = first_page + remaining_pages
17
+ all_pages
18
+ .map { |p| p.html.css("table span[onclick]") }
19
+ .flatten
20
+ .map { |a| a.children.text.gsub(/[[:space:]]+/, "") }
21
+ end
22
+
23
+ private
24
+
25
+ def first_page
26
+ @first_page ||= Crawler.call(paths: uri)
27
+ end
28
+
29
+ def remaining_pages
30
+ return @remaining_pages if @remaining_pages
31
+
32
+ max_page = first_page.first.html.css("a.screener-pages").map { |a| a.children.text.to_i }.max
33
+ return @remaining_pages = [] unless max_page
34
+
35
+ paths = generate_further_pages(max_page)
36
+ @remaining_pages = Crawler.call(paths: paths)
37
+ end
38
+
39
+ def normalize_uri(uri, filters)
40
+ uri = URI(uri || base_uri)
41
+ query = CGI.parse(uri.query || "").tap do |params|
42
+ params["f"] = filters&.join(",") if filters&.any?
43
+ params["v"] = finviz_view_type
44
+ params.delete("r") # remove offset if it was provided
45
+ end
46
+ @uri = URI::HTTPS.build(host: base_uri, path: base_path, query: CGI.unescape(URI.encode_www_form(query)))
47
+ end
48
+
49
+ def base_uri
50
+ "finviz.com"
51
+ end
52
+
53
+ def base_path
54
+ "/screener.ashx"
55
+ end
56
+
57
+ # Output only tickers to reduce pages count
58
+ def finviz_view_type
59
+ 411
60
+ end
61
+
62
+ # In tickers view we can get 1k of tickers per request.
63
+ # It is hardcoded be finviz
64
+ def per_page
65
+ 1_000
66
+ end
67
+
68
+ def generate_further_pages(number)
69
+ (1...number).map do |i|
70
+ uri.dup.tap do |inner_uri|
71
+ new_query_ar = URI.decode_www_form(inner_uri.query) << ["r", i * per_page + 1]
72
+ inner_uri.query = URI.encode_www_form(new_query_ar)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Finviz
4
+ VERSION = "0.2.0"
5
+ end
data/lib/finviz.rb ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup # ready!
6
+
7
+ require "dry-configurable"
8
+ require "active_support"
9
+ require "active_support/core_ext/hash/conversions"
10
+ require "nokogiri"
11
+ require "uri"
12
+ require "cgi"
13
+
14
+ # Main module containing the settings and top-level methods
15
+ module Finviz
16
+ class Error < StandardError; end
17
+
18
+ extend Dry::Configurable
19
+
20
+ setting :timeout, 120 # seconds
21
+ setting :quotes_fetcher do
22
+ # Was manually found by feeding
23
+ # Finviz::TickersFetcher.new.call.first(400).join(',')
24
+ # to QuotesFetcher#uri method that max acceptable value is 400
25
+ # prefer to keep it lower to increase concurrency
26
+ setting :max_tickers_per_page, 100
27
+ end
28
+
29
+ class << self
30
+ def tickers(**opts)
31
+ TickersFetcher.new(**opts).call
32
+ end
33
+
34
+ def quotes(**opts)
35
+ QuotesFetcher.new(**opts).call
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finviz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - kvokka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.56.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.56.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dry-configurable
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.12.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.12.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.11.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.11.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: zeitwerk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.4.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.4.0
83
+ description: Easy way tp access data from finviz.com.
84
+ email:
85
+ - kvokka@yahoo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".rubocop.yml"
94
+ - CHANGELOG.md
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - finviz.gemspec
103
+ - lib/finviz.rb
104
+ - lib/finviz/crawler.rb
105
+ - lib/finviz/quotes_fetcher.rb
106
+ - lib/finviz/tickers_fetcher.rb
107
+ - lib/finviz/version.rb
108
+ homepage: https://github.com/kvokka/finviz
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ homepage_uri: https://github.com/kvokka/finviz
113
+ source_code_uri: https://github.com/kvokka/finviz
114
+ changelog_uri: https://github.com/kvokka/finviz/blob/master/CHANGELOG.md
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 2.6.0
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.1.4
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Unofficial Finviz API.
134
+ test_files: []