Deal with hashtags

This commit is contained in:
Brian Buller 2020-04-07 15:43:17 -05:00
parent d269b6a500
commit 582b3ac10a
2 changed files with 35 additions and 4 deletions

View File

@ -148,3 +148,13 @@ func FileExists(path string) (bool, error) {
}
return true, err
}
func CreateDirIfNotExist(path string) error {
fmt.Println("Making directory", path)
exist, _ := FileExists(path)
if exist {
fmt.Println("Path Exists")
return nil
}
return os.MkdirAll(path, os.ModePerm)
}

29
main.go
View File

@ -75,18 +75,31 @@ func ProcessTimeline(httpClient *http.Client) error {
for _, t := range tweets {
for _, m := range t.Entities.Media {
filename := t.Text[:strings.LastIndex(t.Text, " ")]
filename = strings.ReplaceAll(filename, " ", "_") + ".jpg"
var filenamePts []string
txtPts := strings.Fields(t.Text)
var subFolder string
for _, v := range txtPts {
if strings.HasPrefix(v, "http") {
break
}
if v[0] == '#' {
if subFolder == "" {
subFolder = v[1:]
}
} else {
filenamePts = append(filenamePts, v)
}
}
filename := strings.Join(filenamePts, "_") + ".jpg"
create, err := t.CreatedAtTime()
if err != nil {
create = time.Now()
}
filename = create.Format("20060102T150405") + "_" + filename
filename = subFolder + "/" + create.Format("20060102T150405") + "_" + filename
if ImageNeedsDownload(filename) || appConfig.ForceDownload {
err = DownloadImage(httpClient, m.MediaURLHttps, filename)
if err != nil {
PrintIfVerbose("Error downloading image (", m.MediaURLHttps, ")", err.Error(), "\n")
}
}
}
@ -103,6 +116,14 @@ func ImageNeedsDownload(filename string) bool {
}
func DownloadImage(httpClient *http.Client, url, filename string) error {
pts := strings.FieldsFunc(filename, func(c rune) bool { return c == '/' })
if len(pts) > 1 {
err := CreateDirIfNotExist(appConfig.GetFilePath(pts[0]))
if err != nil {
fmt.Println(err.Error())
return err
}
}
fmt.Println("Downloading ", url, " -> ", filename, "...")
imgResp, err := httpClient.Get(url)
if err != nil {