Skip to content

Commit

Permalink
Use simple matching to find shortcode_media in embed
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed Nov 17, 2024
1 parent f0dd30e commit dc7645c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 67 deletions.
22 changes: 14 additions & 8 deletions handlers/scraper/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,22 @@ func (i *InstaData) ScrapeData() error {
var embedData gjson.Result
var timeSliceData gjson.Result
if len(body) > 0 {
// Pattern matching using LDE
l := &Line{}
var scriptText []byte

// TimeSliceImpl
ldeMatch := false
// TimeSliceImpl (very fragile)
for _, line := range bytes.Split(body, []byte("\n")) {
// Check if line contains TimeSliceImpl
ldeMatch, _ = l.Extract(line)
if bytes.Contains(line, []byte("shortcode_media")) {
scriptText = line
break
}
}

if ldeMatch {
lexer := js.NewLexer(parse.NewInputBytes(l.GetTimeSliceImplValue()))
if len(scriptText) > 0 {
// Remove <script>
findFirstMoreThan := bytes.Index(scriptText, []byte(">"))
scriptText = scriptText[findFirstMoreThan+1:]

lexer := js.NewLexer(parse.NewInputBytes(scriptText))
for {
tt, text := lexer.Next()
if tt == js.ErrorToken || text == nil {
Expand All @@ -237,6 +241,8 @@ func (i *InstaData) ScrapeData() error {
timeSliceData = gjson.Parse(unescapeData).Get("gql_data")
}
}
} else {
slog.Warn("Failed to parse data from TimeSliceImpl", "postID", i.PostID, "err", "No script found")
}

// Scrape from embed HTML
Expand Down
59 changes: 0 additions & 59 deletions handlers/scraper/extractor_lde.go

This file was deleted.

0 comments on commit dc7645c

Please sign in to comment.