-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
108 lines (83 loc) · 2.15 KB
/
parser.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
106
107
108
package tgme
import (
"context"
"net/http"
"strings"
)
type Parser struct {
Client *http.Client
}
// User it's user or bot
type User struct {
// Name of user or bot
Name string `json:"name"`
// Username of user or bot
Username string `json:"username"`
// Bio of user or bot
Bio string `json:"bio,omitempty"`
// Link to avatar
Avatar string `json:"avatar,omitempty"`
}
// Channel it's Telegram channel.
type Channel struct {
// Title of channel
Title string `json:"title"`
// Members count
Members int `json:"members"`
// Description of channel
Description string `json:"description"`
// URL of channel avatar
Avatar string `json:"avatar"`
}
// Chat it's Telegram Chat
type Chat struct {
// Title of channel
Title string `json:"title"`
// Members count
Members int `json:"members"`
// Online members count
Online int `json:"online"`
// Description of channel
Description string `json:"description,omitempty"`
// URL of channel avatar
Avatar string `json:"avatar,omitempty"`
}
type Result struct {
User *User `json:"user,omitempty"`
Channel *Channel `json:"channel,omitempty"`
Chat *Chat `json:"chat,omitempty"`
}
// Parse returns info parsed from t.me. Link can be something like:
// - User: t.me/MrLinch
// - Bot: t.me/crosser_bot
// - Channel: t.me/crosser_live
// - Chat: t.me/crosser_chat
// - Private Channel: t.me/joinchat/AAAAAENM1m0f_WHVNXjP4w
// - Private Chat: https://t.me/joinchat/BEqxykAVc_3aNu906D2v7A
func (psr *Parser) Parse(ctx context.Context, link string) (*Result, error) {
client := psr.Client
if client == nil {
client = http.DefaultClient
}
if !strings.Contains(link, "http://") && !strings.Contains(link, "https://") {
link = "https://" + link
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, link, nil)
if err != nil {
return nil, err
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
page, err := newPage(res.Body)
if err != nil {
return nil, err
}
return page.Parse()
}
var defaultParser = &Parser{Client: http.DefaultClient}
func Parse(ctx context.Context, link string) (*Result, error) {
return defaultParser.Parse(ctx, link)
}