Skip to content

Commit

Permalink
Merge pull request #1 from ciCciC/feature/showingImageBetweenSections
Browse files Browse the repository at this point in the history
Feature/showing image between sections
  • Loading branch information
ciCic authored May 16, 2020
2 parents ab68a7a + c5f473e commit 6f8a711
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 44 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Default ignored files
.idea/*.iws
.idea/*.iml
.idea/*.ipr
.idea/workspace.xml

5 changes: 0 additions & 5 deletions .idea/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion api/nrcnewsapi/src/config/Config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ func GetCollector() *colly.Collector {
colly.Async(true),
)
return collector
}
}
10 changes: 8 additions & 2 deletions api/nrcnewsapi/src/controller/BaseController.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package controller

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
)

type BaseController interface {
InitRoute(r *gin.Engine)
}
}

const (
CATEGORY = "category"
)
19 changes: 19 additions & 0 deletions api/nrcnewsapi/src/controller/CategoryController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package controller

import (
"github.com/gin-gonic/gin"
"net/http"
)

type CategoryController struct {
BaseController
}

func (c CategoryController) InitRoute(r *gin.Engine) {
r.GET("/categories", c.GetCategories)
}

func (c CategoryController) GetCategories(context *gin.Context) {
context.JSON(http.StatusOK,
[...] string {"games", "technology", "physics"})
}
4 changes: 2 additions & 2 deletions api/nrcnewsapi/src/controller/GameController.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type GameController struct {

func (t GameController) InitRoute(r *gin.Engine) {
scraper := scraper.Scraper{Endpoint: "games"}
game := r.Group("/category")
game := r.Group("/" + CATEGORY)
{
game.GET("/games", scraper.GetAllArticles())
game.GET("/games/article", scraper.GetArticle())
game.POST("/games/article", scraper.GetArticle())
}
}
4 changes: 2 additions & 2 deletions api/nrcnewsapi/src/controller/PhysicsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type PhysicsController struct {

func (t PhysicsController) InitRoute(r *gin.Engine) {
scraper := scraper.Scraper{Endpoint: "natuurkunde"}
physics := r.Group("/category")
physics := r.Group("/" + CATEGORY)
{
physics.GET("/physics", scraper.GetAllArticles())
physics.GET("/physics/article", scraper.GetArticle())
physics.POST("/physics/article", scraper.GetArticle())
}
}
8 changes: 4 additions & 4 deletions api/nrcnewsapi/src/controller/TechnologyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ type TechnologyController struct {

func (t TechnologyController) InitRoute(r *gin.Engine) {
scraper := scraper.Scraper{Endpoint: "technologie"}
technology := r.Group("/category")
technology := r.Group("/" + CATEGORY)
{
technology.GET("/technology",
scraper.GetAllArticles())
technology.GET("/technology", scraper.GetAllArticles())
technology.POST("/technology/article", scraper.GetArticle())
}
}
}
20 changes: 16 additions & 4 deletions api/nrcnewsapi/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@ package main

import (
"github.com/gin-gonic/gin"
"net/http"
"nrcnewsapi/api/nrcnewsapi/src/route"
)

func main() {
r := route.SetupRouter()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
welcomeTxt := " _ _ _____ _ \r\n | | | | / ____| | |\r\n | | ___| |_ ___ | (___ ___ _ __ __ _ _ __ ___ | |\r\n | | / _ \\ __/ __| \\___ \\ / __| '__/ _` | '_ \\ / _ \\ | |\r\n | |___| __/ |_\\__ \\ ____) | (__| | | (_| | |_) | __/ |_|\r\n |______\\___|\\__|___/ |_____/ \\___|_| \\__,_| .__/ \\___| (_)\r\n | | \r\n |_| "

r.GET("/",
func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
c.JSON(http.StatusOK, gin.H{
"": welcomeTxt,
})
})
})
r.Run()
}

func preflight(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
c.JSON(http.StatusOK, struct{}{})
}
2 changes: 1 addition & 1 deletion api/nrcnewsapi/src/model/Article.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package model

type Article struct {
ArticleItem
SectionList []Section
SectionList []Section `json:"sectionList"`
}
10 changes: 5 additions & 5 deletions api/nrcnewsapi/src/model/ArticleItem.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package model

type ArticleItem struct {
PageLink string
ImageLink string
Topic string
Title string
Teaser string
PageLink string `json:"pageLink"`
ImageLink string `json:"imageLink"`
Topic string `json:"topic"`
Title string `json:"title"`
Teaser string `json:"teaser"`
}
6 changes: 6 additions & 0 deletions api/nrcnewsapi/src/model/ContentBody.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package model

type ContentBody struct {
Content string `json:"content"`
CType string `json:"cType"`
}
5 changes: 3 additions & 2 deletions api/nrcnewsapi/src/model/Section.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

type Section struct {
Title string
Contents []string
Title string `json:"title"`
//Contents []string
Contents []ContentBody `json:"contentBody"`
}
38 changes: 38 additions & 0 deletions api/nrcnewsapi/src/model/TestModel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package model

type TestModel struct {
TestA
TestB
TestExtendsAnimal
TestExtendsVehicle
}

type TestExtendsAnimal struct {
Name string
}

type TestExtendsVehicle struct {
Price int
}

type TestA interface {
TestAA() string
}

type TestB interface {
TestBB() string
}

func (t TestModel) TestAA() string {
return "TestModel TestAA"
}

func (t TestModel) TestBB() string {
return "TestModel TestBB"
}

func (t TestModel) UseBothExtends() string {
t.TestExtendsVehicle.Price = 125
t.TestExtendsAnimal.Name = "Insane Tiger"
return "Extends: TestExtendsVehicle and TestExtendsAnimal"
}
1 change: 1 addition & 0 deletions api/nrcnewsapi/src/route/Routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func SetupRouter() *gin.Engine {
new(controller.TechnologyController),
new(controller.GameController),
new(controller.PhysicsController),
new(controller.CategoryController),
}

for _, baseController := range cArr {
Expand Down
41 changes: 25 additions & 16 deletions api/nrcnewsapi/src/scraper/Scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (scraper Scraper) GetAllArticles() gin.HandlerFunc {
Attr("href")

imageLink := strings.
Split(e.ChildAttr("img", "data-src"), "|")[0]
Split(e.ChildAttr(IMG, "data-src"), "|")[0]

header := goQuerySelection.Find(".nmt-item__content")

Expand Down Expand Up @@ -81,31 +81,34 @@ func (scraper Scraper) GetArticle() gin.HandlerFunc {
goQuerySelection := e.DOM

var dummy Dummy
goQuerySelection.Find("div.content > p, div.content > h2").
goQuerySelection.Find("div.content > p, div.content > h2, div.content > figure").
Each(func(i int, selection *goquery.Selection) {

if selection.Parent().Is("aside") {
return
}

if selection.Is("h2") {
if selection.Is(H2) {
dummy.Title = selection.Text()
dummy.Content = ""
dummy.Type = "h2"
dummy.ContentBody.Content = ""
dummy.ContentBody.CType = H2

buff = append(buff, dummy)
} else if selection.Is(P) && len(selection.Text()) > 0 {
dummy.ContentBody.Content = selection.Text()
dummy.ContentBody.CType = P

} else if selection.Is("p") && len(selection.Text()) > 0 {
dummy.Content = selection.Text()
dummy.Type = "p"

buff = append(buff, dummy)
} else if selection.Is(FIGURE) {
attr := e.ChildAttr(IMG, "data-src")
dummy.ContentBody.Content = strings.Split(attr, "|")[0]
dummy.ContentBody.CType = IMG
}

buff = append(buff, dummy)
})

groupedByTitle := linq.From(buff).GroupBy(
func(i interface{}) interface{} { return i.(Dummy).Title },
func(i interface{}) interface{} { return i.(Dummy).Content })
func(i interface{}) interface{} { return i.(Dummy).ContentBody })

groupedByTitle.ForEach(func(i interface{}) {
var section Section
Expand Down Expand Up @@ -134,7 +137,7 @@ func (scraper Scraper) GetArticle() gin.HandlerFunc {

c.Wait()

log.Println("Article scraped succesfully")
log.Println("Article scraped successfully")

context.JSON(http.StatusOK, article)
}
Expand Down Expand Up @@ -173,7 +176,13 @@ func printSection(section Section) {
}

type Dummy struct {
Title string
Content string
Type string
Title string
ContentBody ContentBody
}

const (
H2 = "h2"
P = "p"
IMG = "img"
FIGURE = "figure"
)

0 comments on commit 6f8a711

Please sign in to comment.