Implement time ago parser and improve localization handling
- Handle special cases for languages where the number is not shown - Rework the Downloader base implementation, allowing for more advanced things to be done - Separate the localization from the content country (just like YouTube let's the user choose both).
This commit is contained in:
parent
180836c180
commit
3638f0e0ea
274 changed files with 4770 additions and 3468 deletions
116
timeago-parser/raw/java/GeneratePatternClasses.java
Normal file
116
timeago-parser/raw/java/GeneratePatternClasses.java
Normal file
|
@ -0,0 +1,116 @@
|
|||
import com.grack.nanojson.JsonArray;
|
||||
import com.grack.nanojson.JsonObject;
|
||||
import com.grack.nanojson.JsonParser;
|
||||
import com.grack.nanojson.JsonParserException;
|
||||
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
|
||||
import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class GeneratePatternClasses {
|
||||
public static void main(String[] args) throws FileNotFoundException, JsonParserException {
|
||||
final InputStream resourceAsStream =
|
||||
new FileInputStream("timeago-parser/raw/unique_patterns.json");
|
||||
|
||||
final JsonObject from = JsonParser.object().from(resourceAsStream);
|
||||
final TreeMap<String, Object> map = new TreeMap<>(from);
|
||||
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
final String languageCode = entry.getKey().replace('-', '_');
|
||||
final Map<String, Object> unitsList = (Map<String, Object>) entry.getValue();
|
||||
|
||||
final String wordSeparator = (String) unitsList.get("word_separator");
|
||||
|
||||
final JsonArray seconds = (JsonArray) unitsList.get("seconds");
|
||||
final JsonArray minutes = (JsonArray) unitsList.get("minutes");
|
||||
final JsonArray hours = (JsonArray) unitsList.get("hours");
|
||||
final JsonArray days = (JsonArray) unitsList.get("days");
|
||||
final JsonArray weeks = (JsonArray) unitsList.get("weeks");
|
||||
final JsonArray months = (JsonArray) unitsList.get("months");
|
||||
final JsonArray years = (JsonArray) unitsList.get("years");
|
||||
|
||||
final StringBuilder specialCasesString = new StringBuilder();
|
||||
specialCasesConstruct(TimeAgoUnit.SECONDS, seconds, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.MINUTES, minutes, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.HOURS, hours, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.DAYS, days, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.WEEKS, weeks, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.MONTHS, months, specialCasesString);
|
||||
specialCasesConstruct(TimeAgoUnit.YEARS, years, specialCasesString);
|
||||
|
||||
System.out.println("Generating \"" + languageCode + "\" pattern class...");
|
||||
|
||||
try (final FileWriter fileOut = new FileWriter(
|
||||
"timeago-parser/src/main/java/org/schabi/newpipe/extractor/timeago/patterns/" +
|
||||
languageCode + ".java")) {
|
||||
final String test = INFO_CLASS_GENERATED + "\n" +
|
||||
"\n" +
|
||||
"package org.schabi.newpipe.extractor.timeago.patterns;\n\n" +
|
||||
"import org.schabi.newpipe.extractor.timeago.PatternsHolder;\n" +
|
||||
(specialCasesString.length() > 0 ? "import org.schabi.newpipe.extractor.timeago.TimeAgoUnit;\n" : "") +
|
||||
"\n" +
|
||||
"public class " + languageCode + " extends PatternsHolder {\n" +
|
||||
" private static final String WORD_SEPARATOR = \"" + wordSeparator + "\";\n" +
|
||||
" private static final String[]\n" +
|
||||
" SECONDS /**/ = {" + join(seconds) + "},\n" +
|
||||
" MINUTES /**/ = {" + join(minutes) + "},\n" +
|
||||
" HOURS /**/ = {" + join(hours) + "},\n" +
|
||||
" DAYS /**/ = {" + join(days) + "},\n" +
|
||||
" WEEKS /**/ = {" + join(weeks) + "},\n" +
|
||||
" MONTHS /**/ = {" + join(months) + "},\n" +
|
||||
" YEARS /**/ = {" + join(years) + "};\n" +
|
||||
"\n" +
|
||||
" private static final " + languageCode + " INSTANCE = new " + languageCode + "();\n" +
|
||||
"\n" +
|
||||
" public static " + languageCode + " getInstance() {\n" +
|
||||
" return INSTANCE;\n" +
|
||||
" }\n" +
|
||||
"\n" +
|
||||
" private " + languageCode + "() {\n" +
|
||||
" super(WORD_SEPARATOR, SECONDS, MINUTES, HOURS, DAYS, WEEKS, MONTHS, YEARS);\n" +
|
||||
specialCasesString.toString() +
|
||||
" }\n" +
|
||||
"}";
|
||||
fileOut.write(test);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void specialCasesConstruct(TimeAgoUnit unit, JsonArray array, StringBuilder stringBuilder) {
|
||||
final Iterator<Object> iterator = array.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final Object o = iterator.next();
|
||||
if (o instanceof JsonObject) {
|
||||
final JsonObject caseObject = (JsonObject) o;
|
||||
for (Map.Entry<String, Object> caseEntry : caseObject.entrySet()) {
|
||||
final int caseAmount = Integer.parseInt(caseEntry.getKey());
|
||||
final String caseText = (String) caseEntry.getValue();
|
||||
iterator.remove();
|
||||
|
||||
stringBuilder.append(" ")
|
||||
.append("putSpecialCase(TimeAgoUnit.").append(unit.name())
|
||||
.append(", \"").append(caseText).append("\"")
|
||||
.append(", ").append(caseAmount).append(");").append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final String INFO_CLASS_GENERATED = "/**/// DO NOT MODIFY THIS FILE MANUALLY\n" +
|
||||
"/**/// This class was automatically generated by \"GeneratePatternClasses.java\",\n" +
|
||||
"/**/// modify the \"unique_patterns.json\" and re-generate instead.";
|
||||
|
||||
private static String join(List<Object> list) {
|
||||
final StringBuilder toReturn = new StringBuilder();
|
||||
|
||||
for (Object o : list) {
|
||||
toReturn.append('"').append(o).append('"').append(", ");
|
||||
}
|
||||
toReturn.setLength(Math.max(toReturn.length() - 2, 0));
|
||||
|
||||
return toReturn.toString();
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
import com.grack.nanojson.JsonArray;
|
||||
import com.grack.nanojson.JsonObject;
|
||||
import com.grack.nanojson.JsonParser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import static org.schabi.newpipe.extractor.timeago.TimeAgoPatternsManager.RESOURCE_BUNDLE_ARRAY_SEPARATOR;
|
||||
|
||||
public class GenerateResourceBundles {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
File outDir = new File("timeago-parser/outBundle");
|
||||
if (!outDir.isDirectory()) outDir.mkdir();
|
||||
|
||||
JsonObject object = JsonParser.object().from(new FileInputStream(new File("timeago-parser/raw/unique_patterns.json")));
|
||||
|
||||
for (Map.Entry<String, Object> langTimeEntry : new TreeMap<>(object).entrySet()) {
|
||||
final String langName = langTimeEntry.getKey();
|
||||
StringBuilder outString = new StringBuilder();
|
||||
|
||||
final TreeMap<String, Object> sortedMap = new TreeMap<>(Utils.compareByUnitName());
|
||||
sortedMap.putAll((JsonObject) langTimeEntry.getValue());
|
||||
|
||||
final Iterator<Map.Entry<String, Object>> unitEntriesIterator = sortedMap.entrySet().iterator();
|
||||
while (unitEntriesIterator.hasNext()) {
|
||||
final Map.Entry<String, Object> unitEntry = unitEntriesIterator.next();
|
||||
final String unitName = unitEntry.getKey();
|
||||
final List<Object> unitList = (JsonArray) unitEntry.getValue();
|
||||
|
||||
outString.append(unitName).append("=\\\n");
|
||||
|
||||
for (int i = 0; i < unitList.size(); i++) {
|
||||
final String s = unitList.get(i).toString();
|
||||
outString.append(" ").append(s);
|
||||
|
||||
if (i < unitList.size() - 1) {
|
||||
outString.append(RESOURCE_BUNDLE_ARRAY_SEPARATOR).append("\\").append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (unitEntriesIterator.hasNext()) outString.append("\n\n");
|
||||
}
|
||||
|
||||
String fileName = "time_units_" + langName.replaceAll("-", "_") + ".properties";
|
||||
System.out.println("Writing " + fileName + "...");
|
||||
try (OutputStream out = new FileOutputStream(new File(outDir, fileName))) {
|
||||
out.write(outString.toString().getBytes("UTF-8"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue