Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
adds support for twitter param link parsing
Browse files Browse the repository at this point in the history
fixes #135
  • Loading branch information
Seklfreak committed Jun 15, 2019
1 parent b068a41 commit 78d7a13
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func getDownloadLinks(inputURL string, channelID string, interactive bool) (map[
return skipDuplicateLinks(links, channelID, interactive), true
}
}
if RegexpUrlTwitterParams.MatchString(inputURL) {
links, err := getTwitterParamsUrls(inputURL)
if err != nil {
fmt.Println("twitter params URL failed,", inputURL, ",", err)
} else if len(links) > 0 {
return skipDuplicateLinks(links, channelID, interactive), true
}
}
if RegexpUrlTwitterStatus.MatchString(inputURL) {
links, err := getTwitterStatusUrls(inputURL, channelID)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,14 @@ func getTwitterUrls(url string) (map[string]string, error) {
return map[string]string{"https:" + parts[1] + ":orig": filenameFromUrl(parts[1])}, nil
}

func getTwitterParamsUrls(url string) (map[string]string, error) {
matches := RegexpUrlTwitterParams.FindStringSubmatch(url)

return map[string]string{
"https://pbs.twimg.com/media/" + matches[3] + "." + matches[4] + ":orig": "",
}, nil
}

func getTwitterStatusUrls(url string, channelID string) (map[string]string, error) {
if twitterClient == nil {
return nil, errors.New("invalid twitter api keys set")
Expand Down
6 changes: 6 additions & 0 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

const (
REGEXP_URL_TWITTER = `^http(s?):\/\/pbs(-[0-9]+)?\.twimg\.com\/media\/[^\./]+\.(jpg|png)((\:[a-z]+)?)$`
REGEXP_URL_TWITTER_PARAMS = `^http(s?):\/\/pbs(-[0-9]+)?\.twimg\.com\/media\/([^\./]+)\?format=(jpg|png)&name=([a-z]+)[a-z=&]*$`
REGEXP_URL_TWITTER_STATUS = `^http(s?):\/\/(www\.)?twitter\.com\/([A-Za-z0-9-_\.]+\/status\/|statuses\/|i\/web\/status\/)([0-9]+)$`
REGEXP_URL_TISTORY = `^http(s?):\/\/t[0-9]+\.daumcdn\.net\/cfile\/tistory\/([A-Z0-9]+?)(\?original)?$`
REGEXP_URL_TISTORY_LEGACY = `^http(s?):\/\/[a-z0-9]+\.uf\.tistory\.com\/(image|original)\/[A-Z0-9]+$`
Expand All @@ -27,6 +28,7 @@ const (

var (
RegexpUrlTwitter *regexp.Regexp
RegexpUrlTwitterParams *regexp.Regexp
RegexpUrlTwitterStatus *regexp.Regexp
RegexpUrlTistory *regexp.Regexp
RegexpUrlTistoryLegacy *regexp.Regexp
Expand All @@ -50,6 +52,10 @@ func initRegex() error {
if err != nil {
return err
}
RegexpUrlTwitterParams, err = regexp.Compile(REGEXP_URL_TWITTER_PARAMS)
if err != nil {
return err
}
RegexpUrlTwitterStatus, err = regexp.Compile(REGEXP_URL_TWITTER_STATUS)
if err != nil {
return err
Expand Down

0 comments on commit 78d7a13

Please sign in to comment.