add end time of sessions

This commit is contained in:
nyyu 2018-10-14 20:08:03 +02:00
parent 00faf31c82
commit 503b5c5137
2 changed files with 53 additions and 22 deletions

View file

@ -5,6 +5,10 @@
<groupId>tk.nyyu</groupId> <groupId>tk.nyyu</groupId>
<artifactId>devfest2018</artifactId> <artifactId>devfest2018</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.jsoup</groupId> <groupId>org.jsoup</groupId>

View file

@ -2,7 +2,8 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -29,6 +30,7 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -39,11 +41,29 @@ public class Program {
public static void main(String[] args) throws IOException, InvalidFormatException { public static void main(String[] args) throws IOException, InvalidFormatException {
Document doc = Jsoup.parse(new File("DevFest Nantes 2018.html"), "UTF-8"); Document doc = Jsoup.parse(new File("DevFest Nantes 2018.html"), StandardCharsets.UTF_8.name());
JsonObject sessions = new JsonParser().parse( JsonObject sessions = new JsonParser().parse(
IOUtils.toString(new URL("https://devfest.gdgnantes.com/data/sessions.json"), Charset.forName("UTF-8"))) IOUtils.toString(new URL("https://devfest.gdgnantes.com/data/sessions.json"), StandardCharsets.UTF_8))
.getAsJsonObject(); .getAsJsonObject();
JsonArray schedule = new JsonParser().parse(
IOUtils.toString(new URL("https://devfest.gdgnantes.com/data/schedule.json"), StandardCharsets.UTF_8))
.getAsJsonArray();
Map<String, String> timeslotSessions = new HashMap<>();
for (JsonElement day : schedule) {
JsonArray timeslots = day.getAsJsonObject().get("timeslots").getAsJsonArray();
for (JsonElement timeslot : timeslots) {
JsonArray sessionIds = timeslot.getAsJsonObject().get("sessions").getAsJsonArray();
String startTime = timeslot.getAsJsonObject().get("startTime").getAsString();
String endTime = timeslot.getAsJsonObject().get("endTime").getAsString();
for (JsonElement id : sessionIds) {
timeslotSessions.put(id.getAsString(), String.format("%s - %s", startTime, endTime));
}
}
}
XSSFWorkbook workBook = new XSSFWorkbook(); XSSFWorkbook workBook = new XSSFWorkbook();
CreationHelper factory = workBook.getCreationHelper(); CreationHelper factory = workBook.getCreationHelper();
@ -54,35 +74,28 @@ public class Program {
styleWrap.setVerticalAlignment(VerticalAlignment.CENTER); styleWrap.setVerticalAlignment(VerticalAlignment.CENTER);
for (int i = 1; i <= 2; i++) { for (int i = 1; i <= 2; i++) {
Map<String, List<ImmutablePair<String, String>>> map = new LinkedHashMap<String, List<ImmutablePair<String, String>>>(); Map<String, List<ImmutablePair<String, String>>> map = new LinkedHashMap<>();
Elements timeSlots = doc.select("schedule-day[name=day" + i + "] .timeslot"); Elements timeSlots = doc.select("schedule-day[name=day" + i + "] .timeslot");
for (Element timeSlot : timeSlots) { for (Element timeSlot : timeSlots) {
String time = timeSlot.select(".hours").text() + ":" + timeSlot.select(".minutes").text();
List<ImmutablePair<String, String>> list = map.get(time);
if (list == null)
list = new LinkedList<ImmutablePair<String, String>>();
Elements contents = timeSlot.select(".session-content"); Elements contents = timeSlot.select(".session-content");
for (Element content : contents) { for (Element content : contents) {
String title = content.select(".session-title").text(); String title = content.select(".session-title").text();
String id = content.parent().parent().parent().select("a").attr("href").split("=")[1]; String id = content.parent().parent().parent().select("a").attr("href").split("=")[1];
String type = sessions.getAsJsonObject(id).get("type").getAsString(); JsonObject session = sessions.getAsJsonObject(id);
JsonElement cat = sessions.getAsJsonObject(id).get("category"); String type = session.get("type").getAsString();
JsonElement comp = sessions.getAsJsonObject(id).get("complexity"); String cat = getIfNotNull(session, "category");
JsonElement lang = sessions.getAsJsonObject(id).get("language"); String comp = getIfNotNull(session, "complexity");
JsonElement desc = sessions.getAsJsonObject(id).get("description"); String lang = getIfNotNull(session, "language");
String desc = getIfNotNull(session, "description");
String time = timeslotSessions.get(id);
String details = String.format("type: %s, category: %s, complexity: %s, language: %s\n\n%s", type, String details = String.format("type: %s, category: %s, complexity: %s, language: %s\n\n%s", type,
cat != null ? cat.getAsString() : null, comp != null ? comp.getAsString() : null, cat, comp, lang, desc);
lang != null ? lang.getAsString() : null, desc != null ? desc.getAsString() : null);
list.add(new ImmutablePair<String, String>(title, details)); putOrNew(map, time, new ImmutablePair<String, String>(title, details));
logger.info("{} -> {}", time, title); logger.info("{} -> {}", time, title);
} }
map.put(time, list);
} }
XSSFSheet sheet = workBook.createSheet(); XSSFSheet sheet = workBook.createSheet();
@ -91,7 +104,7 @@ public class Program {
for (Map.Entry<String, List<ImmutablePair<String, String>>> entry : map.entrySet()) { for (Map.Entry<String, List<ImmutablePair<String, String>>> entry : map.entrySet()) {
Row row = sheet.createRow(rownum++); Row row = sheet.createRow(rownum++);
int cellnum = 0; int cellnum = 0;
Cell cell = row.createCell(cellnum++); Cell cell = row.createCell(cellnum++);
cell.setCellValue(entry.getKey()); cell.setCellValue(entry.getKey());
@ -116,11 +129,25 @@ public class Program {
if (cellnum > 2) if (cellnum > 2)
sheet.createRow(rownum++); sheet.createRow(rownum++);
} }
sheet.autoSizeColumn(0);
} }
workBook.write(new FileOutputStream(new File(doc.title() + ".xlsx"))); workBook.write(new FileOutputStream(new File(doc.title() + ".xlsx")));
workBook.close(); workBook.close();
} }
private static <K, V> void putOrNew(Map<K, List<V>> map, K key, V val) {
List<V> list = map.get(key);
if (list == null)
list = new LinkedList<V>();
list.add(val);
map.put(key, list);
}
private static String getIfNotNull(JsonObject jsonObject, String name) {
return jsonObject.get(name) == null ? "" : jsonObject.get(name).getAsString();
}
} }