-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparserPlay.java
349 lines (310 loc) · 11.9 KB
/
parserPlay.java
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package Parser;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
/*
* The imports are for log4j
*/
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/*
* The imports are for jsoup
*/
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class parserPlay
{
//static Logger log = Logger.getLogger("hotpadsRent"); // making an object of logger which writes it to files
public static void main(String args[])throws IOException, SQLException, JSONException, InterruptedException // the main metho
{
//PropertyConfigurator.configure("log4j.properties"); // for the properties file which is there in the project
Connection conn=null;
Statement stmt=null;
//File input = new File("3.txt");
try
{
//database connection
//conn=DriverManager.getConnection("jdbc:mysql://localhost/playstore","root","");
conn = DriverManager.getConnection("jdbc:mysql://localhost/playstore","root","");
//conn=DriverManager.getConnection("jdbc:mysql://localhost/appstore?user=root&password=googleplay123");
stmt=conn.createStatement();
String query="SELECT * FROM appraw order by crawltime ";
//String query="SELECT * FROM appraw WHERE `appraw`.`app_page` is not null and `appraw`.`app_url` NOT IN ('/store/apps/details?id=com.bowengames.farmingusa') ORDER BY `app_url` ASC";
ResultSet rs=stmt.executeQuery(query);
//Iterating through records
while(rs.next()){
//variables for parsing
int seed_id=0;
String app_url=null;
String app_id=null;
String appname=null;
Date crawledDate=null;
String app_page=null;
long peopleRated=0;
StringBuilder category=new StringBuilder();
String version=null;
double appsize=0.0;
double numberOfStars=0.0;
String desc =null;
String Developerinfo=null;
Date lastupdated=null;
long[] numofratings=new long[5];
String numofInstalls=null;
long numinstallsupperbound=0;
long numinstallslowerbound=0;
String OSreq=null;
String offeredBy=null;
double cost=0.0;
String inapp_cost=null;
StringBuilder whatsNew=new StringBuilder();
seed_id=rs.getInt("seed_id");
app_url=rs.getString("app_url");
app_page=rs.getString("app_page");
crawledDate=rs.getTimestamp("crawltime");
if(!(app_page==null)){
//Parsing the html page using Jsoup
Document doc = Jsoup.parse(app_page, "UTF-8");
//app_id=app_url.substring(app_url.indexOf('?')+3);
app_id=rs.getString("appid");
//App name
Elements appnameele=doc.select("h1[class=document-title]");
if(appnameele.size()>0){
appname=appnameele.get(0).text();
}
// rating-count
Elements metalinks = doc.select("span[class=rating-count]");
// System.out.println(metalinks);
//cost of the app
Elements costlink=doc.select("button[class=price buy id-track-click]");
if(costlink.size()>0){
int i=0;
Element costlnk=costlink.get(0);
if(costlnk.text().contains("Buy")){
cost=Double.parseDouble(costlnk.text().trim().split(" ")[0].substring(1));
}
i++;
}
//Number of people given rating
String[] temp= metalinks.first().attr("aria-label").trim().split(" ");
peopleRated = Long.parseLong(temp[0].replaceAll("[^0-9]", ""));
//Category of the app
Elements categories=doc.select("a[class=document-subtitle category]");
if(categories.size()>0){
int i=0;
while(i<categories.size())
{
category.append(categories.get(i).text()+" ");
i++;
}
}
// the total number of stars on average
metalinks = doc.select("div[class=tiny-star star-rating-non-editable-container]");
temp=metalinks.first().attr("aria-label").trim().split(" ");
numberOfStars =Double.parseDouble(temp[1]) ;
// description
desc = doc.select("div[class=id-app-orig-desc]").get(0).text();
// Number of persons for each rating
if(doc.select("span[class=bar-number]").size()>0)
{
int i=0;
while(i<doc.select("span[class=bar-number]").size())
{
numofratings[4-i]=Long.parseLong(doc.select("span[class=bar-number]").get(i).text().replaceAll("[^0-9]", ""));
i++;
}
}
// Additional Information
if(doc.select("div[class=meta-info]").size()>0)
{
int i=0;
while(i<doc.select("div[class=meta-info]").size())
{
Element links = doc.select("div[class=meta-info]").get(i);
//System.out.println(links);
String links1 = links.select("div[class = title]").get(0).text();
if(!(links1.equalsIgnoreCase("report") || links1.equalsIgnoreCase("permissions")))
{
String links2 = links.select("div[class = content").get(0).text();
if(links1.equals("Updated")){
String tempdate=links.select("div[class = content").get(0).text().replaceAll("[,]", "");
lastupdated = new SimpleDateFormat("MMM dd yyyy").parse(tempdate);
}
if(links1.equals("Installs")){
numofInstalls= links.select("div[class = content").get(0).text();
numinstallsupperbound=Long.parseLong(numofInstalls.split("-")[1].trim().replaceAll("[,]",""));
numinstallslowerbound=Long.parseLong(numofInstalls.split("-")[0].trim().replaceAll("[,]", ""));
}
if(links1.contains("Requires")){
String temp1=links.select("div[class = content").get(0).text();
if(!temp1.contains("Varies with device")){
OSreq= links.select("div[class = content").get(0).text();
}
else{
OSreq= null;
}
}
if(links1.equals("Offered By")){
offeredBy= links.select("div[class = content").get(0).text();
}
if(links1.equals("In-app Products")){
inapp_cost=links.select("div[class = content").get(0).text();
}
if(links1.equals("Size")){
String temp1=links.select("div[class = content").get(0).text();
if(!temp1.contains("Varies with device")){
String tempsize=links.select("div[class = content").get(0).text();
if(tempsize.contains("k")){
appsize=Double.parseDouble(tempsize.substring(0,tempsize.length()-1).replaceAll("[,]", ""))/1024;
}
else if(tempsize.contains("M")){
appsize=Double.parseDouble(tempsize.substring(0,tempsize.length()-1).replaceAll("[,]", ""));
}
else if(tempsize.contains("G")){
appsize=Double.parseDouble(tempsize.substring(0,tempsize.length()-1).replaceAll("[,]", ""))*1024;
}
}
else{
appsize=0.0;
}
}
if(links1.contains("Version")){
String temp1=links.select("div[class = content").get(0).text();
if(!temp1.contains("Varies with device")){
version=links.select("div[class = content").get(0).text();
}
else{
version=null;
}
}
}
i++;
}
//Developer info ex: developers mail
if(doc.select("a[class=dev-link]").size()>0){
i=0;
while(i<doc.select("a[class=dev-link]").size()){
Element devinfolnk=doc.select("a[class=dev-link]").get(i);
String hrefs=devinfolnk.attr("href").trim();
if(hrefs.contains("mailto")){
Developerinfo=hrefs;
}
i++;
}
}
}
//Collecting Whatsnew information
if(doc.select("div[class=details-section-contents show-more-container]").size()>0)
{
Element metalink = doc.select("div[class=details-section-contents show-more-container]").get(0);
int i=0;
while(i<metalink.select("div[class=recent-change]").size())
{
whatsNew.append(" "+metalink.select("div[class=recent-change]").get(i).text()) ;
i++;
}
}
}
System.out.println("seed_id : "+ seed_id);
System.out.println("app_id : "+app_id);
System.out.println("app_url : "+app_url);
System.out.println("appname : "+appname);
System.out.println("Crawltime : "+crawledDate);
System.out.println("Total number of people rated: "+peopleRated);
System.out.println("Category : "+category.toString());
System.out.println("version: "+version);
System.out.println("Appsize: "+appsize);
System.out.println("Cost: "+cost);
System.out.println("inapp_products: "+inapp_cost);
System.out.println("Average rating: "+numberOfStars);
System.out.println("App description : "+desc);
System.out.println("dev_ email : "+Developerinfo);
System.out.println("Last updated : "+ lastupdated);
System.out.println("rate_5 : "+numofratings[4]);
System.out.println("rate_4 : "+numofratings[3]);
System.out.println("rate_3 : "+numofratings[2]);
System.out.println("rate_2 : "+numofratings[1]);
System.out.println("rate_1 : "+numofratings[0]);
System.out.println("Installs : "+numofInstalls);
System.out.println("OS_requirements : "+OSreq);
System.out.println("Offeredby : "+offeredBy);
System.out.println("Whatsnew : "+whatsNew);
String insertquery="insert into apptable values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement insertstmt=conn.prepareStatement(insertquery);
insertstmt.setString(1, app_id);
insertstmt.setString(2,appname);
insertstmt.setTimestamp(3, (Timestamp) crawledDate);
insertstmt.setString(4,category.toString());
insertstmt.setString(5,offeredBy);
insertstmt.setString(6,version);
insertstmt.setDouble(7, cost);
insertstmt.setString(8,inapp_cost);
insertstmt.setString(9, desc);
insertstmt.setLong(10, numofratings[4]);
insertstmt.setLong(11, numofratings[3]);
insertstmt.setLong(12, numofratings[2]);
insertstmt.setLong(13, numofratings[1]);
insertstmt.setLong(14, numofratings[0]);
insertstmt.setDouble(15, numberOfStars);
insertstmt.setLong(16,peopleRated );
insertstmt.setString(17, whatsNew.toString());
java.sql.Date sqlDate = new java.sql.Date(lastupdated.getTime());
insertstmt.setDate(18,sqlDate );
if(appsize==0.0){
insertstmt.setString(19,null);
}
else{
insertstmt.setDouble(19,appsize);
}
insertstmt.setString(20, Developerinfo);
if(numinstallsupperbound==0){
insertstmt.setString(21, null);
}
else{
insertstmt.setLong(21, numinstallsupperbound);
}
if(numinstallslowerbound==0){
insertstmt.setString(22, null);
}
else{
insertstmt.setLong(22, numinstallslowerbound);
}
insertstmt.executeUpdate();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}