Commit dd283bf0 authored by Doğukan Çelik's avatar Doğukan Çelik

Object parser implemented

parent dc7b2a75
......@@ -41,13 +41,14 @@
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/cavabunga-lib.jar</systemPath>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
package edu.itu.cavabunga.cavabungacollectorlwncalendar.controller;
import org.springframework.stereotype.Controller;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMContinue;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMDate;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.parser.LwnCalendarParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
import java.util.List;
@RestController
@RequestMapping
public class MainController {
@Autowired
private LwnCalendarParser lwnCalendarParser;
@GetMapping
public List<CalMContinue> parse(){
return lwnCalendarParser.parseCalendarForContinue("2018","07");
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.http;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.exception.CollectorException;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@Component
public class HttpAdapter {
public static String doRequest(String requestUrl) {
public String doRequest(String requestUrl) {
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
......@@ -26,38 +25,4 @@ public class HttpAdapter {
throw new CollectorException("There is something wrong with http connection: " + e.getMessage());
}
}
public static void main(String[] args) throws IOException {
org.jsoup.nodes.Document doc = Jsoup.connect("https://lwn.net/Calendar/Monthly/2018-06/").get();
org.jsoup.select.Elements rows = doc.select("tr");
for (org.jsoup.nodes.Element row : rows) {
Integer rowCount = 1;
Integer columnCount = 1;
Integer maxColumnCount = 8;
org.jsoup.select.Elements columns = row.select("td");
for (org.jsoup.nodes.Element column : columns) {
/*if(column.className().equals("CalMDate")){
System.out.println("Date " + column.text());
System.out.println();
}else if(column.className().equals("CalMEvent")){
System.out.println("Event: " + column.text());
System.out.println();
}else if(column.className().equals("CalMContinue")){
System.out.println("Continue//");
System.out.println();
}*/
if (column.className().equals("CalMEvent") && !column.text().trim().equals("")) {
System.out.println("Event: " + column.text() + " row:" + rowCount + " column:" + columnCount);
}
columnCount++;
if (columnCount == 8) {
columnCount = 1;
}
}
rowCount++;
}
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper;
public class IcalMapper {
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper;
import org.springframework.stereotype.Component;
@Component
public class LwnObjectMapper {
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element;
import lombok.Data;
@Data
public class CalMContinue extends Element{
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element;
import lombok.Data;
@Data
public class CalMDate extends Element{
private String date;
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element;
import lombok.Data;
import java.util.Date;
@Data
public class CalMEvent extends Element{
private String eventDate;
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element;
import lombok.Data;
@Data
public abstract class Element {
private Integer row;
private Integer column;
private String text;
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMContinue;
public interface CalMContinueFactory {
CalMContinue create();
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMContinue;
import org.springframework.stereotype.Component;
@Component
public class CalMContinueFactoryImp implements CalMContinueFactory {
public CalMContinueFactoryImp(){}
public CalMContinue create(){
return new CalMContinue();
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMDate;
public interface CalMDateFactory {
CalMDate create();
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMDate;
import org.springframework.stereotype.Component;
@Component
public class CalMDateFactoryImp implements CalMDateFactory {
public CalMDateFactoryImp(){}
public CalMDate create(){
return new CalMDate();
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
public interface CalMEventFactory {
CalMEvent create();
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
import org.springframework.stereotype.Component;
@Component
public class CalMEventFactoryImp implements CalMEventFactory {
public CalMEventFactoryImp(){}
public CalMEvent create(){
return new CalMEvent();
}
}
package edu.itu.cavabunga.cavabungacollectorlwncalendar.parser;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.exception.CollectorException;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.http.HttpAdapter;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.LwnObjectMapper;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMContinue;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMDate;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.element.CalMEvent;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory.CalMContinueFactory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory.CalMDateFactory;
import edu.itu.cavabunga.cavabungacollectorlwncalendar.mapper.factory.CalMEventFactory;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class LwnCalendarParser {
private HttpAdapter httpAdapter;
private CalMContinueFactory calMContinueFactory;
private CalMDateFactory calMDateFactory;
private CalMEventFactory calMEventFactory;
private LwnObjectMapper lwnObjectMapper;
private String baseLwnCalendarUrl;
@Autowired
public LwnCalendarParser(HttpAdapter httpAdapter,
CalMContinueFactory calMContinueFactory,
CalMEventFactory calMEventFactory,
CalMDateFactory calMDateFactory,
LwnObjectMapper lwnObjectMapper){
this.httpAdapter = httpAdapter;
this.calMContinueFactory = calMContinueFactory;
this.calMEventFactory = calMEventFactory;
this.calMDateFactory = calMDateFactory;
this.lwnObjectMapper = lwnObjectMapper;
this.baseLwnCalendarUrl = "https://lwn.net/Calendar/Monthly/";
}
public List<CalMDate> parseCalendarForDate(String year, String month){
String calendarUrl = this.baseLwnCalendarUrl + year + "-" + month + "/";
List<CalMDate> result = new ArrayList<>();
try {
org.jsoup.nodes.Document doc = Jsoup.connect(calendarUrl).get();
org.jsoup.select.Elements rows = doc.select("tr");
Integer rowCount = 1;
Integer columnCount = 1;
Integer maxColumnCount = 8;
for (org.jsoup.nodes.Element row : rows) {
org.jsoup.select.Elements columns = row.select("td");
for (org.jsoup.nodes.Element column : columns) {
if (column.className().equals("CalMDate") && !column.text().trim().equals("")) {
CalMDate date = calMDateFactory.create();
if(Integer.parseInt(column.text()) < 10 ){
date.setText("0" + column.text());
}else{
date.setText(column.text());
}
date.setColumn(columnCount);
date.setRow(rowCount);
date.setDate(year + month + date.getText());
System.out.println(date.getDate());
result.add(date);
}
if(column.className().equals("CalBlank") ||
column.className().equals("CalMDate") ||
column.className().equals("CalMEvent") ||
column.className().equals("CalMContinue")){
columnCount++;
}
if (columnCount.equals(maxColumnCount)){
columnCount = 1;
}
}
if (row.className().equals("CalM")) {
rowCount++;
}
columnCount = 1;
}
}catch (Exception e){
throw new CollectorException("There is something went wrong while parsing calendar data: " + e.getMessage());
}
return result;
}
public List<CalMEvent> parseCalendarForEvent(String year, String month){
String calendarUrl = this.baseLwnCalendarUrl + year + "-" + month + "/";
List<CalMEvent> result = new ArrayList<>();
try {
org.jsoup.nodes.Document doc = Jsoup.connect(calendarUrl).get();
org.jsoup.select.Elements rows = doc.select("tr");
Integer rowCount = 1;
Integer columnCount = 1;
Integer maxColumnCount = 8;
for (org.jsoup.nodes.Element row : rows) {
org.jsoup.select.Elements columns = row.select("td");
for (org.jsoup.nodes.Element column : columns) {
if (column.className().equals("CalMEvent") && !column.text().trim().equals("")) {
CalMEvent event = calMEventFactory.create();
event.setText(column.text());
event.setColumn(columnCount);
event.setRow(rowCount);
System.out.println(event.getRow() + " " + event.getColumn() + " " + event.getText());
result.add(event);
}
if(column.className().equals("CalBlank") ||
column.className().equals("CalMDate") ||
column.className().equals("CalMEvent") ||
column.className().equals("CalMContinue")){
columnCount++;
}
if (columnCount.equals(maxColumnCount)){
columnCount = 1;
}
}
if (row.className().equals("CalM")) {
rowCount++;
}
columnCount = 1;
}
}catch (Exception e){
throw new CollectorException("There is something went wrong while parsing calendar data: " + e.getMessage());
}
return result;
}
public List<CalMContinue> parseCalendarForContinue(String year, String month){
String calendarUrl = this.baseLwnCalendarUrl + year + "-" + month + "/";
List<CalMContinue> result = new ArrayList<>();
try {
org.jsoup.nodes.Document doc = Jsoup.connect(calendarUrl).get();
org.jsoup.select.Elements rows = doc.select("tr");
Integer rowCount = 1;
Integer columnCount = 1;
Integer maxColumnCount = 8;
for (org.jsoup.nodes.Element row : rows) {
org.jsoup.select.Elements columns = row.select("td");
for (org.jsoup.nodes.Element column : columns) {
if (column.className().equals("CalMContinue")) {
CalMContinue mContinue = calMContinueFactory.create();
mContinue.setText(column.text());
mContinue.setColumn(columnCount);
mContinue.setRow(rowCount);
System.out.println(mContinue.getRow() + " " + mContinue.getColumn() + " " + mContinue.getText());
result.add(mContinue);
}
if(column.className().equals("CalBlank") ||
column.className().equals("CalMDate") ||
column.className().equals("CalMEvent") ||
column.className().equals("CalMContinue")){
columnCount++;
}
if (columnCount.equals(maxColumnCount)){
columnCount = 1;
}
}
if (row.className().equals("CalM")) {
rowCount++;
}
columnCount = 1;
}
}catch (Exception e){
throw new CollectorException("There is something went wrong while parsing calendar data: " + e.getMessage());
}
return result;
}
public void mapCoreObjectToIcalFormat(){ }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment