MCPcopy Create free account
hub / github.com/AudDMusic/RedditBot / GetLinkFromComment

Method GetLinkFromComment

main.go:189–305  ·  view source on GitHub ↗
(mention *reddit1.Message, commentsTree []*models.Comment, post *models.Post)

Source from the content-addressed store, hash-verified

187}
188
189func (r *auddBot) GetLinkFromComment(mention *reddit1.Message, commentsTree []*models.Comment, post *models.Post) (string, error) {
190 // Check:
191 // - first for the links in the comment
192 // - then for the links in the comment to which it was a reply
193 // - then for the links in the post
194 var resultUrl string
195 if post != nil {
196 resultUrl = post.URL
197 }
198 if strings.Contains(resultUrl, "reddit.com/rpan") {
199 s := strings.Split(resultUrl, "/")
200 jsonUrl := "https://strapi.reddit.com/videos/t3_" + s[len(s)-1]
201 var page RedditStreamJSON
202 err := getJSON(jsonUrl, &page)
203 if err != nil {
204 return "", err
205 }
206 resultUrl = page.Data.Stream.HlsURL
207 if resultUrl != "" {
208 return resultUrl, nil
209 }
210 }
211 if len(commentsTree) > 0 {
212 if commentsTree[0] != nil {
213 if resultUrl == "" {
214 //resultUrl = commentsTree[0].r2.LinkURL
215 }
216 if mention == nil {
217 mention = commentToMessage(commentsTree[0])
218 if len(commentsTree) > 1 {
219 commentsTree = commentsTree[1:]
220 } else {
221 commentsTree = make([]*models.Comment, 0)
222 }
223 }
224 }
225 } else {
226 if mention == nil {
227 if post == nil {
228 return "", fmt.Errorf("mention, commentsTree, and post are all nil")
229 }
230 mention = &reddit1.Message{
231 Context: post.Permalink,
232 Body: post.Selftext,
233 }
234 }
235 }
236 if resultUrl == "" {
237 j, _ := json.Marshal(post)
238 fmt.Printf("Got a post that's not a link or an empty post (https://www.reddit.com%s, %s)\n", mention.Context, string(j))
239 //return "", fmt.Errorf("got a post without any URL (%s)", string(j))
240 resultUrl = "https://www.reddit.com" + mention.Context
241 }
242 if strings.Contains(resultUrl, "reddit.com/") || resultUrl == "" {
243 if post != nil {
244 if strings.Contains(post.Selftext, "https://reddit.com/link/"+post.ID+"/video/") {
245 s := strings.Split(post.Selftext, "https://reddit.com/link/"+post.ID+"/video/")
246 s = strings.Split(s[1], "/")

Callers 2

GetVideoLinkMethod · 0.95
HandleQueryMethod · 0.95

Calls 4

getJSONFunction · 0.85
commentToMessageFunction · 0.85
linksFromBodyFunction · 0.85
captureFunction · 0.85

Tested by

no test coverage detected