-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.go
105 lines (96 loc) · 3.02 KB
/
news.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
var num_article int = 1
var news News
var news_url *url.URL
func showNews() {
w := myapp.NewWindow("News App")
w.Resize(fyne.NewSize(400, 200))
URL := "https://gnews.io/api/v4/top-headlines?token=f4242aaddbb109f07654431e207e2412&lang=en&max=100"
//API
res, _ := http.Get(URL)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
news, _ = UnmarshalNews(body)
fmt.Println(news)
// num_article = int(news.TotalArticles)
//show title
label3 := widget.NewLabel(news.Articles[1].Title)
label3.TextStyle = fyne.TextStyle{Bold: true}
label3.Wrapping = fyne.TextWrapBreak
// show articles
entry1 := widget.NewLabel(news.Articles[1].Description)
//entry1.MultiLine = true
entry1.Wrapping = fyne.TextWrapBreak
news_url, _ = url.Parse(news.Articles[1].URL)
new_link := widget.NewHyperlink("", news_url)
next_icon := canvas.NewImageFromFile("media\\next-icon.jpg")
link_icon := canvas.NewImageFromFile("media\\link.jpg")
img := canvas.NewImageFromFile("media\\news2.png")
img.FillMode = canvas.ImageFillOriginal
img_container := container.NewGridWrap(fyne.NewSize(400, 250), container.NewPadded(img))
btn := widget.NewButton("", func() {
num_article += 1
label3.Text = news.Articles[num_article].Title
entry1.Text = news.Articles[num_article].Description
news_url, _ = url.Parse(news.Articles[num_article].URL)
new_link.URL = news_url
new_link.Refresh()
label3.Refresh()
entry1.Refresh()
})
x := container.New(layout.NewVBoxLayout(), label3, layout.NewSpacer(), entry1, layout.NewSpacer(),
container.New(layout.NewHBoxLayout(),
container.NewGridWrap(fyne.NewSize(25, 25), container.NewPadded(new_link, link_icon)),
layout.NewSpacer(),
container.NewGridWrap(fyne.NewSize(25, 25), container.NewPadded(btn, next_icon)),
),
)
r, _ := fyne.LoadResourceFromPath("media\\newslogo.png")
w.SetIcon(r)
// e.Resize(fyne.NewSize(300, 300))
w.SetContent(container.NewBorder(img_container, nil, nil, nil, x))
w.CenterOnScreen()
w.Show()
}
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
// news, err := UnmarshalNews(bytes)
// bytes, err = news.Marshal()
func UnmarshalNews(data []byte) (News, error) {
var r News
err := json.Unmarshal(data, &r)
return r, err
}
func (r *News) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type News struct {
TotalArticles int64 `json:"totalArticles"`
Articles []Article `json:"articles"`
}
type Article struct {
Title string `json:"title"`
Description string `json:"description"`
Content string `json:"content"`
URL string `json:"url"`
Image string `json:"image"`
PublishedAt string `json:"publishedAt"`
Source Source `json:"source"`
}
type Source struct {
Name string `json:"name"`
URL string `json:"url"`
}