mtgtop8_scrapper 0.0.4

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: 6747361b0fb4574105a6af90cd26976e25b78724bc69e794501b8557e274c11d
4
+ data.tar.gz: b535056d9f3e9ecd64ada389abbf46b0511e52deed8980706d22b0202ae57c0a
5
+ SHA512:
6
+ metadata.gz: b72bd1b861ff38ffda55998565131e47d7b0b2c360b54eba45ade585b54f6542cef441d4a76b37f71c0279650de38bad02ab8cbcbee6d62159b7d8ee54ccd51a
7
+ data.tar.gz: d119c69d177d367c756527a563c09923021218d44cd864422a0ee4a94159b018aed7659399be4c5867d623f477a0fd14de43584f558a5b0777b40579b925979f
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ <h1 align="center">Welcome to MTGTop8 scrapper πŸ‘‹</h1>
2
+
3
+
4
+ <div align="center">
5
+ <img alt="logo" src="assets/logo.png" />
6
+ </div>
7
+
8
+ <p>
9
+ <img alt="Version" src="https://img.shields.io/badge/version-4-blue.svg?cacheSeconds=2592000" />
10
+ <a href="https://twitter.com/kammzinho" target="_blank">
11
+ <img alt="Twitter: kammzinho" src="https://img.shields.io/twitter/follow/kammzinho.svg?style=social" />
12
+ </a>
13
+ </p>
14
+
15
+ > Scrape data from MTGTop8, Magic the Gathering tournaments, results and decks into simple reports.
16
+
17
+
18
+ ## How it looks
19
+
20
+ ### Convert this:
21
+
22
+ ![img.png](assets/demo.png)
23
+
24
+ ### Into this:
25
+
26
+
27
+ https://user-images.githubusercontent.com/34798570/220813931-7d3d73db-6b1e-4564-86ea-8583ba12509a.mov
28
+
29
+
30
+
31
+
32
+ ## Why?
33
+ - πŸ₯Ή Mixing coding and Magic πŸ’›
34
+ - 🎲 Being able to extract & format in a fast/easy way is really helpful, so it's possible to play with it around later.
35
+ - πŸ“ˆ Reports are being used to generate Monthly Retrospectives related to the weekly tournaments around the city.
36
+ - πŸ§‘β€πŸ’» All of them are available [here, in mtgjoinville.super.site](https://mtgjoinville.super.site/)
37
+
38
+ ### The current ecosystem
39
+
40
+ - [MTG Joinville Website](https://mtgjoinville.super.site/)
41
+ - πŸ˜‰ Entrypoint for a bunch of information related to Magic in our city
42
+ - [MTGTop8 Scrapper](https://github.com/kammradt/mtgtop8-scrapper)
43
+ - πŸͺ“ Will gather data from the main website that holds results from many tournaments
44
+ - [MTG Top/Ranking generator?]
45
+ - Use reports from [MTGTop8 Scrapper](https://github.com/kammradt/mtgtop8-scrapper) to build social media ready images showing top 8 players and their decks.
46
+ - This is being currently build and there are some [examples/prototypes here](https://mtgjoinville.super.site/noticias/janeiro2023-piooner-retrospectiva).
47
+ - <details>
48
+
49
+ <summary>Some image examples</summary>
50
+
51
+ <img alt="retro" src="assets/retro.png" />
52
+ <img alt="retro1" src="assets/retro1.png" />
53
+ </details>
54
+
55
+ ## Install
56
+ > None of the builds/versions will probably be stable. This is a kind of pet project, so use at your own risk.
57
+
58
+ ```sh
59
+ gem install mtgtop8_scrapper
60
+ ```
61
+
62
+ ## Usage
63
+
64
+ > Feel free to copy it from examples.rb file
65
+
66
+ ```sh
67
+ require 'mtgtop8_scrapper'
68
+
69
+ link = 'https://www.mtgtop8.com/event?e=41158&d=505864&f=PAU'
70
+
71
+ # Just create an instance passing a link as argument
72
+ scrapper = MTGTop8Scrapper.new(link)
73
+
74
+ # This generates and returns a report
75
+ scrapper.generate_report
76
+ # That you can also access later
77
+ puts scrapper.report
78
+
79
+ # You can also save the report locally as json
80
+ scrapper.save_report_locally_as_json
81
+ ```
82
+
83
+
84
+ ## Author
85
+
86
+ πŸ‘€ **VinΓ­cius Kammradt**
87
+
88
+ * Website: https://kammradt.super.site
89
+ * Twitter: [@kammzinho](https://twitter.com/kammzinho)
90
+ * Github: [@kammradt](https://github.com/kammradt)
91
+ * LinkedIn: [@vinicius-kammradt](https://linkedin.com/in/vinicius-kammradt)
92
+
93
+ ## Show your support
94
+
95
+ Give a ⭐️ if this project helped you!
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+ require_relative 'scrapper'
5
+
6
+ class MTGTop8Scrapper < Scrapper
7
+ def generate_report
8
+ @doc = doc_or_cache
9
+
10
+ players = top_8_players
11
+ format = event_format
12
+ date = event_date
13
+ event_link = event_link_without_prefix
14
+
15
+ @report = { players:, date:, format:, event_link: }
16
+ end
17
+
18
+ private
19
+
20
+ def top_8_players
21
+ # TODO: Handle events with less than/more than 8
22
+ indexes_for_top_8_player_table = (2..9).to_a
23
+
24
+ indexes_for_top_8_player_table.map { |i| map_doc_to_report(i) }.sort_by { |player| player[:rank] }
25
+ end
26
+
27
+ def map_doc_to_report(index)
28
+ rank = rank_value(index)
29
+ player = player_value(index)
30
+ deck = deck_value(index)
31
+
32
+ { rank:, player:, deck: }
33
+ end
34
+
35
+ def event_format
36
+ path = '/html/body/div/div/div[7]/div[1]/div/div[1]/div[1]/div[1]'
37
+
38
+ @doc.at(path).text.strip
39
+ end
40
+
41
+ def event_date
42
+ path = '/html/body/div/div/div[7]/div[1]/div/div[1]/div[1]/div[2]'
43
+
44
+ @doc.at(path).text.partition('-').last.strip
45
+ end
46
+
47
+ def rank_value(index)
48
+ path = "/html/body/div/div/div[7]/div[1]/div/div[1]/div[#{index}]/div/div[1]"
49
+
50
+ @doc.at(path).text.strip.to_i
51
+ end
52
+
53
+ def player_value(index)
54
+ path = "/html/body/div/div/div[7]/div[1]/div/div[1]/div[#{index}]/div/div[3]/div[2]/a"
55
+
56
+ @doc.at(path).text.strip
57
+ end
58
+
59
+ def deck_value(index)
60
+ path = "/html/body/div/div/div[7]/div[1]/div/div[1]/div[#{index}]/div/div[3]/div[1]"
61
+
62
+ @doc.at(path).text.strip
63
+ end
64
+ end
data/lib/scrapper.rb ADDED
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+ require 'json'
5
+
6
+ class Scrapper
7
+ attr_reader :report
8
+
9
+ def initialize(link)
10
+ @link = link
11
+ end
12
+
13
+ def generate_report
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def save_report_locally_as_json
18
+ File.write(filename_for_json, @report.to_json)
19
+
20
+ filename_for_json
21
+ end
22
+
23
+ private
24
+
25
+ def doc_or_cache
26
+ return File.open('offline/doc.html') { |f| Nokogiri::HTML(f) } if ENV['TESTING']
27
+
28
+ Nokogiri::HTML(URI.parse(@link).open)
29
+ end
30
+
31
+ def event_link_without_prefix
32
+ @link
33
+ .delete_prefix('https')
34
+ .delete_prefix('://')
35
+ .delete_prefix('www.')
36
+ end
37
+
38
+ def filename_for_json
39
+ event_link = @link.delete_prefix('https://www.mtgtop8.com/')
40
+
41
+ "#{event_link}.json"
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtgtop8_scrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Vinicius Kammradt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Scrape data from MTGTop8 into simple reports
14
+ email: vinicius.kammradt1@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/mtgtop8_scrapper.rb
21
+ - lib/scrapper.rb
22
+ homepage: https://rubygems.org/gems/mtgtop8_scrapper
23
+ licenses:
24
+ - MIT
25
+ metadata:
26
+ rubygems_mfa_required: 'true'
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 3.1.2
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.3.7
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Scrape data from MTGTop8 into simple reports
46
+ test_files: []