-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch
More file actions
152 lines (133 loc) · 3.45 KB
/
fetch
File metadata and controls
152 lines (133 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env ruby
require 'httparty'
require 'nokogiri'
require 'pry'
require 'pry-nav'
@year, @day, @override = ARGV
raise 'put in a number dumbass' unless @year && @day
@cookie = File.read('cookie')
def build_text
<<~RUBY.freeze
require 'pry'
require 'pry-nav'
require 'ostruct'
require 'set'
#{fetch_body}
# part2
@data = DATA.each_line.map(&:chomp).map(&:freeze).freeze
@ex1 = ex1.each_line.map(&:chomp).map(&:freeze).freeze
@ex1.each do |line|
_, *args = line.match(//).to_a
_ = *args
end
__END__
#{fetch_input}
RUBY
end
def fetch_input
base_url = "https://adventofcode.com/#{@year}/day/#{@day}/input"
res = HTTParty.get(base_url, {
headers: {
cookie: "session=#{@cookie}"
}
})
end
def fetch_page
base_url = "https://adventofcode.com/#{@year}/day/#{@day}"
content = HTTParty.get(base_url, {
headers: {
cookie: "session=#{@cookie}"
}
})
if content.body =~ /Please don't repeatedly/
puts 'Still Waiting...'
exit
end
@content ||= content.body
end
def fetch_title
title = Nokogiri::HTML(@content).css('article h2').first.text
filename = title.split(':').last[0..-4]
filename.strip.split(' ').map(&:downcase).unshift("#{@year}/#{@day}").join('_') + '.rb'
end
def fetch_body
body = Nokogiri::HTML(@content).css('.day-desc').first.children.map { |n| [n.name, n.text] }
process_section(body)
end
def process_section(section)
# section is Array[name, text]
precount = 0
res = section.map do |type, text|
if type == 'p'
[
'# ',
text.split(/\s/).each_with_object([[]]) do |t, a|
if a[-1].join(' ').length > 80
a << [t]
else
a.last << t
end
end.map { |l| l.join(' ') }.join("\n# ")
]
elsif type == 'text'
nil
elsif type == 'h2'
['# ', text]
elsif type == 'pre'
precount += 1
["ex#{precount} = <<~T\n", text.split("\n").map { |l| " #{l}" }.join("\n"), "\nT"]
elsif type == 'ul'
[text.split("\n").tap(&:shift).map { |l| "# - #{l}" }.join("\n")]
end
end
res.compact.map(&:join).join("\n#\n")
end
def write_file
File.open(fetch_title, 'w+') do |file|
file.write(build_text)
rescue StandardError => e
file.write('something went wrong')
file.write(e)
ensure
file.close
end
end
def insert_part_2
body = Nokogiri::HTML(@content).css('.day-desc').last.children.map { |n| [n.name, n.text] }
text = process_section(body)
existing = File.read(fetch_title)
newtext = existing.gsub('# part2', text)
File.open(fetch_title, 'wb') do |file|
file.write(newtext)
rescue StandardError => e
puts "FUCK, #{e}"
ensure
file.close
end
end
def create_file
fetch_page
body = Nokogiri::HTML(@content).css('.day-desc')
if body.count == 1
if File.exist?(fetch_title) && @override == '-f'
puts 'file already exists, youre still on part 1'
exit
else
write_file
puts 'OK GO!'
`code #{fetch_title}`
end
else
if File.exist?(fetch_title)
puts 'inserting part 2 now...'
insert_part_2
`code #{fetch_title}`
else
puts 'youve already completed this one, but sure...'
write_file
insert_part_2
`code #{fetch_title}`
end
end
end
create_file