Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Twitter social media info #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
rxWhitespace = regexp.MustCompile(`(?i)^\s*$`)
rxHasContent = regexp.MustCompile(`(?i)\S$`)
rxPropertyPattern = regexp.MustCompile(`(?i)\s*(dc|dcterm|og|twitter)\s*:\s*(author|creator|description|title|site_name|image\S*)\s*`)
rxNamePattern = regexp.MustCompile(`(?i)^\s*(?:(dc|dcterm|og|twitter|weibo:(article|webpage))\s*[\.:]\s*)?(author|creator|description|title|site_name|image)\s*$`)
rxNamePattern = regexp.MustCompile(`(?i)^\s*(?:(dc|dcterm|og|twitter|weibo:(article|webpage))\s*[\.:]\s*)?(author|creator|description|title|site|site_name|image)\s*$`)
rxTitleSeparator = regexp.MustCompile(`(?i) [\|\-\\/>»] `)
rxTitleHierarchySep = regexp.MustCompile(`(?i) [\\/>»] `)
rxTitleRemoveFinalPart = regexp.MustCompile(`(?i)(.*)[\|\-\\/>»] .*`)
Expand Down Expand Up @@ -76,6 +76,11 @@ type parseAttempt struct {
textLength int
}

// SocialInfo info contains information about the social media accounts related to this page
type SocialInfo struct {
Username string
}

// Article is the final readable content.
type Article struct {
Title string
Expand All @@ -88,6 +93,7 @@ type Article struct {
SiteName string
Image string
Favicon string
Social map[string]SocialInfo
}

// Parser is the parser that parses the page to get the readable content.
Expand Down Expand Up @@ -1263,13 +1269,26 @@ func (ps *Parser) getArticleMetadata() map[string]string {
metadataExcerpt = shtml.UnescapeString(metadataExcerpt)
metadataSiteName = shtml.UnescapeString(metadataSiteName)

metadataTwitter := ""
possibleAttrNames = []string{"twitter:site", "twitter:creator"}
for _, name := range possibleAttrNames {
if value, ok := values[name]; ok {
metadataTwitter = value
if metadataTwitter != "" && metadataTwitter[0] != '@' {
metadataTwitter = "@" + metadataTwitter
}
break
}
}

return map[string]string{
"title": metadataTitle,
"byline": metadataByline,
"excerpt": metadataExcerpt,
"siteName": metadataSiteName,
"image": metadataImage,
"favicon": metadataFavicon,
"twitter": metadataTwitter,
}
}

Expand Down Expand Up @@ -1943,6 +1962,13 @@ func (ps *Parser) Parse(input io.Reader, pageURL string) (Article, error) {
validByline := strings.ToValidUTF8(finalByline, "")
validExcerpt := strings.ToValidUTF8(excerpt, "")

social := make(map[string]SocialInfo)
if twitter, ok := metadata["twitter"]; ok && twitter != "" {
social["twitter"] = SocialInfo{
Username: twitter,
}
}

return Article{
Title: validTitle,
Byline: validByline,
Expand All @@ -1954,6 +1980,7 @@ func (ps *Parser) Parse(input io.Reader, pageURL string) (Article, error) {
SiteName: metadata["siteName"],
Image: metadata["image"],
Favicon: metadata["favicon"],
Social: social,
}, nil
}

Expand Down