commit 664195d24da6408707aecda856fb7f6f9ff3c494 Author: Iain R. Learmonth irl@fsfe.org Date: Tue Mar 20 14:11:49 2018 +0000
Imports UpdateNews.java
This commit imports UpdateNews.java verbatim, as attached to #23854 by Karsten.
https://trac.torproject.org/projects/tor/attachment/ticket/23854/UpdateNews.... --- .../org/torproject/metrics/web/UpdateNews.java | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+)
diff --git a/src/main/java/org/torproject/metrics/web/UpdateNews.java b/src/main/java/org/torproject/metrics/web/UpdateNews.java new file mode 100644 index 0000000..527988f --- /dev/null +++ b/src/main/java/org/torproject/metrics/web/UpdateNews.java @@ -0,0 +1,131 @@ +package org.torproject.metrics.web; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.BufferedReader; +import java.io.FileWriter; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class UpdateNews { + private static class News { + String start; + String end; + Boolean ongoing; + List<String> places; + List<String> protocols; + String description; + List<String> links; + Boolean unknown; + } + + /** Update news. */ + public static void main(String[] args) throws Exception { + URL textFile = new URL( + "https://trac.torproject.org/projects/tor/wiki/doc/" + + "MetricsTimeline?format=txt"); + Gson gson = new GsonBuilder().disableHtmlEscaping().create(); + List<News> news = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new InputStreamReader( + textFile.openStream()))) { + String line; + Boolean unknown = null; + while ((line = br.readLine()) != null) { + if (line.startsWith("== Unknown")) { + unknown = true; + continue; + } + if (!line.startsWith("||") || line.startsWith("||=start")) { + continue; + } + line = line.trim() + .replaceAll("×", "×") + .replaceAll("§", "§") + .replaceAll("–", "–") + .replaceAll("“", "“") + .replaceAll("”", "”"); + String[] parts = line.split("\|\|"); + News entry = new News(); + entry.start = parts[1].replaceAll("~", "").trim(); + if (entry.start.contains(" ")) { + entry.start = entry.start.substring(0, entry.start.indexOf(" ")); + } else if (entry.start.isEmpty()) { + entry.start = null; + } + entry.end = parts[2].replaceAll("~", "").trim(); + if ("ongoing".equalsIgnoreCase(entry.end)) { + entry.end = null; + entry.ongoing = true; + } else if (entry.end.contains(" ")) { + entry.end = entry.end.substring(0, entry.end.indexOf(" ")); + } else if (entry.end.isEmpty()) { + entry.end = null; + } + for (String place : parts[3].trim().split(" +")) { + if (!place.isEmpty()) { + if (null == entry.places) { + entry.places = new ArrayList<>(); + } + entry.places.add(place); + } + } + for (String protocol : parts[4].trim().split(" +")) { + if (!protocol.isEmpty()) { + if (null == entry.protocols) { + entry.protocols = new ArrayList<>(); + } + entry.protocols.add(protocol); + } + } + String desc = parts[5].trim(); + while (desc.contains("[")) { + int open = desc.indexOf("["); + int space = desc.indexOf(" ", open); + int close = desc.indexOf("]", open); + if (open < 0 || space < 0 || close < 0) { + System.err.println("Cannot convert link."); + System.exit(1); + } + desc = desc.substring(0, open) + "<a href="" + + desc.substring(open + 1, space) + "">" + + desc.substring(space + 1, close) + "</a>" + + desc.substring(close + 1); + } + while (desc.contains("`")) { + int open = desc.indexOf("`"); + int close = desc.indexOf("`", open + 1); + if (open < 0 || close < 0) { + System.err.println("Cannot convert code fragment."); + System.exit(1); + } + desc = desc.substring(0, open) + "<code>" + + desc.substring(open + 1, close) + "</code>" + + desc.substring(close + 1); + } + entry.description = desc; + if (parts.length >= 7) { + for (String link : parts[6].split("[\[\]]")) { + link = link.trim(); + if (link.isEmpty()) { + continue; + } + if (null == entry.links) { + entry.links = new ArrayList<>(); + } + entry.links.add("<a href="" + link.substring(0, link.indexOf(" ")) + + "">" + link.substring(link.indexOf(" ") + 1) + "</a>"); + } + } + entry.unknown = unknown; + news.add(entry); + } + } + try (FileWriter fw = new FileWriter( + "src/main/resources/web/json/news-raw.json")) { + fw.write(gson.toJson(news)); + } + } +}
tor-commits@lists.torproject.org