Skip to content

Commit

Permalink
feat: Parse labels from Blogger
Browse files Browse the repository at this point in the history
If they have the category prefix, add them to the slice of categories. Otherwise, add them to the slice of tags
  • Loading branch information
slashtechno committed Jul 14, 2024
1 parent 03dff67 commit d9339c9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var watchCmd = &cobra.Command{
log.Debug("Destination is not Markdown; not cleaning up posts", "destination", dest.GetName())
}
}

}
wg.Add(1)
go func() {
Expand Down
29 changes: 29 additions & 0 deletions internal/platforms/blogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ func (b Blogger) Pull(options PushPullOptions) (PostData, error) {
return PostData{}, fmt.Errorf("updated date not found in response or is not a string")
}
// result["labels"].([]interface{})
labels := []string{}
categories := []string{}
tags := []string{}
// Assert that the labels key is a slice of interfaces
// Type assertions don't work on a per-element basis so we have to loop through the slice
labelsAsserted, ok := result["labels"].([]interface{})
if !ok {
log.Infof("No labels found for post %s", title)
} else {
for _, tag := range labelsAsserted {
if tagStr, ok := tag.(string); ok {
labels = append(labels, tagStr)
} else {
log.Warn("Blogger label is not a string", "tag", tag)
}
}
// For each label, check if it has the category prefix
// If it does, add it to the categories slice
// Otherwise, add it to the tags slice
for _, label := range labels {
if strings.HasPrefix(label, b.CategoryPrefix) {
categories = append(categories, strings.TrimPrefix(label, b.CategoryPrefix))
} else {
tags = append(tags, label)
}
}
}
dateUpdated, err := time.Parse(time.RFC3339, rfcDateUpdated)
if err != nil {
return PostData{}, err
Expand Down Expand Up @@ -173,6 +200,8 @@ func (b Blogger) Pull(options PushPullOptions) (PostData, error) {
Date: date,
DateUpdated: dateUpdated,
Description: postDescription,
Categories: categories,
Tags: tags,
CanonicalUrl: canonicalUrl,
}, nil

Expand Down
38 changes: 24 additions & 14 deletions internal/platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ type WatchableSource interface {
}

type PushPullOptions struct {
AccessToken string
BlogId string
PostUrl string
Filepath string
RefreshToken string
ClientId string
ClientSecret string
LlmProvider string
LlmBaseUrl string
LlmApiKey string
LlmModel string
CategoryPrefix string
AccessToken string
BlogId string
PostUrl string
Filepath string
RefreshToken string
ClientId string
ClientSecret string
LlmProvider string
LlmBaseUrl string
LlmApiKey string
LlmModel string
}

type PostData struct {
Expand All @@ -50,6 +49,8 @@ type PostData struct {
DateUpdated time.Time
// TODO: Add frontmatter descriptions
Description string
Categories []string
Tags []string
// Other fields that are probably needed are canonical URL, publish date, and description
CanonicalUrl string
}
Expand All @@ -63,8 +64,9 @@ type PostData struct {
// }

type Blogger struct {
Name string
BlogUrl string
Name string
BlogUrl string
CategoryPrefix string
// https://developers.google.com/blogger/docs/3.0/reference/posts/delete
Overwrite bool
GenerateLlmDescriptions bool
Expand Down Expand Up @@ -133,11 +135,19 @@ func CreateSource(sourceMap map[string]interface{}) (Source, error) {
if !ok || blogUrl == "" {
return nil, fmt.Errorf("blog_url is required for blogger")
}
// Check if category_prefix is set, if not, set it to null and move on
// If the value is not a string, it will be set to "category::"
categoryPrefix, ok := sourceMap["category_prefix"].(string)
if !ok || categoryPrefix == "" {
log.Warn("category_prefix is not a string or is empty. Using default", "default", "category::")
categoryPrefix = "category::"
}
generateLlmDescriptions, _ := sourceMap["generate_llm_descriptions"].(bool)
return &Blogger{
Name: name,
BlogUrl: blogUrl,
GenerateLlmDescriptions: generateLlmDescriptions,
CategoryPrefix: categoryPrefix,
}, nil
case "markdown":
// If the content_dir is not set, set it to null as its not required
Expand Down

0 comments on commit d9339c9

Please sign in to comment.