-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawler.rb~
164 lines (134 loc) · 4.45 KB
/
crawler.rb~
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
require "rubygems"
require "nokogiri"
require "open-uri"
count = 0
def processLowestLevelPage(url)
page = Nokogiri::HTML(open(url))
leftStack = page.css("div[id=left-stack]")
topicEntry = leftStack.css("li[class=genre]")
language = ""
topic = ""
podcastId = ""
podcastName = ""
artist = ""
link = ""
discription = ""
result = ""
releaseDate = ""
unless topicEntry.nil?
topic = '"'+topicEntry.css("a")[0].text.gsub(/[\r,\n,\"]/,'').strip+'"'
end
languageEntry = leftStack.css("li[class=language]")
unless languageEntry.nil?
language = languageEntry[0].text
end
if language == "语言: 中文"
podcastInfo = page.css("div").select{|podcastId| podcastId['podcast-id']}[0]
unless podcastInfo['podcast-id'].nil?
podcastId = podcastInfo['podcast-id']
end
unless podcastInfo['podcast-name'].nil?
podcastName = '"'+podcastInfo['podcast-name'].gsub(/[\r,\n,\"]/,'').strip+'"'
end
print podcastId+","
print podcastName+","
print topic+","
print language+","
#link should compare with last time
#if same do not update
#if not update and also flag
discriptionEntry = page.css("div[metrics-loc=Titledbox_描述]").css("p")
unless discriptionEntry.nil?
discription = '"'+discriptionEntry.text.gsub(/[\r,\n,\"]/,'').strip+'"'
#puts discription
end
contentBox = page.css("div[class=tracklist-content-box]")
lastestEntryArray = contentBox.css("tbody").css("tr")
unless lastestEntryArray.nil?
lastestEntryArray = lastestEntryArray[0..[lastestEntryArray.size,15].min]
lastestEntry = lastestEntryArray[0]
unless lastestEntry.nil?
unless lastestEntry['preview-artist'].nil?
artist = '"'+lastestEntry['preview-artist'].gsub(/[\r,\n,\"]/,'').strip+'"'
end
unless lastestEntry['audio-preview-url'].nil?
link = lastestEntry['audio-preview-url']
end
end
print artist+","
print link
puts
end
result = result+podcastId+","+podcastName+","+artist+","+link
#to figure out the release cycle
lastestEntryArray.each do |row|
releaseDateEntry = row.css("td[class=release-date]")
unless releaseDateEntry.nil?
releaseDate =releaseDate + releaseDateEntry.text.gsub(/[\r,\n,\"]/,'').strip+','
end
end
releaseDate = '"'+releaseDate + '"'
result+","+releaseDate +","+discription+","+url
else
end
end
##########
def processThirdLevel( topic,thirdLevelUrl)
thirdLevelPage = Nokogiri::HTML(open(thirdLevelUrl))
File.open('result.txt', 'a') do |f2|
firstColumn = thirdLevelPage.css("div[class='column first']").css("li")
firstColumn.each do |singlePage|
#puts singlePage.text
url = singlePage.css("a")[0]["href"]
result = processLowestLevelPage(url)
f2.puts result+","+topic
end
f2.close
end
File.open('result.txt', 'a') do |f2|
secondColumn = thirdLevelPage.css("div[class='column']").css("li")
secondColumn.each do |singlePage|
#puts singlePage.text
url = singlePage.css("a")[0]["href"]
result = processLowestLevelPage(url)
f2.puts result+","+topic
end
f2.close
end
File.open('result.txt', 'a') do |f2|
lastColumn = thirdLevelPage.css("div[class='column last']").css("li")
lastColumn.each do |singlePage|
#puts singlePage.text
url = singlePage.css("a")[0]["href"]
result = processLowestLevelPage(url)
f2.puts result+","+topic
end
f2.close
end
end
rootUrl = "https://itunes.apple.com/cn/genre/podcast/id26?mt=2"
page = Nokogiri::HTML(open(rootUrl))
topLevelTopics = page.css("a[class=top-level-genre]")
topLevelTopics.each do |topLevelTopic|#each
### puts topLevelTopic.text
# puts topLevelTopic["href"]
##TO-DO
#need to more encapsulated for each part we need to print out OK when it has finished
nextPage = Nokogiri::HTML(open(topLevelTopic["href"]))
children = nextPage.css("ul[class='list top-level-subgenres']")
thirdLevelUrl =""
if children.size>0
children.css("a").each do |lowerTopic|
topic = '"'+topLevelTopic.text.gsub(/[\r,\n,\"]/,'').strip+'"'+","+'"'+lowerTopic.text.gsub(/[\r,\n,\"]/,'').strip+'"'
#puts lowerTopic["href"]
thirdLevelUrl = lowerTopic["href"]
processThirdLevel(topic,thirdLevelUrl)
end
else
lowerLevel = nextPage.css("a[class='selected top-level-genre']")
topic = '"'+lowerLevel.text.gsub(/[\r,\n,\"]/,'').strip+'"'
#puts lowerLevel[0]["href"]
thirdLevelUrl = lowerLevel[0]["href"]
processThirdLevel(topic,thirdLevelUrl)
end
end