Skip to content

Commit 62c5007

Browse files
authored
Fix not parsing all news ticker properly (#291)
* fix not parsing all news ticker properly * add news 504 to testdata
1 parent 9b7ca9e commit 62c5007

File tree

3 files changed

+822
-8
lines changed

3 files changed

+822
-8
lines changed

src/TibiaNews.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ type NewsResponse struct {
2727
Information Information `json:"information"`
2828
}
2929

30-
var (
31-
martelRegex = regexp.MustCompile(`<img src=\"https:\/\/static\.tibia\.com\/images\/global\/letters\/letter_martel_(.)\.gif\" ([^\/>]+..)`)
32-
)
30+
var martelRegex = regexp.MustCompile(`<img src=\"https:\/\/static\.tibia\.com\/images\/global\/letters\/letter_martel_(.)\.gif\" ([^\/>]+..)`)
3331

3432
func TibiaNewsImpl(NewsID int, rawUrl string, BoxContentHTML string) (NewsResponse, error) {
3533
// Declaring vars for later use..
@@ -89,11 +87,20 @@ func TibiaNewsImpl(NewsID int, rawUrl string, BoxContentHTML string) (NewsRespon
8987
// checking if its a ticker..
9088
if NewsData.Type == "ticker" {
9189
tmp1 = s.Find("p")
92-
NewsData.Content = tmp1.Text()
93-
NewsData.ContentHTML, err = tmp1.Html()
94-
if err != nil {
95-
insideError = fmt.Errorf("[error] TibiaNewsImpl failed at NewsData.ContentHTML, err = tmp1.Html(), err: %s", err)
96-
return false
90+
if tmp1.Text() == "" {
91+
NewsData.Content = s.Text()
92+
NewsData.ContentHTML, err = s.Html()
93+
if err != nil {
94+
insideError = fmt.Errorf("[error] TibiaNewsImpl failed at NewsData.ContentHTML, err = s.Html(), err: %s", err)
95+
return false
96+
}
97+
} else {
98+
NewsData.Content = tmp1.Text()
99+
NewsData.ContentHTML, err = tmp1.Html()
100+
if err != nil {
101+
insideError = fmt.Errorf("[error] TibiaNewsImpl failed at NewsData.ContentHTML, err = tmp1.Html(), err: %s", err)
102+
return false
103+
}
97104
}
98105
} else {
99106
// getting html

src/TibiaNews_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,35 @@ func TestNews6512(t *testing.T) {
6666
assert.Equal("<a href=\"https://tibiadata.com\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">TibiaData.com</a> has some news to share! First of all, they invite you to participate in their <a href=\"https://tibiadata.com/2021/07/join-tibiadata-on-discord/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">Discord Server</a>. Further, they are now present on <a href=\"https://tibiadata.com/2021/12/tibiadata-has-gone-open-source/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">GitHub</a>. They are working on their <a href=\"https://tibiadata.com/doc-api-v3/v3-beta/\" target=\"_blank\" rel=\"noopener noreferrer\" rel=\"noopener\">v3</a>, which is still in beta. If you are interested in such things, head on over there to see what is cooking.", newsArticleJson.News.ContentHTML)
6767
}
6868

69+
func TestNews504(t *testing.T) {
70+
file, err := static.TestFiles.Open("testdata/news/archive/504.html")
71+
if err != nil {
72+
t.Fatalf("file opening error: %s", err)
73+
}
74+
defer file.Close()
75+
76+
data, err := io.ReadAll(file)
77+
if err != nil {
78+
t.Fatalf("File reading error: %s", err)
79+
}
80+
81+
newsArticleJson, err := TibiaNewsImpl(504, "https://www.tibia.com/news/?subtopic=newsarchive&id=504", string(data))
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
assert := assert.New(t)
87+
88+
assert.Equal(504, newsArticleJson.News.ID)
89+
assert.Equal("2007-04-27", newsArticleJson.News.Date)
90+
assert.Empty(newsArticleJson.News.Title)
91+
assert.Equal("community", newsArticleJson.News.Category)
92+
assert.Equal("ticker", newsArticleJson.News.Type)
93+
assert.Equal("https://www.tibia.com/news/?subtopic=newsarchive&id=504", newsArticleJson.News.TibiaURL)
94+
assert.Equal("A new feedback form has been released today. Help us to find out which websites and magazines are popular in your country by filling out the new questionnaire. In our current poll we are curious about your occupation.", newsArticleJson.News.Content)
95+
assert.Equal("<br/>A new feedback form has been released today. Help us to find out which websites and magazines are popular in your country by filling out the new questionnaire. In our current poll we are curious about your occupation.", newsArticleJson.News.ContentHTML)
96+
}
97+
6998
func TestNews6481(t *testing.T) {
7099
file, err := static.TestFiles.Open("testdata/news/archive/6481.html")
71100
if err != nil {

0 commit comments

Comments
 (0)