-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpiderActivity.java
More file actions
88 lines (79 loc) · 2.78 KB
/
Copy pathSpiderActivity.java
File metadata and controls
88 lines (79 loc) · 2.78 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
package com.example.newsreader;
import java.util.ArrayList;
import android.app.Activity;
public class SpiderActivity extends Activity {
private String URL; //(新闻的url)
private String website; //(新闻的来源网站:中大数计院)
private NewsData db;
public SpiderActivity(String site,String url,NewsData database){
website=site;
URL=url;
db=database;
}
//调用即可抓取所有新闻
@SuppressWarnings("rawtypes")
public int fetchNewsUpdate(){
Html homepage=new Html(URL);
int count=0;
ArrayList allurl=homepage.getAllUrl();
ArrayList col_student=new ArrayList();
ArrayList col_teaching=new ArrayList();
ArrayList col_learning=new ArrayList();
ArrayList col_activity=new ArrayList();
for (int i=0;i<allurl.size();i++){
String tempurl=allurl.get(i).toString();
Html temp=new Html(tempurl,0);
String Col=temp.getCol();
if (Col=="学生园地"){
col_student.add(tempurl);
}
if (Col=="教学经纬"){
col_teaching.add(tempurl);
}
if (Col=="学院动态"){
col_activity.add(tempurl);
}
if (Col=="学术资讯"){
col_learning.add(tempurl);
}
}
count+=updateProgram(col_student,"学生园地");
count+=updateProgram(col_teaching,"教学经纬");
count+=updateProgram(col_activity,"学院动态");
count+=updateProgram(col_learning,"学术资讯");
return count;
}
@SuppressWarnings("rawtypes")
private int updateProgram(ArrayList Col,String Program){
int count=0;
String tempLatestURL=db.getLatestURL(website,Program);
//empty
if (tempLatestURL=="-1"){
for (int i=Col.size()-1;i>=0;i--){
String tempurl=Col.get(i).toString();
Html temp=new Html(tempurl);
db.insert(tempurl, temp.getTitle(), "中山大学数计院", temp.getCol(), temp.getTime(), temp.getContent());
count++;
}
}
else if (tempLatestURL==Col.get(0).toString()){
//dont have to do anything
}
else {
int i;
for (i=Col.size()-1;i>=0;i--){
if (Col.get(i).toString()==tempLatestURL){
break;
}
}
i--;
for (;i>=0;i--){
String tempurl=Col.get(i).toString();
Html temp=new Html(tempurl);
db.insert(tempurl, temp.getTitle(), "中山大学数计院", temp.getCol(), temp.getTime(), temp.getContent());
count++;
}
}
return count;
}
}