-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMySpaceParser.java
More file actions
194 lines (169 loc) · 6.86 KB
/
MySpaceParser.java
File metadata and controls
194 lines (169 loc) · 6.86 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Bhoomika
*/
import java.io.*;
import java.util.*;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
*
* @author Akshay
*
*/
public class MySpaceParser {
String filePath = "D:\\myspacefiles\\";
static DataOutputStream out = null;
static HashMap<String, SongData> index = new HashMap<String, SongData>();
/**
*
*/
public void parseHtmlData() throws Exception {
File folder = new File(filePath);
// parseSingleFile(folder);
//parseSingleFile(folder);
if (folder.exists() && folder.isDirectory()) {
File[] files = folder.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName());
parseSingleFile(file);
}
}
} else {
System.out.println("Folder Path cannot be found");
}
}
private void parseSingleFile(File file) throws Exception {
Document htmlFile = null;
try {
htmlFile = Jsoup.parse(file, "ISO-8859-1");
} catch (Exception e) {
e.printStackTrace();
}
// Elements parents =htmlFile.getElementsByClass("cover");
Elements parents = htmlFile.getElementsByTag("section");
String title = "*^*";
String artist = "*^*";
String url = "*^*";
String imageurl = "*^*";
String pageTitle = "*^*";
String description = "*^*";
String songid = "*^*";
String genre = "*^*";
String album = "*^*";
String year = "*^*";
boolean isVideo = false;
Elements titles = htmlFile.getElementsByTag("title");
Elements metas = htmlFile.getElementsByTag("meta");
for (Element meta : metas) {
String name = meta.attr("name");
String prop = meta.attr("property");
if (prop.equals("og:video")) {
System.out.println();
url = meta.attr("content");
String arr[] = url.split("/");
songid = arr[arr.length-1];
title = arr[arr.length-2];
artist = arr[arr.length-4];
isVideo = true;
}
if (name.equals("description")) {
// System.out.println();
description = meta.attr("content");
}
}
for (Element Pagetitle : titles) {
pageTitle = Pagetitle.html();
// System.out.println(pageTitle);
break;
}
if(isVideo){
SongData s = new SongData(title, url, album, artist, year, genre, imageurl);
s.setPagetitle(pageTitle);
s.setDescrption(description);
index.put(songid, s);
return;
}
if (parents.isEmpty() && !isVideo) {
return;
} else {
// boolean isVideo = false;
titles = htmlFile.getElementsByTag("title");
metas = htmlFile.getElementsByTag("meta");
for (Element meta : metas) {
String name = meta.attr("name");
String prop = meta.attr("property");
if (prop.equals("og:video")) {
System.out.println();
url = meta.attr("content");
String arr[] = url.split("/");
songid = arr[arr.length-1];
isVideo = true;
}
if (name.equals("description")) {
// System.out.println();
description = meta.attr("content");
}
}
for (Element Pagetitle : titles) {
pageTitle = Pagetitle.html();
// System.out.println(pageTitle);
break;
}
for (Element e : parents) {
if (e.attr("id").equals("song")) {
Elements e1 = e.children();
for (Element e2 : e1) {
if (e2.attr("id").equals("actions")) {
Elements e3 = e2.children();
int count = 0;
for (Element e4 : e3) {
if (count == 1) {
songid = e4.attr("data-song-id");
album = e4.attr("data-album-title");
title = e4.attr("data-title");
artist = e4.attr("data-artist-name");
url = "www.myspace.com" + e4.attr("data-url");
genre = e4.attr("data-genre-name");
imageurl = e4.attr("data-image-url");
SongData s = new SongData(title, url, album, artist, year, genre, imageurl);
s.setPagetitle(pageTitle);
s.setDescrption(description);
index.put(songid, s);
}
count++;
}
// System.out.println();
}
}
// System.out.println(e.attr("id"));
}
}
//System.out.println();
}
}
public void writeToFile() throws IOException {
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.getPagetitle() + ";" + current.getDescrption() + "\n");
}
}
public static void main(String[] args) throws Exception {
out = new DataOutputStream(new FileOutputStream("D:\\myspace.txt"));
MySpaceParser sParser = new MySpaceParser();
sParser.parseHtmlData();
sParser.writeToFile();
}
}