From 3106ddc7968e2956cddb6dc00a98114317e6aa2c Mon Sep 17 00:00:00 2001 From: Andreas Mieke Date: Wed, 15 Jan 2020 23:10:20 +0100 Subject: [PATCH] WIP --- config/config.go | 7 +++ folder/main.go | 7 +++ instagram/main.go | 12 +++-- snapchat/main.go | 7 +++ tumblr/api.go | 115 ++++++++++++++++++++++++++++++++++++++++++++++ tumblr/http.go | 34 ++++++++++++++ 6 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 tumblr/api.go create mode 100644 tumblr/http.go diff --git a/config/config.go b/config/config.go index 98df821..3057256 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ type Config struct { Snapchat Snapchat Twitter Twitter Instagram Instagram + Tumblr Tumblr Folder Folder } @@ -26,6 +27,12 @@ type Folder struct { Folder string } +// Tumblr defines fields for Tumblr configuration +type Tumblr struct { + Tag string + APIKey string +} + // Instagram defines fields for Instagram configuration type Instagram struct { Tag string diff --git a/folder/main.go b/folder/main.go index 3596ba2..8d5bcb2 100644 --- a/folder/main.go +++ b/folder/main.go @@ -15,8 +15,14 @@ import ( "git.1750studios.com/AniNite/SocialDragon/database" ) +var jobRunning bool + // LoadNewFolders loads the content of configured folder to database func LoadNewFolders() { + if jobRunning { + return + } + jobRunning = true log.Printf("Loading new folders...") files, _ := ioutil.ReadDir(config.C.Folder.Folder) for _, f := range files { @@ -44,6 +50,7 @@ func LoadNewFolders() { os.Remove(path.Join(config.C.Folder.Folder, f.Name())) log.Printf("Found picture %s", f.Name()) } + jobRunning = false } // ImageNameGenerator generates media paths diff --git a/instagram/main.go b/instagram/main.go index e19e2da..91cd48f 100644 --- a/instagram/main.go +++ b/instagram/main.go @@ -7,9 +7,14 @@ import ( ) var running bool +var jobRunning bool // LoadNewInstas gets new media from Instagram func LoadNewInstas() { + if jobRunning { + return + } + jobRunning = true log.Printf("Loading new Instas...") var count int if running { @@ -31,18 +36,19 @@ func LoadNewInstas() { } } log.Printf("Finished looking for new Instas.") + jobRunning = false } func iterList(list *ListResponse) string { var token string for _, image := range list.Tag.Media.Nodes { var count int - if database.Db.Model(database.Item{}).Where("original_id = ?", image.Code).Count(&count); count > 0 { + token = list.Tag.Media.PageInfo.EndCursor + post, err := LoadPost(image.Code) + if database.Db.Model(database.Item{}).Where("original_id = ?", post.Graphql.ShortcodeMedia.Shortcode).Count(&count); count > 0 { token = "" continue } - token = list.Tag.Media.PageInfo.EndCursor - post, err := LoadPost(image.Code) if err != nil { log.Printf("Can't load Instagram post %s: %+v", image.Code, err) continue diff --git a/snapchat/main.go b/snapchat/main.go index 5f5fe91..4e2678c 100644 --- a/snapchat/main.go +++ b/snapchat/main.go @@ -6,8 +6,14 @@ import ( "git.1750studios.com/AniNite/SocialDragon/database" ) +var jobRunning bool + // LoadNewSnaps loads all new snaps from snapchat func LoadNewSnaps() { + if jobRunning { + return + } + jobRunning = true log.Print("Loading new Snaps...") cons, err := GetConversations() if err != nil { @@ -55,4 +61,5 @@ func LoadNewSnaps() { } } log.Printf("Finished looking for new snaps.") + jobRunning = false } diff --git a/tumblr/api.go b/tumblr/api.go new file mode 100644 index 0000000..8fc36fe --- /dev/null +++ b/tumblr/api.go @@ -0,0 +1,115 @@ +package tumblr + +// ResponseT defines a Rumblr response +type ResponseT struct { + Meta struct { + Status int `json:"status"` + Msg string `json:"msg"` + } `json:"meta"` + Response []struct { + BlogName string `json:"blog_name"` + ID int64 `json:"id"` + PostURL string `json:"post_url"` + Slug string `json:"slug"` + Type string `json:"type"` + Date string `json:"date"` + Timestamp int `json:"timestamp"` + State string `json:"state"` + Format string `json:"format"` + ReblogKey string `json:"reblog_key"` + Tags []string `json:"tags"` + ShortURL string `json:"short_url"` + Summary string `json:"summary"` + RecommendedSource interface{} `json:"recommended_source"` + RecommendedColor interface{} `json:"recommended_color"` + NoteCount int `json:"note_count"` + Caption string `json:"caption,omitempty"` + Reblog struct { + TreeHTML string `json:"tree_html"` + Comment string `json:"comment"` + } `json:"reblog"` + Trail []struct { + Blog struct { + Name string `json:"name"` + Active bool `json:"active"` + Theme struct { + AvatarShape string `json:"avatar_shape"` + BackgroundColor string `json:"background_color"` + BodyFont string `json:"body_font"` + HeaderBounds int `json:"header_bounds"` + HeaderImage string `json:"header_image"` + HeaderImageFocused string `json:"header_image_focused"` + HeaderImageScaled string `json:"header_image_scaled"` + HeaderStretch bool `json:"header_stretch"` + LinkColor string `json:"link_color"` + ShowAvatar bool `json:"show_avatar"` + ShowDescription bool `json:"show_description"` + ShowHeaderImage bool `json:"show_header_image"` + ShowTitle bool `json:"show_title"` + TitleColor string `json:"title_color"` + TitleFont string `json:"title_font"` + TitleFontWeight string `json:"title_font_weight"` + } `json:"theme"` + ShareLikes bool `json:"share_likes"` + ShareFollowing bool `json:"share_following"` + CanBeFollowed bool `json:"can_be_followed"` + } `json:"blog"` + Post struct { + ID string `json:"id"` + } `json:"post"` + ContentRaw string `json:"content_raw"` + Content string `json:"content"` + IsCurrentItem bool `json:"is_current_item"` + IsRootItem bool `json:"is_root_item"` + } `json:"trail"` + PhotosetLayout string `json:"photoset_layout,omitempty"` + Photos []struct { + Caption string `json:"caption"` + AltSizes []struct { + URL string `json:"url"` + Width int `json:"width"` + Height int `json:"height"` + } `json:"alt_sizes"` + OriginalSize struct { + URL string `json:"url"` + Width int `json:"width"` + Height int `json:"height"` + } `json:"original_size"` + Exif struct { + Camera string `json:"Camera"` + ISO int `json:"ISO"` + Aperture string `json:"Aperture"` + Exposure string `json:"Exposure"` + FocalLength string `json:"FocalLength"` + } `json:"exif"` + } `json:"photos,omitempty"` + CanLike bool `json:"can_like"` + CanReblog bool `json:"can_reblog"` + CanSendInMessage bool `json:"can_send_in_message"` + CanReply bool `json:"can_reply"` + DisplayAvatar bool `json:"display_avatar"` + LinkURL string `json:"link_url,omitempty"` + ImagePermalink string `json:"image_permalink,omitempty"` + Title string `json:"title,omitempty"` + Body string `json:"body,omitempty"` + PermalinkURL string `json:"permalink_url,omitempty"` + HTML5Capable bool `json:"html5_capable,omitempty"` + Video struct { + Youtube struct { + VideoID string `json:"video_id"` + Width int `json:"width"` + Height int `json:"height"` + } `json:"youtube"` + } `json:"video,omitempty"` + ThumbnailURL string `json:"thumbnail_url,omitempty"` + ThumbnailWidth int `json:"thumbnail_width,omitempty"` + ThumbnailHeight int `json:"thumbnail_height,omitempty"` + Player []struct { + Width int `json:"width"` + EmbedCode string `json:"embed_code"` + } `json:"player,omitempty"` + VideoType string `json:"video_type,omitempty"` + VideoURL string `json:"video_url,omitempty"` + Duration int `json:"duration,omitempty"` + } `json:"response"` +} diff --git a/tumblr/http.go b/tumblr/http.go new file mode 100644 index 0000000..cf797b2 --- /dev/null +++ b/tumblr/http.go @@ -0,0 +1,34 @@ +package tumblr + +import ( + "errors" + "log" + "net/http" + "strconv" +) + +var sem = make(chan byte, 2) +var client = &http.Client{} + +// GetHTTPResource gets http resources with Tumblr headers +func GetHTTPResource(u string) (*http.Response, error) { + sem <- 1 + req, err := http.NewRequest("GET", u, nil) + if err != nil { + log.Fatalf("FAT HTTP - Failed to create new Request: %+v", err) + <-sem + return nil, err + } + res, err := client.Do(req) + if err != nil { + <-sem + return nil, err + } + if res.StatusCode != 200 { + res.Body.Close() + <-sem + return nil, errors.New(strconv.Itoa(res.StatusCode)) + } + <-sem + return res, nil +}