Skip to content

Commit

Permalink
✨add sessionize api integration
Browse files Browse the repository at this point in the history
Signed-off-by: Mateusz Halada <[email protected]>
  • Loading branch information
haladamateusz committed Jun 27, 2024
1 parent 2949b4c commit 7be5ddd
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import swiss.fihlon.apus.event.Session;
import swiss.fihlon.apus.plugin.event.EventConfig;
import swiss.fihlon.apus.plugin.event.demo.DemoConfig;
import swiss.fihlon.apus.plugin.event.doag.DoagConfig;
import swiss.fihlon.apus.plugin.event.sessionize.SessionizeConfig;
import swiss.fihlon.apus.plugin.social.mastodon.MastodonConfig;

@org.springframework.context.annotation.Configuration
Expand All @@ -41,6 +43,7 @@ public class Configuration {
// Event Plugin Configs
private DemoConfig demo;
private DoagConfig doag;
private SessionizeConfig sessionize;

// Social Plugin Configs
private MastodonConfig mastodon;
Expand Down Expand Up @@ -101,6 +104,12 @@ public void setDoag(@NotNull final DoagConfig doag) {
this.doag = doag;
}

public SessionizeConfig getSessionize() {
return sessionize;
}

public void setSessionize(@NotNull final SessionizeConfig sessionize) { this.sessionize = sessionize;}

///////////////////////////
// Social Plugin Configs //
///////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package swiss.fihlon.apus.plugin.event.sessionize;

public record SessionizeConfig(String eventId) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package swiss.fihlon.apus.plugin.event.sessionize;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import swiss.fihlon.apus.configuration.Configuration;
import swiss.fihlon.apus.event.Language;
import swiss.fihlon.apus.event.Room;
import swiss.fihlon.apus.event.Session;
import swiss.fihlon.apus.event.SessionImportException;
import swiss.fihlon.apus.event.Speaker;
import swiss.fihlon.apus.event.Track;
import swiss.fihlon.apus.plugin.event.EventPlugin;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public final class SessionizePlugin implements EventPlugin {
public static final Logger LOGGER = LoggerFactory.getLogger(SessionizePlugin.class);

private final String eventId;
private final String eventApi;

public SessionizePlugin(@NotNull final Configuration configuration) {
this.eventId = configuration.getSessionize().eventId();
this.eventApi = String.format("https://sessionize.com/api/v2/%s/view/Sessions", this.eventId);
}

@Override
public boolean isEnabled() {
return !eventId.equals("0");
}


@Override
@NotNull public List<Session> getSessions() {
final ArrayList<Session> sessions = new ArrayList<>();
int lastSlotId = 0;
try {
final String json = getJSON();
final JSONObject jsonObject = new JSONArray(json).getJSONObject(0);
final JSONArray sessionizeSessions = jsonObject.getJSONArray("sessions");
for (int counter = 0; counter < sessionizeSessions.length(); counter++) {
JSONObject singleSession = sessionizeSessions.getJSONObject(counter);

String id = singleSession.getString("id");
LocalDateTime startDate = LocalDateTime.parse(singleSession.getString("startsAt"));
if (startDate.toLocalDate().getDayOfMonth() == 16) {
continue;
}
LocalDateTime endDate = LocalDateTime.parse(singleSession.getString("endsAt"));
Room room = new Room(singleSession.getString("room"));
String title = singleSession.getString("title");
final JSONArray persons = singleSession.getJSONArray("speakers");
final ArrayList<String> speakers = new ArrayList<>(persons.length());
for (int personCounter = 0; personCounter < persons.length(); personCounter++) {
final JSONObject person = persons.getJSONObject(personCounter);
final String publicName = person.getString("name");
speakers.add(publicName);
}


Session session = new Session(id,startDate, endDate, room, title,
speakers.stream().map(Speaker::new).toList(), Language.EN, Track.NONE);

sessions.add(session);
}
LOGGER.info("Successfully loaded {} sessions for event ID {}", sessions.size(), eventId);
} catch (IOException | URISyntaxException | JSONException e) {
throw new SessionImportException(String.format("Error parsing slot %d: %s", lastSlotId, e.getMessage()), e);
}
return sessions;
}

private String getJSON() throws IOException, URISyntaxException {
LOGGER.info("Starting download of JSON for event ID {} using address {}", eventId, eventApi);
try (InputStream in = new URI(eventApi).toURL().openStream()) {
final String json = new String(in.readAllBytes(), StandardCharsets.UTF_8);
LOGGER.info("Successfully downloaded JSON for event ID {}", eventId);
return json;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,10 @@
"name" : "apus.mastodon.instance",
"type" : "java.lang.String",
"description" : "The Mastodon instance used to read the posts from."
},
{
"name" : "apus.sessionize.eventId",
"type" : "java.lang.String",
"description" : "The ID of the Sessionize event to read the conference agenda."
}
] }
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ apus.mastodon.hashtag=${MASTODON_HASHTAG:}
apus.mastodon.imageLimit=${MASTODON_IMAGE_LIMIT:1}
apus.mastodon.imagesEnabled=${MASTODON_IMAGES_ENABLED:true}
apus.mastodon.instance=${MASTODON_INSTANCE:}
apus.sessionize.eventId=${SESSIONIZE_EVENT_ID:0}

0 comments on commit 7be5ddd

Please sign in to comment.