Instagram Post Code

 avatar
user_5561637
javascript
a year ago
5.4 kB
3
Indexable
Never
    prepare_post_object: function (post, ig_followers, locations, is_single_post_obj = false) {
        try {
            let output = {};

            output.id = post.id; // post ID
            output.user_id = post.owner.id; // IG ID
            output.type = (post.__typename === "GraphVideo") ? "video" : ((post.__typename === "GraphImage") ? 'image' : 'carousel'); // post type
            output.created_time = post.taken_at_timestamp; // post unixtime
            output.created_date = moment.unix(post.taken_at_timestamp).format("YYYY-MM-DD"); // post date
            output.link = post.shortcode ? "https://www.instagram.com/p/" + post.shortcode : null; // generate post link using shortcode
            output.caption = (post.edge_media_to_caption && post.edge_media_to_caption.edges && post.edge_media_to_caption.edges.length > 0 && post.edge_media_to_caption.edges[0].node && post.edge_media_to_caption.edges[0].node.text) ? post.edge_media_to_caption.edges[0].node.text : null; //post caption
            output.videos_standard_resolution_url = (post.video_url) ? post.video_url : null; // video url
            output.picture_analysis = (post.accessibility_caption) ? post.accessibility_caption : null; // picture analysis
            output.is_ad = (post.is_ad) ? post.is_ad : null; // is promoted post
            // pick imageUrl and likes and comments according to post object...
            output.likes_count = this.extract_ig_post_likes(post, is_single_post_obj);
            output.view_count = post.video_view_count || 0;
            output.images_standard_resolution_url = this.extract_ig_post_image(post, is_single_post_obj);
            output.comments_count = this.extract_ig_post_comments(post, is_single_post_obj);

            //add location information...
            if (post.location && Object.keys(post.location).length > 0) {
                // add info in the main param...
                output.location = post.location;
                // add location ID in the main param...
                output.location_id = post.location.id;
                // add location info into the array...
                locations.push([post.location.id, post.location.name, post.location.slug, post.location.has_public_page]);
                // add location into caption...
                output.caption += ` location - ${post.location.name}`;
            }//EOI
            //add sponsered users in caption...
            if (post.hasOwnProperty('edge_media_to_sponsor_user') && post.edge_media_to_sponsor_user.edges && post.edge_media_to_sponsor_user.edges.length > 0) {
                let sponsered_string = "";
                // run loop through items...
                for (let user of post.edge_media_to_sponsor_user.edges) {
                    sponsered_string += " @" + user.node.sponsor.username;
                }//EOL
                // add this to caption...
                output.caption += sponsered_string;
                // add this to main array...
                output.sponsored_by = sponsered_string.trim();
            }//EOI
            //add taged users in caption...
            if (post.hasOwnProperty('edge_media_to_tagged_user') && post.edge_media_to_tagged_user.edges.length > 0) {
                // run loop through items...
                for (let user of post.edge_media_to_tagged_user.edges) {
                    output.caption += " @" + user.node.user.username;
                }
            }//EOI
            // extract tags from caption...
            if (output.caption) {
                output.name_tags = output.caption.match(/@[a-zA-Z0-9._]+/g);
                if (output.name_tags) {
                    output.name_tags = _.map(output.name_tags, function (name_tag) {
                        return _.trim(name_tag.trim(), '@');
                    });
                }
                // calculate #tags...
                output.tags = output.caption.match(/#[a-zA-Z0-9._]+/g);
                if (output.tags) {
                    output.tags = _.map(output.tags, function (tag) {
                        return _.trim(tag.trim(), '#');
                    });
                }
            }
            output.interactions = (output.comments_count + output.likes_count); // calculate interaction
            // engagement rate
            let engagement_rate = (output.interactions / ((ig_followers && !isNaN(ig_followers)) ? ig_followers : 0)) * 100;
            // we need to check and then assign it to save in db.
            output.engagement_rate = (isNaN(engagement_rate) || engagement_rate === Infinity) ? null : engagement_rate;
            // Count media value for post
            output.media_value = monitor_worker.calculate_media_value(output.interactions, monitor_worker.calculate_post_impressions(ig_followers, 'instagram'), 'instagram');

            //carrousel images
            output.carrousel_image = [];
            if(post.hasOwnProperty('edge_sidecar_to_children') && post.edge_sidecar_to_children && 
            post.edge_sidecar_to_children.edges && _.isArray(post.edge_sidecar_to_children.edges) &&
            _.size(post.edge_sidecar_to_children.edges) > 0){
                output.carrousel_image = post.edge_sidecar_to_children.edges   
            }
            return output;
        } catch (error) {
            throw error;
        }
    },