archare 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "archare"
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,20 @@
1
+ require "archare/version"
2
+ require "archare/introducer"
3
+ require "archare/crawler"
4
+
5
+
6
+
7
+ module Archare
8
+ def self.introduce
9
+
10
+ introducer = Introducer.new
11
+ introducer.introduce
12
+ end
13
+
14
+
15
+ def self.crawler
16
+
17
+ Crawler.instance
18
+ end
19
+
20
+ end
@@ -0,0 +1,129 @@
1
+ require 'nokogiri'
2
+ require 'curb'
3
+
4
+ require 'json'
5
+ require 'singleton'
6
+
7
+ class Archare::Crawler
8
+
9
+ include Singleton
10
+
11
+ @@LEETCODE_URI_BASE = "https://leetcode.com/"
12
+
13
+ @@LEETCODE_URI_PROBLEM = @@LEETCODE_URI_BASE + "problemset/algorithms/"
14
+
15
+ @@LEETCODE_URI_TAG = @@LEETCODE_URI_BASE + "tag/"
16
+
17
+
18
+
19
+ def lc_problems(update = false, uri = @@LEETCODE_URI_PROBLEM)
20
+
21
+ problems = []
22
+
23
+
24
+ unless update
25
+ tags_hash = read_json_file "lc_problems"
26
+ problems = tags_hash["problems"]
27
+ else
28
+ body = get_dom_from_uri(uri)
29
+ problem_list = body.css('table')
30
+
31
+ links = problem_list.css('a').each do |link|
32
+ href_str = link['href'].to_s
33
+ if href_str.include? '/problems/'
34
+ problems << link.text
35
+ # problems << href_str.split('problems/')[-1][0..-2].gsub('-', ' ')
36
+ end
37
+ end
38
+
39
+ write_json_file "lc_problems", {"problems" => problems}
40
+ end
41
+
42
+ return problems
43
+ end
44
+
45
+
46
+ def lc_tags(update = false, uri = @@LEETCODE_URI_PROBLEM)
47
+
48
+ tags = []
49
+ tags_hash = {}
50
+ unless update
51
+ tags_hash = read_json_file "lc_tags"
52
+ tags = tags_hash['tags']
53
+ else
54
+ body = get_dom_from_uri(uri)
55
+ links = body.css('a').each do |link|
56
+ href_str = link['href'].to_s
57
+ if href_str.include? '/tag/'
58
+ tags << href_str.split('tag/')[-1][0..-2]
59
+ end
60
+ end
61
+
62
+ tags_hash['tags'] = tags
63
+ write_json_file "lc_tags", tags_hash
64
+ end
65
+
66
+ return tags
67
+ end
68
+
69
+ def lc_tags_problems_map(update = false)
70
+
71
+
72
+ map = Hash.new
73
+
74
+ unless update
75
+ map = read_json_file "lc_tags_problems_map"
76
+ else
77
+ tags = lc_tags(true)
78
+
79
+ puts "updating tags-problems map. It will finish in about 1 minute \n\n"
80
+ tags.each_with_index do |tag, i|
81
+
82
+ map[tag] = lc_problems(true, lc_tag_uri_of(tag))
83
+ print "\033[1A \r #{i + 1} of #{tags.length} tags: #{tag} \n"
84
+ $stdout.flush
85
+ end
86
+
87
+ write_json_file "lc_tags_problems_map", map
88
+ end
89
+
90
+ puts tags, map
91
+
92
+ return map
93
+ end
94
+
95
+
96
+ def update_lc_data
97
+ lc_tags_problems_map true
98
+ lc_problems true
99
+ end
100
+
101
+ private
102
+
103
+ def get_dom_from_uri(uri)
104
+ page = Curl.get(uri)
105
+ body = Nokogiri::HTML(page.body_str).xpath('//body')
106
+ end
107
+
108
+ def lc_tag_uri_of(tag)
109
+ @@LEETCODE_URI_TAG + tag + '/'
110
+ end
111
+
112
+ def file_directory
113
+ File.join(File.dirname(File.expand_path(__FILE__)), '/data/')
114
+ end
115
+
116
+ def write_json_file(file_name, map)
117
+ File.open(file_directory + file_name + ".json", 'w') do |f|
118
+ f.write(map.to_json)
119
+ end
120
+ end
121
+
122
+ def read_json_file(file_name)
123
+ map = {}
124
+ File.open(file_directory + file_name + ".json", 'r') do |f|
125
+ map = JSON.parse(f.read)
126
+ end
127
+ return map
128
+ end
129
+ end
@@ -0,0 +1 @@
1
+ {"problems":["Intersection of Two Arrays II","Intersection of Two Arrays","Wiggle Sort II","Best Meeting Point","Wiggle Sort","H-Index","Meeting Rooms II","Meeting Rooms","Valid Anagram","Largest Number","Maximum Gap","Sort List","Insertion Sort List","Sort Colors","Insert Interval","Merge Intervals"]}
@@ -0,0 +1 @@
1
+ {"tags":["array","hash-table","linked-list","math","two-pointers","string","divide-and-conquer","binary-search","dynamic-programming","backtracking","stack","heap","greedy","sort","bit-manipulation","tree","depth-first-search","breadth-first-search","union-find","graph","design","topological-sort","trie","binary-indexed-tree","segment-tree","binary-search-tree","recursion","brainteaser","memoization","queue"]}
@@ -0,0 +1,26 @@
1
+
2
+ class Archare::Introducer
3
+ def introduce
4
+ puts """
5
+
6
+ Archare crawler
7
+
8
+ Usage:
9
+ crawler = Archare.crawler
10
+
11
+ leetcode tags:
12
+ crawler.lc_tags
13
+
14
+ leetcode problems:
15
+ crawler.lc_problems
16
+
17
+ leetcode tags-problems map:
18
+ crawler.lc_tags_problems_map
19
+
20
+ Update data from leetcode:
21
+ crawler.update_lc_data (updating time is about 1 minute)
22
+
23
+ """
24
+
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Archare
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: archare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xhanshawn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-30 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: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
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.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: curb
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nokogiri
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: curb
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: A gem for archare.
112
+ email:
113
+ - xhan610@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - Guardfile
123
+ - README.md
124
+ - Rakefile
125
+ - archare.gemspec
126
+ - archare.sublime-project
127
+ - archare.sublime-workspace
128
+ - bin/console
129
+ - bin/setup
130
+ - lib/archare.rb
131
+ - lib/archare/crawler.rb
132
+ - lib/archare/data/lc_problems.json
133
+ - lib/archare/data/lc_tags.json
134
+ - lib/archare/data/lc_tags_problems_map.json
135
+ - lib/archare/introducer.rb
136
+ - lib/archare/version.rb
137
+ homepage: http://archare.elasticbeanstalk.com
138
+ licenses:
139
+ - MIT
140
+ metadata:
141
+ allowed_push_host: https://rubygems.org
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.6.4
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Archare gem
162
+ test_files: []