-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSongParser.java
More file actions
227 lines (199 loc) · 6.33 KB
/
SongParser.java
File metadata and controls
227 lines (199 loc) · 6.33 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
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class SongParser {
HashMap<String, String> songListMap = new HashMap<>();
static HashMap<String, SongData> index = new HashMap<String, SongData>();
String filePath = "C:\\My files\\Information Retrieval\\Music Sites Raw Data\\RawDataGaana";
// String filePath = "C:\\My files\\Information Retrieval\\Music Sites Raw Data\\TempTest";
public void putData(JSONObject obj, String url, String pageTitle,
String meta) throws JSONException {
String songId = obj.getString("id");
String title = obj.getString("title");
String album = obj.getString("albumtitle");
String artist = obj.getString("artist");
String albumart = obj.getString("albumartwork");
artist = artist.substring(0, artist.indexOf("#"));
if (album.isEmpty())
album = "*^*";
if (title.isEmpty())
title = "*^*";
if (artist.isEmpty())
artist = "*^*";
if (albumart.isEmpty())
albumart = "*^*";
// System.out.println(obj);
if (!index.containsKey(songId)) {
index.put(songId, new SongData(title, url, album, artist, "*^*",
"*^*", albumart, pageTitle, meta));
}
}
public void parseData() throws FileNotFoundException, JSONException {
// int count =0;
File folder = new File(filePath);
if (folder.exists() && folder.isDirectory()) {
File[] files = folder.listFiles();
for (File file : files) {
// if(count>10)break;
if (file.isFile()) {
parseSingleFile(file);
// count++;
}
}
} else {
System.out.println("Folder Path cannot be found");
}
}
public void parseSingleFile(File file) throws FileNotFoundException {
Scanner wordScanner = null;
boolean isUrl = true;
String url = null;
String pageTitle = null, meta = null;
// System.out.println(file.getName());
wordScanner = new Scanner(file);
if (!wordScanner.hasNextLine())
return;
url = wordScanner.nextLine();
wordScanner.close();
Document htmlFile = null;
try {
htmlFile = Jsoup.parse(file, "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
pageTitle = htmlFile.select("title").text();
meta = htmlFile.select("meta[name=description]").attr("content");
// System.out.println(pageTitle+"\t"+meta);
if (pageTitle == "")
pageTitle = "*^*";
if (meta == "")
meta = "*^*";
pageTitle = pageTitle.replaceAll("\\n", "").replaceAll("\\r","");
meta=meta.replaceAll("\\\n", "");
Elements mainDiv = htmlFile.select("div.track");
if (mainDiv.size() > 0) {
for (Element currentDiv : mainDiv) {
String token = currentDiv.select("span").text();
if (token.contains("{") && token.contains("}")
&& token.contains("object_type")) {
// System.out.println(token);
token = token.trim();
token = token.replaceAll(""", "");
JSONObject obj = null;
try {
obj = new JSONObject(token);
} catch (JSONException e1) {
System.out.println(token);
e1.printStackTrace();
}
try {
if (obj.has("object_type")) {
Object current = obj.get("object_type");
if (!current.getClass().equals(Integer.class))
return;
if (obj.getInt("object_type") == 10)
putData(obj, url, pageTitle, meta);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// wordScanner = new Scanner(file);
//
// while (wordScanner.hasNext()) {
// token = wordScanner.nextLine();
// if(isUrl){
// url = token;
// isUrl = false;
// Document finaldoc = null;
// try {
// finaldoc = Jsoup.connect(url).get();
// } catch (IOException e) {
// System.out.println("BadURL");
// continue;
// }
// pageTitle = finaldoc.select("title").text();
// meta = finaldoc.select("meta[name=description]").attr("content");
// System.out.println(pageTitle+"\t"+meta);
// if(pageTitle=="")pageTitle="*^*";
// if(meta=="")meta="*^*";
// }
//
// if(token.contains("{") && token.contains("}") &&
// token.contains("object_type")){
// //System.out.println(token);
// token = token.trim();
// token = token.replaceAll(""", "");
// JSONObject obj = null;
// try {
// obj = new JSONObject(token);
// } catch (JSONException e1) {
// System.out.println(token);
// e1.printStackTrace();
// }
//
// try {
// if(obj.has("object_type") ){
// Object current = obj.get("object_type");
// if(!current.getClass().equals(Integer.class))
// return;
// if(obj.getInt("object_type")==10)
// putData(obj,url,pageTitle,meta);
// }
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
//
// }
//
//
//
// wordScanner.close();
}
public void writeToFile() throws IOException {
DataOutputStream out = null;
out = new DataOutputStream(
new FileOutputStream(
"C:\\My files\\Information Retrieval\\ParsedTextData\\sampleGaana.txt"));
SongData current;
for (Map.Entry<String, SongData> entry : index.entrySet()) {
// out.writeBytes(entry.getKey());
// out.writeBytes("\t");
current = entry.getValue();
out.writeBytes(entry.getKey() + ";" + current.getTitle() + ";"
+ current.getArtist() + ";" + current.getAlbum() + ";"
+ current.getAlbumArt() + ";" + current.getYear() + ";"
+ current.getGenre() + ";" + current.getUrl() + ";"
+ current.getmetaTitle() + ";"
+ current.getmetaDescription() + "\n");
}
out.close();
}
public static void main(String[] args) throws JSONException, IOException {
SongParser s1 = new SongParser();
System.out.println("Parsing Data");
s1.parseData();
System.out.println("Writing to file");
s1.writeToFile();
// for(Map.Entry<String, SongData> entry : index.entrySet()){
// System.out.println(entry.getValue().getTitle()+"\t"+entry.getValue().getArtist()+"\t"+entry.getValue().getAlbum());
// }
}
}