This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
162 lines (146 loc) · 3.99 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package reddit
import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"runtime"
"strings"
"github.com/vartanbeno/go-reddit/v2/reddit"
"github.com/icedream/irc-medialink/parsers"
"github.com/icedream/irc-medialink/version"
)
const (
header = "\x0307Reddit\x03"
)
var emptyURLValues = url.Values{}
// Parser implements parsing for Reddit URLs.
type Parser struct {
api *reddit.Client
Config *Config
}
// Init initializes this parser.
func (p *Parser) Init(_ context.Context) error {
// <platform>:<app ID>:<version string> (by /u/<reddit username>)
// TODO make username configurable
userAgent := fmt.Sprintf("%s:%s:%s (by /u/%s)", runtime.GOOS, "tech.icedream.medialink", strings.TrimLeft(version.AppVersion, "v"), p.Config.RedditUsername)
log.Println("Using user agent:", userAgent)
var err error
var creds *reddit.Credentials
if len(p.Config.ClientID) > 0 {
creds = &reddit.Credentials{
ID: p.Config.ClientID,
Secret: p.Config.ClientSecret,
}
}
opts := []reddit.Opt{
reddit.WithUserAgent(userAgent),
reddit.WithApplicationOnlyOAuth(true),
}
if creds != nil {
log.Println("Using reddit credentials")
p.api, err = reddit.NewClient(*creds, opts...)
} else {
log.Println("Using readonly reddit client")
p.api, err = reddit.NewReadonlyClient(opts...)
}
if err != nil {
return err
}
return nil
}
// Name returns the parser's descriptive name.
func (p *Parser) Name() string {
return "Reddit"
}
var (
rxPostRoute = regexp.MustCompile(".+/comments/(?P<id>[a-z0-9]+)(?:/.*|$)")
rxWikiRoute = regexp.MustCompile("^/r/(?P<name>[^/]+)/wiki/(?:revisions/|edit/)?(?P<id>[a-z0-9]+)/?$")
// rxCommentRoute = regexp.MustCompile(".+/comments/(?P<id>[a-z0-9]+)/comment/(?P<cid>[a-z0-9]+)(?:/.*)?")
rxSubredditRoute = regexp.MustCompile("/r/(?P<name>[^/]+)$")
)
// Parse parses the given URL.
func (p *Parser) Parse(ctx context.Context, u *url.URL, referer *url.URL) (result parsers.ParseResult) {
if !strings.EqualFold(u.Host, "reddit.com") &&
!strings.EqualFold(u.Host, "www.reddit.com") {
result.Ignored = true
return
}
if m := rxPostRoute.FindStringSubmatch(u.Path); m != nil {
// path points to a post
id := m[1]
pac, _, err := p.api.Post.Get(ctx, id)
if err != nil {
result.Error = err
return
}
result.Information = []map[string]interface{}{
{
"IsUpload": true,
"IsArticle": true,
"IsPost": true,
"Author": pac.Post.Author,
"PublishedAt": pac.Post.Created.Time,
"Spoiler": pac.Post.Spoiler,
"Upvotes": uint64(pac.Post.Score),
"Comments": uint64(pac.Post.NumberOfComments),
"IsSelfPost": pac.Post.IsSelfPost,
"Locked": pac.Post.Locked,
"Title": pac.Post.Title,
"Description": pac.Post.Body,
"Header": header,
},
}
if pac.Post.Edited != nil {
result.Information[0]["ModifiedAt"] = pac.Post.Edited.Time
}
if pac.Post.NSFW {
result.Information[0]["AgeRestriction"] = "NSFW"
}
} else if m := rxSubredditRoute.FindStringSubmatch(u.Path); m != nil {
// path points to a post
name := m[1]
pac, _, err := p.api.Subreddit.Get(ctx, name)
if err != nil {
result.Error = err
return
}
result.Information = []map[string]interface{}{
{
"IsGroup": true,
"Name": pac.Name,
"PublishedAt": pac.Created.Time,
"Subscribers": uint64(pac.Subscribers),
"Title": pac.Title,
"Description": pac.Description,
"Header": header,
},
}
if pac.NSFW {
result.Information[0]["AgeRestriction"] = "NSFW"
}
} else if m := rxWikiRoute.FindStringSubmatch(u.Path); m != nil {
// path points to a post
subreddit := m[1]
page := m[2]
pac, _, err := p.api.Wiki.Page(ctx, subreddit, page)
if err != nil {
result.Error = err
return
}
result.Information = []map[string]interface{}{
{
"Description": pac.Content,
"Header": header,
},
}
if pac.RevisionDate != nil {
result.Information[0]["ModifiedAt"] = pac.RevisionDate.Time
}
} else {
// all other paths
result.Ignored = true
}
return
}