enter-rockstar 0.1

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: 587022dfb160e25d89d735b881451c94c89b9ca0eeb3228f3e500590f93a9fc2
4
+ data.tar.gz: 96e1ff6b6eb0e0ce212aa338eeb0ce60c2c460bd69178e822be5d37f45be84c3
5
+ SHA512:
6
+ metadata.gz: 26395477d080378a0a2d33d3d20307aed8ea23a7f273df94d2c9b538e388337111663a400a20a7c45cf322d93fd802ed3d47d741084575913ebcb97941dfd714
7
+ data.tar.gz: 0a027225b5e58fab4f20bbf35c8c0f3275bd26ce9427ec1b56507ef562eee9a2c30ce45f472c55b8d615f3979f55c839fdce18d1c564e6e688fe87dd4c823e64
data/.gitignore ADDED
@@ -0,0 +1,13 @@
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
+ .DS_Store
13
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics:
5
+ Enabled: false
6
+
7
+ Style/RescueModifier:
8
+ Enabled: false
9
+
10
+ Lint/InterpolationCheck:
11
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.2
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in enter-rockstar.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ enter-rockstar (0.1)
5
+ nokogiri
6
+ thor
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.6.0)
12
+ public_suffix (>= 2.0.2, < 4.0)
13
+ ast (2.4.0)
14
+ coderay (1.1.2)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ hashdiff (0.3.8)
19
+ jaro_winkler (1.5.2)
20
+ method_source (0.9.2)
21
+ mini_portile2 (2.4.0)
22
+ nokogiri (1.10.2)
23
+ mini_portile2 (~> 2.4.0)
24
+ parallel (1.15.0)
25
+ parser (2.6.2.0)
26
+ ast (~> 2.4.0)
27
+ pry (0.12.2)
28
+ coderay (~> 1.1.0)
29
+ method_source (~> 0.9.0)
30
+ psych (3.1.0)
31
+ public_suffix (3.0.3)
32
+ rainbow (3.0.0)
33
+ rake (12.3.2)
34
+ rspec (3.8.0)
35
+ rspec-core (~> 3.8.0)
36
+ rspec-expectations (~> 3.8.0)
37
+ rspec-mocks (~> 3.8.0)
38
+ rspec-core (3.8.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-expectations (3.8.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-mocks (3.8.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-support (3.8.0)
47
+ rubocop (0.66.0)
48
+ jaro_winkler (~> 1.5.1)
49
+ parallel (~> 1.10)
50
+ parser (>= 2.5, != 2.5.1.1)
51
+ psych (>= 3.1.0)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 1.6)
55
+ ruby-progressbar (1.10.0)
56
+ safe_yaml (1.0.5)
57
+ thor (0.20.3)
58
+ unicode-display_width (1.5.0)
59
+ vcr (4.0.0)
60
+ webmock (3.5.1)
61
+ addressable (>= 2.3.6)
62
+ crack (>= 0.3.2)
63
+ hashdiff
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ bundler (~> 2.0)
70
+ enter-rockstar!
71
+ pry
72
+ rake
73
+ rspec
74
+ rubocop
75
+ vcr
76
+ webmock
77
+
78
+ BUNDLED WITH
79
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Marcin Ruszkiewicz
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,64 @@
1
+ [![Build Status](https://travis-ci.org/marcinruszkiewicz/enter-rockstar.svg?branch=master)](https://travis-ci.org/marcinruszkiewicz/enter-rockstar)
2
+ [![Gem Version](https://badge.fury.io/rb/enter-rockstar.svg)](https://badge.fury.io/rb/enter-rockstar)
3
+
4
+ # Enter Rockstar - a tool to help with programming in Rockstar
5
+
6
+ This is a set of tools that help Rockstar programmers create programs in the [Rockstar language](https://github.com/RockstarLang/rockstar).
7
+
8
+ For details on what is done and what I'm still working on, see the TODO.md and CHANGELOG.md files.
9
+
10
+ ## Installation
11
+
12
+ Install the gem by issuing the following command.
13
+
14
+ ```
15
+ $ gem install enter-rockstar
16
+ ```
17
+
18
+ This gem works best on a current Ruby version and requires Ruby 2.3 at minimum. Running it on 2.3 has the downside of metal umlauts not being entirely correct as that Ruby version doesn't know how to `.downcase` a capital umlaut letter, which was fixed in 2.4.
19
+
20
+ If you're not using the umlauts (or at least are careful to only replace lowercase letters with them), all should be fine otherwise.
21
+
22
+ ## Setup
23
+
24
+ To start working with Enter Rockstar, you will need a word base to generate new lyrics from. To help with that task, the gem includes word data created from the "Heavy Metal" category, which should cover most common lyrics, however you can import a different category if you want.
25
+
26
+ ### Scraping a Wikia category page
27
+
28
+ You have to start with creating a list of all the pages in the Wikia's category, for example like this:
29
+
30
+ ```
31
+ $ enter-rockstar scrape_category power_metal /wiki/Category:Genre/Power_Metal
32
+ ```
33
+
34
+ This will create a file in `lyrics_data/wikia_power_metal.json` with all the links to pages in this category. Next you want to scrape actual lyric pages based on the links in the json file:
35
+
36
+ ```
37
+ $ enter-rockstar scrape_lyrics power_metal
38
+ ```
39
+
40
+ After this command finishes (which might take a long time depending on what category you use), you will have a set of directories with text files in the `lyrics` directory.
41
+
42
+ ### Generating a word base
43
+
44
+ Now that you have a set of lyrics, it's time to convert them into something that Enter Rockstar can use.
45
+
46
+ ## Generating lyrics
47
+
48
+ ### Finding words for poetic literals
49
+
50
+ The most common and basic function of Enter Rockstar is just finding interesting words of good length to use in the poetic numeral representations. Finding out what words to use to represent `123` is not as easy as it might sound and this makes it easier:
51
+
52
+ ```
53
+ $ enter-rockstar poetic 123
54
+ ```
55
+
56
+
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcinruszkiewicz/enter-rockstar. I'm also available for questions at the [Rockstar Developers Discord Group](https://discord.gg/kEUe5bM)
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'enter_rockstar'
6
+ require 'pry'
7
+
8
+ EnterRockstar.scrape_lyrics
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,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'enter_rockstar/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'enter-rockstar'
9
+ spec.version = EnterRockstar::VERSION
10
+ spec.authors = ['Marcin Ruszkiewicz']
11
+ spec.email = ['marcin.ruszkiewicz@polcode.net']
12
+
13
+ spec.summary = 'Generating helpful Rock phrases to make programming in Rockstar easier.'
14
+ spec.homepage = 'https://github.com/marcinruszkiewicz/enter-rockstar'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(spec|examples)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = ['enter-rockstar']
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.required_ruby_version = '>= 2.3'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 2.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'rubocop'
31
+ spec.add_development_dependency 'vcr'
32
+ spec.add_development_dependency 'webmock'
33
+ # spec.add_dependency 'hashie'
34
+ spec.add_dependency 'thor'
35
+ spec.add_dependency 'nokogiri'
36
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'rubygems'
5
+ require 'bundler/setup'
6
+ require 'enter_rockstar'
7
+ require 'enter_rockstar/cli'
8
+
9
+ EnterRockstar::CLI.start(ARGV)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require_relative 'version'
5
+
6
+ module EnterRockstar
7
+ # command line interface for enter-rockstar command
8
+ class CLI < Thor
9
+ package_name "Enter-Rockstar v#{EnterRockstar::VERSION}"
10
+
11
+ desc 'scrape_category CATEGORY_NAME URL', 'scrape lyrics wikia category page for bands and albums'
12
+ def scrape_category(category_name, url)
13
+ scraper = EnterRockstar::Scraper::Wikia.new(category_name: category_name, url: url)
14
+ scraper.parse_category(test_limit: true)
15
+ scraper.save_category
16
+ say
17
+ end
18
+
19
+ desc 'scrape_lyrics CATEGORY_NAME START_INDEX', 'scrape actual lyrics from the lyrics wikia using the generated json file'
20
+ def scrape_lyrics(category_name, start_index=0)
21
+ scraper = EnterRockstar::Scraper::Wikia.new(category_name: category_name)
22
+ scraper.load_saved_json
23
+ scraper.parse_all_pages(start_index: start_index)
24
+ end
25
+
26
+ desc 'print_indexed_tree CATEGORY_NAME', 'print tree with indexes'
27
+ def print_indexed_tree(category_name)
28
+ scraper = EnterRockstar::Scraper::Wikia.new(category_name: category_name)
29
+ scraper.load_saved_json
30
+ scraper.print_indexed_tree
31
+ end
32
+
33
+ desc 'tokenize DATA_DIR', 'take the downloaded lyrics text files and tokenize them'
34
+ def tokenize(data_dir)
35
+ tokenizer = EnterRockstar::Corpus::Tokenizer.new(data_dir: data_dir)
36
+ tokenizer.tokenize
37
+ end
38
+
39
+ desc 'poetic NUMBER', 'generate a poetic representation of a number from the word base'
40
+ def poetic(number)
41
+ generator = EnterRockstar::Generator::Poetic.new
42
+ results = generator.number(123)
43
+
44
+ say results
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnterRockstar
4
+ module Corpus
5
+ # take the downloaded lyrics texts and tokenize them
6
+ class Tokenizer
7
+ def initialize(data_dir: )
8
+ @data_dir = data_dir
9
+ end
10
+
11
+ def tokenize
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnterRockstar
4
+ module Generator
5
+ # lyrics scraper for lyrics.wikia.com
6
+ class Poetic
7
+ def initialize
8
+
9
+ end
10
+
11
+ def number(number)
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+ require 'nokogiri'
5
+ require 'json'
6
+
7
+ module EnterRockstar
8
+ module Scraper
9
+ # lyrics scraper for lyrics.wikia.com
10
+ class Wikia
11
+ START_HOST = 'http://lyrics.wikia.com'
12
+ DATA_DIR = 'lyrics'
13
+ SLEEP_BETWEEN_REQUESTS = 0.1
14
+
15
+ attr_reader :tree
16
+
17
+ def initialize(category_name: 'heavy_metal', url: '/wiki/Category:Genre/Heavy_Metal', data_dir: 'lyrics_data')
18
+ @tree = {}
19
+ @output = "#{data_dir}/wikia_#{category_name}.json"
20
+ @url = url
21
+ @category_name = category_name
22
+ end
23
+
24
+ def parse_category(url: nil, test_limit: false)
25
+ url ||= START_HOST + @url
26
+ html = URI.open(url)
27
+ doc = Nokogiri::HTML(html)
28
+
29
+ # get all category member links and sort them by band and album
30
+ doc.css('li.category-page__member a').each do |category_link|
31
+ next if category_link.attr('title').include?('Category:')
32
+
33
+ band, album = category_link.attr('title').split(':')
34
+ @tree[band] ||= {}
35
+
36
+ if album.nil?
37
+ @tree[band]['band_url'] = category_link.attr('href')
38
+ else
39
+ @tree[band][album] = category_link.attr('href')
40
+ end
41
+ end
42
+
43
+ return if test_limit # test only first page scraping so it's easier
44
+
45
+ print '.'
46
+ # get next page if one exists and parse that
47
+ next_url = doc.css('a.category-page__pagination-next')&.first&.attr('href')
48
+ parse_category(url: next_url) unless next_url.nil?
49
+ end
50
+
51
+ def save_category
52
+ puts
53
+ out = File.new(@output, 'w')
54
+ out.write @tree.to_json
55
+ out.close
56
+ puts "Saved JSON data to #{@output}"
57
+ end
58
+
59
+ def load_saved_json
60
+ file = File.read(@output)
61
+ @tree = JSON.parse(file)
62
+ @new_tree = JSON.parse(file)
63
+ end
64
+
65
+ def print_indexed_tree
66
+ @tree.each_with_index do |(key, _val), index|
67
+ puts "#{index}: #{key}"
68
+ end
69
+ end
70
+
71
+ def parse_all_pages(start_index: 0)
72
+ @tree.each_with_index do |(key, val), index|
73
+ next if index < start_index
74
+
75
+ puts "#{index}: #{key}"
76
+
77
+ val.each do |k, v|
78
+ dirname = k == 'band_url' ? [DATA_DIR, @category_name, key].join('/') : [DATA_DIR, @category_name, key, k].join('/')
79
+ FileUtils.mkdir_p dirname
80
+
81
+ parse_page(v, dirname, key)
82
+ end
83
+ end
84
+
85
+ @tree = @new_tree
86
+ save_category
87
+ end
88
+
89
+ def parse_page(url, dirname, band)
90
+ puts url
91
+ sleep SLEEP_BETWEEN_REQUESTS
92
+ html = URI.open(START_HOST + url)
93
+ doc = Nokogiri::HTML(html)
94
+
95
+ if doc.css('h2 span.mw-headline a').count.zero?
96
+ # single album page listed on the category
97
+ doc.css('div.mw-content-text ol li a').each do |song|
98
+ parse_song(song.attr('href'), dirname, song.text) if song&.attr('href')
99
+ end
100
+ puts
101
+ else
102
+ doc.css('h2 span.mw-headline a').each do |album|
103
+ puts album.text
104
+ # some band pages have extra albums that are not listed in the category page for some reason
105
+ album_dirname = [dirname, album.text].join('/')
106
+ FileUtils.mkdir_p album_dirname
107
+ @new_tree[band][album.text] = album.attr('href')
108
+
109
+ # get song pages
110
+ album.parent.parent.css('+ div + ol > li a').each do |song|
111
+ parse_song(song.attr('href'), album_dirname, song.text) if song&.attr('href')
112
+ end
113
+ puts
114
+ end
115
+ end
116
+ end
117
+
118
+ def parse_song(url, dirname, songname)
119
+ return if url.start_with? 'http'
120
+
121
+ songfile = "#{dirname}/#{songname}.txt"
122
+ without_last = songfile.split('/')
123
+ without_last.pop
124
+ FileUtils.mkdir_p without_last.join('/')
125
+ return if File.exist?(songfile)
126
+
127
+ print '.'
128
+ sleep SLEEP_BETWEEN_REQUESTS
129
+ html = URI.open(START_HOST + url)
130
+ doc = Nokogiri::HTML(html)
131
+
132
+ lyrics = doc.css('div.lyricbox').first
133
+ return if lyrics.nil?
134
+
135
+ if lyrics.css('a')&.first&.attr('href') == '/wiki/Category:Instrumental'
136
+ # instrumental song, whatever
137
+ else
138
+ proper_text = lyrics.inner_html.gsub(%r{<div.*?(\/div>)}, '').split('<br>').join("\n")
139
+
140
+ out = File.new(songfile, 'w')
141
+ out.write proper_text
142
+ out.close
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnterRockstar
4
+ VERSION = '0.1'
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'enter_rockstar/scraper/wikia'
4
+ require 'pry'
5
+
6
+ module EnterRockstar
7
+
8
+ end