-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwwdc.rb
More file actions
303 lines (178 loc) · 5.34 KB
/
wwdc.rb
File metadata and controls
303 lines (178 loc) · 5.34 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require 'nokogiri'
require 'open-uri'
require 'json'
def fetch_topics(url)
doc = Nokogiri::HTML(open(url))
topics = []
doc.css("div.topics-container div.column").each_with_index do |topic_row, index|
topic = {}
topic_name = topic_row.css("a h4").text
topic[:name] = topic_name
topic[:url] = topic_row.css("a").attr("href").value
topic[:type] = "topic"
puts topic
topic[:children] = []
topic_row.css("ul li").each do |child_topic_row|
child_topic = {}
child_topic_name = child_topic_row.css("a").text.gsub!("\t","").gsub!("\n","")
child_topic[:name] = child_topic_name
child_topic[:url] = child_topic_row.css("a").attr("href").value
child_topic[:type] = "topic"
topic[:children] << child_topic
end
topics << topic
end
# File.open("sections.json",'wb') do |file|
# file << JSON.pretty_generate(topics)
# end
topics
end
def fetch_video(url, topic_name)
doc = Nokogiri::HTML(open(url))
videos = []
doc.css("li.collection-item section.column").each_with_index do |video_row, index|
video = {}
video_url = video_row.css("a").attr("href").value
video[:url] = video_url
next unless video_url.include?("2018")
video_title = video_row.css("a").text.gsub!("\t","").gsub!("\n","")
video[:title] = video_title
next unless video_title.size > 0 #trim photo preview row
#platform
video_row.css("ul.video-tags li.focus").each do |platform|
video[:platform] = platform.text
end
video_content = video_row.css("p").text
video[:content] = video_content
video[:type] = "video"
video[:parent_topic] = topic_name
videos << video
end
# File.open("videos.json",'wb') do |file|
# file << JSON.pretty_generate(videos)
# end
videos
end
#
def fetch_wwdc_json
host = "https://developer.apple.com"
topic_url = host + "/videos/topics/"
topics = fetch_topics(topic_url)
topics.each do |topic|
next unless topic[:children].size > 0
sub_topics = topic[:children]
puts "."
sub_topics.each do |child|
child_url = host + child[:url]
videos = fetch_video(child_url, child[:name])
child[:videos] = videos.sort_by{ |k| k[:url]}
end
end
return topics
end
def find_topics_in_wwdc_by_video_url(wwdc, video_url)
txt_topics = []
wwdc.each do |topic|
next unless topic[:children].size > 0
sub_topics = topic[:children]
sub_topics.each do |sub_topic|
sub_topic[:videos].each do |video|
if video[:url] == video_url
txt_topics << video[:parent_topic]
end
end
end
end
txt_topics
end
def find_video_with_url(videos, url)
target = nil
videos.each do |video|
target = video if video[:url] == url
end
return target
end
def generate_md(videos, sorted_by_alphabetical)
md = ""
host = "https://developer.apple.com"
filename = "./example/wwdc.md"
if sorted_by_alphabetical
videos = videos.sort_by{ |k| k[:title]}
filename = "./example/wwdc_sorted_by_alphabetical.md"
end
videos.each do |video|
md << "## " + video[:title] + "\n"
md << "platform : " + video[:platform] + "\n\n"
md << "topics : " + video[:topics].join("/") + "\n\n"
md << video[:content] + "\n\n"
md << "[link](" + host + video[:url] + ")" + "\n\n"
md << "\n"
end
File.open(filename,'wb+') do |file|
file << md
end
end
def generate_topic_video_md(topics, videos)
md = ""
filename = "./example/wwdc_topic_video.md"
host = "https://developer.apple.com"
topics.each do |topic|
md << "# " + topic[:name] + "\n"
topic[:children].each do |sub_topic|
md << "### " + sub_topic[:name] + "\n"
sub_topic[:videos].each do |video|
video_object = find_video_with_url(videos, video[:url])
topics_text = video_object[:topics]
video_url_title = "[" + video[:title] + "](" + host + video[:url] + ")"
if topics_text.size > 0
md << "* " + video_url_title + " **(" + topics_text.join(", ") + ")**" + "\n"
else
md << "* " + video_url_title + "\n"
end
end
end
md << "\n"
end
File.open(filename,'wb+') do |file|
file << md
end
end
def fetch_all_videos
directory_name = "example"
Dir.mkdir(directory_name) unless File.exists?(directory_name)
topics = fetch_wwdc_json
File.open("./example/wwdc.json",'wb+') do |file|
file << JSON.pretty_generate(topics)
end
# generate vidoe list
videos = []
video_string_array = []
topics.each do |topic|
next unless topic[:children].size > 0
sub_topics = topic[:children]
sub_topics.each do |child|
child[:videos].each do |video|
video_url = video[:url]
unless video_string_array.include?(video_url)
videos << video
video_string_array << video_url
end
end
end
end
# sort videos
videos = videos.sort_by{ |k| k[:url]}
# combine topic and video
videos.each do |video|
topics_text = find_topics_in_wwdc_by_video_url(topics, video[:url])
video[:topics] = topics_text
video.delete(:parent_topic)
end
File.open("./example/wwdc_videos.json",'wb+') do |file|
file << JSON.pretty_generate(videos)
end
generate_md(videos, false)
generate_md(videos, true)
generate_topic_video_md(topics, videos)
end
fetch_all_videos