first commit

master v1.0.0
dai_48k 2024-02-03 17:24:14 +08:00
commit b286b8ae8f
10 changed files with 386 additions and 0 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
.idea/
out/
*.iml

2
banned.txt 100644
View File

@ -0,0 +1,2 @@
房,彩礼,单身,火了,专家,谣言,彩票,
张艺谋,郭德纲,贾玲

BIN
icon.jpg 100644

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

1
order.txt 100644
View File

@ -0,0 +1 @@
救,华春莹,外交部,雨,雪

55
src/EditFrame.java 100644
View File

@ -0,0 +1,55 @@
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
public class EditFrame extends JFrame {
public EditFrame(String title, String fileName) {
setTitle(title);
setSize(300,300);
setIconImage(new ImageIcon("icon.jpg").getImage());
File file = new File(fileName);
JTextArea jta = new JTextArea();
jta.setLineWrap(true);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
String tmp;
StringBuilder sb = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
sb.append(tmp).append("\n");
}
jta.setText(sb.toString());
} catch (Exception e){
e.printStackTrace();
}
JPanel bottom = new JPanel();
JButton ok = new JButton("确认");
ok.setBackground(Color.white);
JButton cancel = new JButton("取消");
cancel.setBackground(Color.white);
bottom.add(ok);
bottom.add(cancel);
setLocationRelativeTo(null);
getContentPane().add(jta, BorderLayout.CENTER);
getContentPane().add(bottom, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
ok.addActionListener(e -> {
try (FileWriter writer = new FileWriter(file, StandardCharsets.UTF_8)){
writer.write(jta.getText());
writer.flush();
NewsComparator.reloadConfig();
NewsPredicate.reloadConfig();
} catch (IOException ioException) {
ioException.printStackTrace();
}
dispose();
});
cancel.addActionListener(e -> dispose());
}
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: RunToday

View File

