Class: Feed2Gram::PublishesPosts
- Inherits:
-
Object
- Object
- Feed2Gram::PublishesPosts
- Defined in:
- lib/feed2gram/publishes_posts.rb
Constant Summary collapse
- SECONDS_PER_UPLOAD_CHECK =
ENV.fetch("SECONDS_PER_UPLOAD_CHECK") { 30 }.to_i
- MAX_UPLOAD_STATUS_CHECKS =
ENV.fetch("MAX_UPLOAD_STATUS_CHECKS") { 100 }.to_i
- RETRIES_AFTER_UPLOAD_TIMEOUT =
ENV.fetch("RETRIES_AFTER_UPLOAD_TIMEOUT") { 5 }.to_i
Instance Method Summary collapse
-
#create_carousel_container(post, media_containers, config) ⇒ Object
Pseudo-public “API mode”.
-
#create_carousel_media_container(media, config) ⇒ Object
Pseudo-public “API mode”.
-
#create_single_media_container(post, config) ⇒ Object
Pseudo-public “API mode”.
-
#get_media_permalink(media_id, config) ⇒ Object
Pseudo-public “API mode”.
- #publish(posts, config, options) ⇒ Object
-
#publish_container(container_id, config) ⇒ Object
Pseudo-public “API mode”.
-
#upload_finished?(container_id, config) ⇒ Boolean
Pseudo-public “API mode”.
Instance Method Details
#create_carousel_container(post, media_containers, config) ⇒ Object
Pseudo-public “API mode”
58 59 60 61 62 63 64 65 |
# File 'lib/feed2gram/publishes_posts.rb', line 58 def create_carousel_container(post, media_containers, config) Http.post("/#{config.instagram_id}/media", { caption: post., media_type: post.media_type, children: media_containers.join(","), access_token: config.access_token })[:id] end |
#create_carousel_media_container(media, config) ⇒ Object
Pseudo-public “API mode”
47 48 49 50 51 52 53 54 55 |
# File 'lib/feed2gram/publishes_posts.rb', line 47 def create_carousel_media_container(media, config) res = Http.post("/#{config.instagram_id}/media", { :media_type => media.media_type, :is_carousel_item => true, :access_token => config.access_token, media.video? ? :video_url : :image_url => media.url }.compact) res[:id] end |
#create_single_media_container(post, config) ⇒ Object
Pseudo-public “API mode”
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/feed2gram/publishes_posts.rb', line 35 def create_single_media_container(post, config) media = post.medias.first Http.post("/#{config.instagram_id}/media", { :media_type => post.media_type, :caption => post., :access_token => config.access_token, :cover_url => media.cover_url, media.video? ? :video_url : :image_url => media.url }.compact)[:id] end |
#get_media_permalink(media_id, config) ⇒ Object
Pseudo-public “API mode”
99 100 101 102 103 104 105 |
# File 'lib/feed2gram/publishes_posts.rb', line 99 def get_media_permalink(media_id, config) res = Http.get("/#{media_id}", { fields: "permalink", access_token: config.access_token }) res[:permalink] end |
#publish(posts, config, options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/feed2gram/publishes_posts.rb', line 10 def publish(posts, config, ) post_limit = .limit || posts.size puts "Publishing #{post_limit} posts to Instagram" if .verbose # reverse to post oldest first (most Atom feeds are reverse-chronological) posts.reverse.take(post_limit).map { |post| begin retry_if_upload_times_out(RETRIES_AFTER_UPLOAD_TIMEOUT, post, ) do if post.medias.size == 1 puts "Publishing #{post.media_type.downcase} for: #{post.url}" if .verbose publish_single_media(post, config, ) else puts "Publishing carousel for: #{post.url}" if .verbose publish_carousel(post, config, ) end end rescue => e warn "Failed to post #{post.url}: #{e.}" e.backtrace.join("\n").each_line { |line| warn line } Result.new(post: post, status: :failed) end } end |
#publish_container(container_id, config) ⇒ Object
Pseudo-public “API mode”
90 91 92 93 94 95 96 |
# File 'lib/feed2gram/publishes_posts.rb', line 90 def publish_container(container_id, config) res = Http.post("/#{config.instagram_id}/media_publish", { creation_id: container_id, access_token: config.access_token }) res[:id] end |
#upload_finished?(container_id, config) ⇒ Boolean
Pseudo-public “API mode”
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/feed2gram/publishes_posts.rb', line 68 def upload_finished?(container_id, config) res = Http.get("/#{container_id}", { fields: "status_code,status", access_token: config.access_token }) if res[:status_code] == "FINISHED" true elsif res[:status_code] == "IN_PROGRESS" false else raise <<~MSG Unexpected status code (#{res[:status_code]}) uploading container: #{container_id}" API sent back this: #{res[:status]} Error codes can be looked up here: https://developers.facebook.com/docs/instagram-platform/instagram-graph-api/reference/error-codes/ MSG end end |