Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
janwillies committed May 9, 2023
0 parents commit 9d33881
Show file tree
Hide file tree
Showing 17 changed files with 5,835 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/launch.json
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/janwillies/libplattentests-go

go 1.20

require (
github.com/PuerkitoBio/goquery v1.8.1
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
golang.org/x/net v0.9.0
)

require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/google/go-cmp v0.5.9
golang.org/x/text v0.9.0 // indirect
)
56 changes: 56 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20230418202329-0354be287a23 h1:4NKENAGIctmZYLK9W+X1kDK8ObBFqOSCJM6WE7CvkJY=
golang.org/x/exp v0.0.0-20230418202329-0354be287a23/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230419192730-864b3d6c5c2c h1:HDdYQYKOkvJT/Plb5HwJJywTVyUnIctjQm6XSnZ/0CY=
golang.org/x/exp v0.0.0-20230419192730-864b3d6c5c2c/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb h1:rhjz/8Mbfa8xROFiH+MQphmAmgqRM0bOMnytznhWEXk=
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
39 changes: 39 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"os"

"github.com/janwillies/libplattentests-go/pkg/mainpage"
"github.com/janwillies/libplattentests-go/pkg/reviews"

"golang.org/x/exp/slog"
)

func main() {
mc, err := mainpage.New()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}

mainpage, err := mc.GetMainpage()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
fmt.Printf("%+v\n", mainpage)

rc, err := reviews.New("https://www.plattentests.de/rezi.php?show=18380")
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}

review, err := rc.GetReview()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
fmt.Printf("%+v\n", review)
}
180 changes: 180 additions & 0 deletions pkg/mainpage/mainpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package mainpage

import (
"errors"
"net/http"
"strconv"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html/charset"
)

type Mainpage struct {
Hashsum int
ReviewIdCat map[int]string
Previews []Preview
}

type Preview struct {
Artist string
Album string
ReleaseDate time.Time
}

type MainpageClient struct {
Doc *goquery.Document
}

const (
baseUrl = "https://www.plattentests.de/index.php"
selAlbumDerWoche = "div.adw h3"
selAktuelleHighlights = "#akt_highlights div div"
selNeueRezis = ".neuerezis li"
selWeiterAktuell = "#weiteraktuell li"
selVorschau = "#vorschau li"
catAlbumDerWoche = "adw"
catAktuelleHighlights = "akt_highlights"
catNeueRezis = "neuerezis"
catWeiterAktuell = "weiteraktuell"
)

func New() (*MainpageClient, error) {
return NewWithUrl(baseUrl)
}

func NewWithUrl(url string) (*MainpageClient, error) {
// Request the HTML page.
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("received bad http statuscode " + strconv.Itoa(resp.StatusCode))
}
// go works on utf-8 only
utf8body, err := charset.NewReader(resp.Body, "iso-8859-1")
if err != nil {
return nil, err
}
// Load HTML document
doc, err := goquery.NewDocumentFromReader(utf8body)
if err != nil {
return nil, err
}

return &MainpageClient{
Doc: doc,
}, nil

}

func (mc *MainpageClient) GetMainpage() (Mainpage, error) {
var mainpage Mainpage

mainpage.ReviewIdCat = make(map[int]string)

// Find the other Review items (bottom right)
others, err := mc.getRezis(selWeiterAktuell)
if err != nil {
return mainpage, err
}
for _, v := range others {
mainpage.ReviewIdCat[v] = catWeiterAktuell
}

// Find the newest Review items (left menu)
newest, err := mc.getRezis(selNeueRezis)
if err != nil {
return mainpage, err
}
for _, v := range newest {
mainpage.ReviewIdCat[v] = catNeueRezis
}

// Find the current highlights (center)
highlights, err := mc.getRezis(selAktuelleHighlights)
if err != nil {
return mainpage, err
}
for _, v := range highlights {
mainpage.ReviewIdCat[v] = catAktuelleHighlights
}

// Find the Review of the week (top)
adw, err := mc.getRezis(selAlbumDerWoche)
if err != nil {
return mainpage, err
}
for _, v := range adw {
mainpage.ReviewIdCat[v] = catAlbumDerWoche
}

// Find upcoming reviews
previews, err := mc.getPreviews(selVorschau)
if err != nil {
return mainpage, err
}
mainpage.Previews = previews

// create hashsum so we can later check whether we already have a copy
for k := range mainpage.ReviewIdCat {
mainpage.Hashsum += k
}

return mainpage, nil
}

func (mc *MainpageClient) getRezis(selector string) ([]int, error) {
var ids []int
var err error
mc.Doc.Find(selector).Each(func(i int, s *goquery.Selection) {
link, exists := s.Find("a").Attr("href")
if !exists {
return
}
id, errIn := findId(link)
if errIn != nil {
err = errIn
}
ids = append(ids, id)
})
return ids, err
}

func (mc *MainpageClient) getPreviews(selector string) ([]Preview, error) {
var previews []Preview
var err error
mc.Doc.Find(selector).Each(func(i int, s *goquery.Selection) {
var preview Preview

artistSlice := strings.Split(s.Text(), " - ")
preview.Artist = strings.TrimSpace(artistSlice[0])

albumSlice := strings.Split(artistSlice[1], "(")
preview.Album = strings.TrimSpace(albumSlice[0])

releaseSlice := strings.Split(albumSlice[1], ")")
releaseSlice = strings.Split(releaseSlice[0], " ")
releaseDate, errIn := time.Parse("02.01.2006", releaseSlice[1])
if errIn != nil {
err = errIn
}
preview.ReleaseDate = releaseDate

previews = append(previews, preview)
})
return previews, err
}

// extract id from rezi.php?show=18373
func findId(link string) (int, error) {
idSlice := strings.SplitAfter(link, "=")
id, err := strconv.Atoi(idSlice[len(idSlice)-1])
if err != nil {
return 0, err
}
return id, nil
}
Loading

0 comments on commit 9d33881

Please sign in to comment.