working calendar
This commit is contained in:
1 parent
0cb7e201fa
commit
754774a916
8 files changed
+377
-106
No files matched your search
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"java.compile.nullAnalysis.mode": "automatic"
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package fr.nessar.backend;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
public class UCalendar {
|
||||
|
||||
private final CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
public UCalendar(String group) {
|
||||
// prout
|
||||
}
|
||||
|
||||
public static String getCalendar(Date start, Date end, String group) throws IOException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
final HttpPost post = new HttpPost(UWebAPI.DOMAIN + UWebAPI.CALENDARDATA);
|
||||
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
params.add(new BasicNameValuePair("start", dateFormat.format(start)));
|
||||
params.add(new BasicNameValuePair("end", dateFormat.format(end)));
|
||||
params.add(new BasicNameValuePair("resType", "103"));
|
||||
params.add(new BasicNameValuePair("calView", "agendaDay"));
|
||||
params.add(new BasicNameValuePair("federationIds[]", group));
|
||||
post.addHeader("Connection", "keep-alive");
|
||||
post.addHeader("Pragma", "no-cache");
|
||||
post.addHeader("Cache-Control", "no-cache");
|
||||
post.addHeader("Accept", "application/json");
|
||||
post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
post.setEntity(new UrlEncodedFormEntity(params));
|
||||
try (CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client.execute(post)) {
|
||||
final int statusCode = response.getStatusLine().getStatusCode();
|
||||
assert (statusCode != HttpStatus.OK.value());
|
||||
return EntityUtils.toString(response.getEntity());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||
try {
|
||||
Date start = formatter.parse("01-01-2023");
|
||||
Date end;
|
||||
end = formatter.parse("31-07-2023");
|
||||
String s = getCalendar(start, end, "INF601A12");
|
||||
Gson gson = new Gson();
|
||||
ArrayList<UEvent> events = gson.fromJson(s, new TypeToken<ArrayList<UEvent>>() {
|
||||
}.getType());
|
||||
for (UEvent event : events) {
|
||||
System.out.println(event.description);
|
||||
System.out.println("SITES : " + (event.sites == null ? "null" : event.sites[0]));
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void close() throws IOException {
|
||||
httpClient.close();
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package fr.nessar.backend;
|
||||
|
||||
public class UEvent {
|
||||
public String id;
|
||||
public String start;
|
||||
public String end;
|
||||
public boolean allDay;
|
||||
public String description;
|
||||
public String backgroundColor;
|
||||
public String textColor;
|
||||
public String department;
|
||||
public String faculty;
|
||||
public String eventCategory;
|
||||
public String[] sites;
|
||||
public String[] modules;
|
||||
public int registerStatus;
|
||||
public int studentMark;
|
||||
public String custom1;
|
||||
public String custom2;
|
||||
public String custom3;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package fr.nessar.backend.VCal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import fr.nessar.backend.u.UCalendar;
|
||||
import fr.nessar.backend.u.UEvent;
|
||||
|
||||
public class VCalendar {
|
||||
|
||||
private final StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
public VCalendar(UCalendar calendar) {
|
||||
stringBuilder.append("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:" + calendar.getCalName() + "\r\n");
|
||||
stringBuilder.append(
|
||||
"BEGIN:VTIMEZONE\r\nTZID:Europe/Paris\r\nX-LIC-LOCATION:Europe/Paris\r\nBEGIN:DAYLIGHT\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nTZNAME:CEST\r\nDTSTART:19700329T020000\r\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nTZNAME:CET\r\nDTSTART:19701025T030000\r\nRRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n");
|
||||
this.addEvent(calendar.getEvents());
|
||||
}
|
||||
|
||||
public void addEvent(UEvent event) {
|
||||
VEvent v = new VEvent(event);
|
||||
this.addEvent(v);
|
||||
}
|
||||
|
||||
public void addEvent(VEvent v) {
|
||||
this.stringBuilder.append(v.getBuilder());
|
||||
}
|
||||
|
||||
public void addEvent(List<UEvent> u) {
|
||||
for (UEvent event : u) {
|
||||
this.addEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.stringBuilder.toString() + "END:VCALENDAR";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package fr.nessar.backend.VCal;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import fr.nessar.backend.u.UEvent;
|
||||
|
||||
public class VEvent {
|
||||
|
||||
private StringBuilder stringBuilder = new StringBuilder();
|
||||
|
||||
public VEvent(UEvent event) {
|
||||
this.stringBuilder.append("BEGIN:VEVENT\r\n");
|
||||
this.stringBuilder.append("UID:" + UUID.randomUUID().toString() + "\r\n");
|
||||
String modules = "";
|
||||
if (event.modules != null)
|
||||
for (String mod : event.modules) {
|
||||
modules += mod + ", ";
|
||||
}
|
||||
this.stringBuilder.append(
|
||||
"SUMMARY:"
|
||||
+ parseComma("[" + event.eventCategory + "] " + modules
|
||||
+ (event.modules != null ? event.modules[0] : "???"))
|
||||
+ "\r\n");
|
||||
Date now = new Date();
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
|
||||
this.stringBuilder.append("DTSTAMP;TZID=Europe/Paris:" + sdf.format(now) + "\r\n");
|
||||
this.stringBuilder.append("DTSTART;TZID=Europe/Paris:" + event.getParsedStart() + "\r\n");
|
||||
if (!event.allDay)
|
||||
this.stringBuilder.append("DTEND;TZID=Europe/Paris:" + event.getParsedEnd() + "\r\n");
|
||||
String room = event.getRoom();
|
||||
String desc;
|
||||
if (room == null)
|
||||
desc = parseDesc(event.description);
|
||||
else {
|
||||
desc = room + "\\nGroups: " + event.getGroups() + "\\nProf: " + event.getProf()
|
||||
+ "\\nNotes: " + event.getNotes();
|
||||
}
|
||||
this.stringBuilder.append("DESCRIPTION:" + parseComma(desc) + "\r\n");
|
||||
String loc = event.getLocation();
|
||||
if (loc != null)
|
||||
this.stringBuilder.append("LOCATION:" + parseComma(loc) + "\r\n");
|
||||
this.stringBuilder.append("END:VEVENT\r\n");
|
||||
}
|
||||
|
||||
private String parseComma(String input) {
|
||||
return input.replace(",", "\\,");
|
||||
}
|
||||
|
||||
private String parseDesc(String input) {
|
||||
return input.replace("\r", "").replace("\n", "");
|
||||
}
|
||||
|
||||
public StringBuilder getBuilder() {
|
||||
return this.stringBuilder;
|
||||
}
|
||||
|
||||
public String getEvent() {
|
||||
return this.stringBuilder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package fr.nessar.backend.u;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import fr.nessar.backend.VCal.VCalendar;
|
||||
|
||||
public class UCalendar {
|
||||
|
||||
private final String group;
|
||||
private final Date start;
|
||||
private final Date end;
|
||||
private final String calName;
|
||||
private final ArrayList<UEvent> events;
|
||||
private final HttpPost post = new HttpPost(UWebAPI.DOMAIN + UWebAPI.CALENDARDATA);
|
||||
public static final String FORMAT = "dd-MM-yyyy";
|
||||
public static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat(FORMAT);
|
||||
|
||||
public UCalendar(String group, Date start, Date end, List<String> moduleUnwanted) {
|
||||
this.group = group;
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
this.calName = group + "_" + DATEFORMAT.format(start) + "_" + DATEFORMAT.format(end);
|
||||
this.events = this.getCalendar();
|
||||
this.sortList(moduleUnwanted);
|
||||
}
|
||||
|
||||
public ArrayList<UEvent> getCalendar() {
|
||||
final List<NameValuePair> params = new ArrayList<NameValuePair>();
|
||||
params.add(new BasicNameValuePair("start", DATEFORMAT.format(this.start)));
|
||||
params.add(new BasicNameValuePair("end", DATEFORMAT.format(this.end)));
|
||||
params.add(new BasicNameValuePair("resType", "103"));
|
||||
params.add(new BasicNameValuePair("calView", "agendaDay"));
|
||||
params.add(new BasicNameValuePair("federationIds[]", this.group));
|
||||
this.post.addHeader("Connection", "keep-alive");
|
||||
this.post.addHeader("Pragma", "no-cache");
|
||||
this.post.addHeader("Cache-Control", "no-cache");
|
||||
this.post.addHeader("Accept", "application/json");
|
||||
this.post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
try {
|
||||
this.post.setEntity(new UrlEncodedFormEntity(params));
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = (CloseableHttpResponse) client.execute(this.post);
|
||||
final int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != HttpStatus.OK.value())
|
||||
throw new RuntimeException("Excepted 200 but got :" + response.getStatusLine().getStatusCode());
|
||||
String calString = EntityUtils.toString(response.getEntity());
|
||||
Gson gson = new Gson();
|
||||
ArrayList<UEvent> events = gson.fromJson(calString, new TypeToken<ArrayList<UEvent>>() {
|
||||
}.getType());
|
||||
client.close();
|
||||
return events;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private void sortList(List<String> moduleUnwanted) {
|
||||
if (moduleUnwanted != null)
|
||||
for (int i = 0; i < this.events.size(); i++) {
|
||||
String[] mods = this.events.get(i).modules;
|
||||
if (mods != null) {
|
||||
String mod = mods[0];
|
||||
for (String UWmod : moduleUnwanted) {
|
||||
if (mod.contains(UWmod))
|
||||
this.events.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Set<String> loggers = new HashSet<>(Arrays.asList("org.apache.http", "groovyx.net.http"));
|
||||
|
||||
for (String log : loggers) {
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(log);
|
||||
logger.setLevel(Level.INFO);
|
||||
logger.setAdditive(false);
|
||||
}
|
||||
try {
|
||||
UCalendar cal = new UCalendar("INF601A12", DATEFORMAT.parse("01-01-2023"), DATEFORMAT.parse("31-07-2023"),
|
||||
null);
|
||||
VCalendar vcal = new VCalendar(cal);
|
||||
FileWriter f = new FileWriter("test.ics");
|
||||
f.write("\ufeff");
|
||||
f.write(vcal.toString());
|
||||
f.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public List<UEvent> getEvents() {
|
||||
return this.events;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Date getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public String getCalName() {
|
||||
return calName;
|
||||
}
|
||||
|
||||
public HttpPost getPost() {
|
||||
return post;
|
||||
}
|
||||
|
||||
public static String getFormat() {
|
||||
return FORMAT;
|
||||
}
|
||||
|
||||
public static SimpleDateFormat getDateformat() {
|
||||
return DATEFORMAT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package fr.nessar.backend.u;
|
||||
|
||||
public class UEvent {
|
||||
public String id;
|
||||
public String start;
|
||||
public String end;
|
||||
public boolean allDay;
|
||||
public String description;
|
||||
public String backgroundColor;
|
||||
public String textColor;
|
||||
public String department;
|
||||
public String faculty;
|
||||
public String eventCategory;
|
||||
public String[] sites;
|
||||
public String[] modules;
|
||||
public int registerStatus;
|
||||
public int studentMark;
|
||||
public String custom1;
|
||||
public String custom2;
|
||||
public String custom3;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "id: " + this.id + ", start: " + this.start + ", Pstart: " + this.getParsedStart() + ", end: " + this.end
|
||||
+ ", Pend: " + this.getParsedEnd() + ", allDay: " + this.allDay + ", description: " + this.description
|
||||
+ ", backgroundColor: " + this.backgroundColor + ", textColor: " + this.textColor + ", department: "
|
||||
+ this.department + ", faculty: " + this.faculty + ", eventCategory: " + this.eventCategory
|
||||
+ ", sites: " + (this.sites != null ? this.sites[0] : "aucun") + ", modules: "
|
||||
+ (this.modules != null ? this.modules[0] : "aucun") + ", registerStatus: " + this.registerStatus
|
||||
+ ", studentMark: " + this.studentMark + ", custom1: " + this.custom1 + ", custom2: " + this.custom2
|
||||
+ ", custom3: " + this.custom3 + ", Prof: " + this.getProf() + ", Groups: " + this.getGroups()
|
||||
+ ", Notes:" + this.getNotes();
|
||||
}
|
||||
|
||||
public String getParsedStart() {
|
||||
String str = this.start.replaceAll("-", "").replaceAll(":", "");
|
||||
int index = str.indexOf("T");
|
||||
if (this.allDay)
|
||||
return str.substring(0, index);
|
||||
return str;
|
||||
}
|
||||
|
||||
public String getParsedEnd() {
|
||||
if (this.end == null)
|
||||
return "null";
|
||||
return this.end.replaceAll("-", "").replaceAll(":", "");
|
||||
|
||||
}
|
||||
|
||||
public String getGroups() {
|
||||
String[] strs = this.description.replaceAll("\\r", "").replaceAll("\\n", "").split("<br />");
|
||||
if (strs.length > 2)
|
||||
return strs[2];
|
||||
return "???";
|
||||
}
|
||||
|
||||
public String getProf() {
|
||||
String[] strs = this.description.replaceAll("\\r", "").replaceAll("\\n", "").split("<br />");
|
||||
int indexRoom = getRoomIndex(strs);
|
||||
if (strs.length > indexRoom - 1 && indexRoom - 1 != 2)
|
||||
return strs[indexRoom - 1];
|
||||
return "???";
|
||||
}
|
||||
|
||||
public String getNotes() {
|
||||
String[] strs = this.description.replaceAll("\\r", "").replaceAll("\\n", "").split("<br />");
|
||||
if (!Character.isDigit(strs[strs.length - 1].charAt(0)))
|
||||
return strs[strs.length - 1];
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
if (this.sites == null)
|
||||
return null;
|
||||
String loc = this.sites[0].replaceAll("Bâtiment ", "");
|
||||
return loc + ", 33400 Talence, France";
|
||||
}
|
||||
|
||||
private int getRoomIndex(String[] strs) {
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
if (strs[i] != null)
|
||||
if (strs[i].contains("Amphithéâtre ") || strs[i].contains("Salle "))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String getRoom() {
|
||||
String[] strs = this.description.replaceAll("\\r", "").replaceAll("\\n", "").split("<br />");
|
||||
int indexRoom = getRoomIndex(strs);
|
||||
if (indexRoom == -1)
|
||||
return null;
|
||||
String locAndRoom = strs[indexRoom];
|
||||
String[] amphis = locAndRoom.split("Amphithéâtre ");
|
||||
String[] salles = locAndRoom.split("Salle ");
|
||||
String ret = null;
|
||||
if (salles.length == 2)
|
||||
ret = "Salle: ";
|
||||
else if (salles.length > 2)
|
||||
ret = "Salles: ";
|
||||
else if (amphis.length == 2)
|
||||
ret = "Amphithéâtre: ";
|
||||
else if (amphis.length > 2)
|
||||
ret = "Amphithéâtres: ";
|
||||
else
|
||||
return null;
|
||||
for (int i = 1; i < salles.length; i++) {
|
||||
if (i % 2 == 1)
|
||||
ret += salles[i] + (i + 1 == salles.length ? ", " : "");
|
||||
}
|
||||
for (int i = 1; i < amphis.length; i++) {
|
||||
if (i % 2 == 1)
|
||||
ret += amphis[i] + (i + 1 == amphis.length ? ", " : "");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package fr.nessar.backend;
|
||||
package fr.nessar.backend.u;
|
||||
|
||||
public class UWebAPI {
|
||||
public static final String DOMAIN = "https://celcat.u-bordeaux.fr/Calendar/Home/";
|
||||
Reference in new issue
Block a user