@ -0,0 +1,48 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
/**
*
*/
public class NewsComparator implements Comparator<OneNews> {
public static String[] valueInits;
/*= new String[]{
"救","华春莹", "外交部", "雨", "雪"
};*/
static {
reloadConfig();
}
public static void reloadConfig(){
File file = new File("order.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
String tmp;
StringBuilder sb = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
sb.append(tmp).append("\n");
}
valueInits = sb.toString().split(",");
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public int compare(OneNews o1, OneNews o2) {
return o2.getValue() - o1.getValue();
}
}

View File

@ -0,0 +1,60 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.function.Predicate;
/**
*
*/
public class NewsPredicate implements Predicate<OneNews> {
public static String[] banKeys;
/*= new String[]{
// 社会类
"房", "彩礼", "单身",
// 无意义炒作
"火了", "专家", "谣言", "彩票",
// 人物类
"张艺谋", "郭德纲", "贾玲"
};*/
static {
reloadConfig();
}
public static void reloadConfig(){
File file = new File("banned.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
String tmp;
StringBuilder sb = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
sb.append(tmp).append("\n");
}
banKeys = sb.toString().split(",");
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public boolean test(OneNews oneNews) {
if (banKeys.length == 1 && banKeys[0].equals("")) return true;
for (String banKey : banKeys) {
// 如果存在禁词,则拒绝此条
if (oneNews.getTitle().contains(banKey)){
return false;
}
}
// 无禁词,默认通过
return true;
}
}

54
src/OneNews.java 100644
View File

@ -0,0 +1,54 @@
public class OneNews {
private String title;
private String url;
private String content;
private int value;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "OneNews{" +
"title='" + title + '\'' +
", url='" + url + '\'' +
", content='" + content + '\'' +
", value=" + value +
'}';
}
}

160
src/RunToday.java 100644
View File

@ -0,0 +1,160 @@
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.plaf.metal.MetalScrollBarUI;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* CREATED BY liusu
*/
public class RunToday{
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame("kawachi 前线新闻");
jf.setSize(700,600);
jf.setIconImage(new ImageIcon("icon.jpg").getImage());
JToolBar toolBar = new JToolBar();
JButton fresh = new JButton("刷新");
JButton editBanned = new JButton("过滤");
JButton editOrder = new JButton("排序");
JButton author = new JButton("关于");
toolBar.add(fresh);
toolBar.add(editBanned);
toolBar.add(editOrder);
toolBar.add(author);
toolBar.setFloatable(false);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
drawPanel(getList(), panel);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
scrollPane.getVerticalScrollBar().setUI(new BasicScrollBarUI() {
@Override
protected void configureScrollBarColors() {
this.thumbColor = Color.PINK;
}
});
jf.add(toolBar, BorderLayout.NORTH);
jf.add(scrollPane, BorderLayout.CENTER);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
fresh.addActionListener( e ->
{
panel.removeAll();
panel.updateUI();
try {
drawPanel(getList(), panel);
} catch (URISyntaxException | IOException uriSyntaxException) {
uriSyntaxException.printStackTrace();
}
SwingUtilities.invokeLater(() -> scrollPane.getVerticalScrollBar().setValue(0));
}
);
editBanned.addActionListener(e -> {
new EditFrame("编辑过滤器", "banned.txt").setVisible(true);
});
editOrder.addActionListener(e -> {
new EditFrame("编辑排序器", "order.txt").setVisible(true);
});
author.addActionListener(e -> {
JOptionPane.showMessageDialog(null, "<html>作者: 时光潜流<br/></html>名称: kawachi 前线新闻");
});
}
private static List<OneNews> getList() throws URISyntaxException, IOException {
URL url = new URI("https://top.baidu.com/board?tab=realtime").toURL();
URLConnection urlConnection = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) urlConnection;
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))){
String tmp;
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
} catch (Exception e){
e.printStackTrace();
}
List<OneNews> list = new LinkedList<>();
Pattern patternTitle = Pattern.compile("<div class=\"c-single-text-ellipsis\">(.+?) </div>");
Pattern patternUrl = Pattern.compile("<a[^>]*href=\"([^\"]*)\".class=\"title_dIF3B.*?>");
Pattern patternContent = Pattern.compile("<!--s-frag-->[^\"].*?<div class=\".*? \">[^\"](.*?)<");
Matcher m1 = patternTitle.matcher(sb);
Matcher m2 = patternUrl.matcher(sb);
Matcher m3 = patternContent.matcher(sb);
while (m1.find() && m2.find() && m3.find()){
OneNews tmp = new OneNews();
tmp.setTitle(m1.group(1).trim());
tmp.setUrl(m2.group(1).trim());
tmp.setContent(m3.group(1).trim());
tmp.setValue(0);
// 获取权重
for (String valueInit : NewsComparator.valueInits) {
if (tmp.getTitle().contains(valueInit)) {
tmp.setValue(tmp.getValue() + 1);
}
}
list.add(tmp);
}
return list.stream().sorted(new NewsComparator()).filter(new NewsPredicate()).collect(Collectors.toList());
}
private static void drawPanel(List<OneNews> list, JPanel panel) {
Font font = new Font("宋体", Font.BOLD, 16);
AtomicInteger i = new AtomicInteger();
list.forEach(item -> {
i.getAndIncrement();
JButton tmp = new JButton("<html><u> " + (i.get() < 10 ? "0" + i : i) + "." + item.getTitle() + "</u></html>");
tmp.setBackground(Color.WHITE);
tmp.setFont(font);
tmp.setForeground(Color.BLUE);
tmp.setHorizontalAlignment(JButton.LEFT);
tmp.setBorder(null);
tmp.addActionListener(e -> {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(item.getUrl()));
} catch (IOException | URISyntaxException ioException) {
ioException.printStackTrace();
}
});
JTextArea jta = new JTextArea(" " + (item.getContent().equals("") ? "-" : item.getContent()));
jta.setLineWrap(true);
jta.setEditable(false);
jta.setFont(new Font("宋体", Font.PLAIN, 16));
panel.add(tmp);
panel.add(jta);
});
}
}