-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJamendoParser.java
More file actions
259 lines (236 loc) · 8.36 KB
/
JamendoParser.java
File metadata and controls
259 lines (236 loc) · 8.36 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
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* Class for parsing Raw HTML Data from Jamendo.com
*
* @author Akshay
*
*/
public class JamendoParser {
String filePath = "C:\\My files\\Information Retrieval\\Music Sites Raw Data\\JamendoData";
// String filePath = "C:\\My files\\Information Retrieval\\Music Sites Raw Data\\TempTest";
/* ArrayList containing song ID's */
ArrayList<String> songIDList = new ArrayList<>();
/* ArrayList containing song titles */
ArrayList<String> songTitlesList = new ArrayList<>();
/* ArrayList containing song artists */
ArrayList<String> songArtistsList = new ArrayList<>();
/* ArrayList containing song album */
ArrayList<String> songAlbumList = new ArrayList<>();
/* ArrayList containing song album art */
ArrayList<String> songAlbumArtList = new ArrayList<>();
/* ArrayList containing song URL */
ArrayList<String> songUrlList = new ArrayList<>();
/* ArrayList containing song genre */
ArrayList<String> songGenreList = new ArrayList<>();
/* ArrayList containing song released year */
ArrayList<String> songYearList = new ArrayList<>();
/* ArrayList containing meta tag title */
ArrayList<String> metaPageTitlesList = new ArrayList<>();
/* ArrayList containing meta tag description */
ArrayList<String> metaDescriptionList = new ArrayList<>();
static HashMap<String, SongData> index = new HashMap<>();
ArrayList<String> allGenreList = new ArrayList<>();
static int countID = 0;
/**
* Method for reading Raw HTML file
*/
public void readHtmlData() {
File folder = new File(filePath);
if (folder.exists() && folder.isDirectory()) {
File[] files = folder.listFiles();
Arrays.sort(files);
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName());
parseSingleFile(file);
}
}
} else {
System.out.println("Folder Path cannot be found");
}
}
/**
* Method for parsing single HTML file and extracting text Data
*
* @param file
* :HTML file containing raw data
*/
private void parseSingleFile(File file) {
Elements nestedImgTag, titleSpan, genreULTag, nestedGenreSpans, dataList, dataDef;
String songURL = "", songGenre = "", songID = "", songArtist, songTitle = "", songAlbum = "";
String songYear = "", songAlbumArt = "", metaPageTitle = "", metaDescription = "";
String genreSpansAttr = "", dataDefAttr = "", dateText = "", metaAttribute = "";
String[] songIdSplitter,dateSplitter;
boolean metaTitleFlag = false, metaDescriptionFlag = false, yearFlag = false,urlFlagger=false;
Document htmlFile = null;
try {
htmlFile = Jsoup.parse(file, "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
// String htmlTagLang = htmlFile.getElementsByTag("html").attr("lang");
// if (htmlTagLang.contains("en")) {
metaPageTitle = htmlFile.getElementsByTag("title").text();
Elements metaTags = htmlFile.getElementsByTag("meta");
metaTitleFlag = true;
for (Element meta : metaTags) {
metaAttribute = meta.attr("name");
if (metaAttribute.contains("description")) {
metaDescription = meta.attr("content").toString();
metaDescriptionFlag = true;
}
if (!metaTitleFlag)
metaPageTitle = "*^*";
if (!metaDescriptionFlag)
metaDescription = "*^*";
metaAttribute = meta.attr("property");
if (metaAttribute.contains("og:url")) {
songURL = meta.attr("content").toString();
songUrlList.add(songURL);
urlFlagger=true;
}
}
Elements div = htmlFile.select("div.hero_content_card");
if (div.size()!=0) {
nestedImgTag = div.select("img.hero_content_card_image");
songAlbumArt = nestedImgTag.attr("srcset").toString();
songAlbumArtList.add(songAlbumArt);
titleSpan = div.select("span.hero_content_card_name");
songTitle = titleSpan.text();
songTitlesList.add(songTitle);
songArtist = div.select("a.hero_content_card_author").text();
if(!songArtist.contains(""))
songArtistsList.add(songArtist);
else{
songArtist="*^*";
songArtistsList.add(songArtist);
}
songIdSplitter = songURL.split("/");
songID = songIdSplitter[4];
songIDList.add(songID);
genreULTag = div.select("ul.hero_content_card_tags");
nestedGenreSpans = genreULTag.select("span");
genreSpansAttr = nestedGenreSpans.attr("itemprop");
if (genreSpansAttr.contains("genre")){
for (Element span : nestedGenreSpans) {
allGenreList.add(span.text());
}
songGenreList.add(allGenreList.get(0));
}else{
songGenre="*^*";
songGenreList.add(songGenre);
}
Elements innerDiv = htmlFile.select("div.row");
if (innerDiv.size()!=0) {
dataList = innerDiv.select("dl.entity-description-list");
dataDef = dataList.select("dd");
for (Element dd : dataDef) {
dataDefAttr = dd.attr("itemprop");
if (dataDefAttr.contains("datePublished")) {
dateText = dd.text();
dateSplitter = dateText.split("\\.");
songYear = dateSplitter[2];
yearFlag = true;
break;
}
}
if (!yearFlag)
songYear = "*^*";
songYearList.add(songYear);
} else {
songYear = "*^*";
songYearList.add(songYear);
}
songAlbum = "*^*";
songAlbumList.add(songAlbum);
putData(songIDList, songTitlesList, songArtistsList,
songAlbumList, songAlbumArtList, songYearList, songGenreList,
songUrlList, metaPageTitle, metaDescription);
}
// }
}
/**
* Method for putting the data in the index map and SongData wrapper
*
* @param songIDList2
* :ArrayList holding all song unique ID's
* @param songTitlesList2
* :ArrayList holding all song titles
* @param songArtistsList2
* :ArrayList holding all song artists
* @param songAlbumList2
* :ArrayList holding all song URL's
* @param songAlbumArtList2
* :ArrayList holding all song album arts
* @param songYearList2
* :ArrayList holding all song years
* @param songGenreList2
* :ArrayList holding all song genre
* @param songUrlList2
* :ArrayList holding all song URL's
* @param metaDescription
* @param metaPageTitle
*/
private void putData(ArrayList<String> songIDList2,
ArrayList<String> songTitlesList2,
ArrayList<String> songArtistsList2,
ArrayList<String> songAlbumList2,
ArrayList<String> songAlbumArtList2,
ArrayList<String> songYearList2, ArrayList<String> songGenreList2,
ArrayList<String> songUrlList2, String metaPageTitle,
String metaDescription) {
for (int i = 0; i < songIDList2.size(); i++) {
index.put(songIDList2.get(i), new SongData(songTitlesList2.get(i),
songUrlList2.get(i), songAlbumList2.get(i),
songArtistsList2.get(i), songYearList2.get(i),
songGenreList2.get(i), songAlbumArtList2.get(i),
metaPageTitle, metaDescription));
}
}
/**
* Driver function
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
JamendoParser sParser = new JamendoParser();
sParser.readHtmlData();
sParser.writeToFile();
System.out.println("Written to file");
}
/**
* Method to write data from map to the file in text format
*
* @throws IOException
*/
private void writeToFile() throws IOException {
DataOutputStream out = null;
out = new DataOutputStream(
new FileOutputStream(
"C:\\My files\\Information Retrieval\\ParsedTextData\\jamendoTextData.txt"));
SongData current;
for (Map.Entry<String, SongData> entry : index.entrySet()) {
out.writeBytes(entry.getKey());
out.writeBytes(";");
current = entry.getValue();
out.writeBytes(current.getTitle() + ";" + current.getArtist() + ";"
+ current.getAlbum() + ";" + current.getAlbumArt() + ";"
+ current.getYear() + ";" + current.getGenre() + ";"
+ current.getUrl().get(0) + ";" + current.getmetaTitle()
+ ";" + current.getmetaDescription()+ "\n");
}
out.close();
}
}