Instagram Profile

mail@pastecode.io avatar
unknown
javascript
a year ago
3.0 kB
1
Indexable
Never
format_user_data: async function (user_data, locations, profile) {
        try {
            let prepared_data = {
                "user": {},
                "daily_count": {},
                "posts": []
            };

            if (user_data && user_data.graphql && user_data.graphql.user && user_data.graphql.user.id) {
                let user = user_data.graphql.user;
                let profile_picture = await spaces.upload(user.profile_pic_url_hd || user.profile_pic_url, profile.profile_id || profile.insertId, ['profile_picture', 'instagram'], false, user);
                prepared_data.user.id = user.id;
                prepared_data.user.username = user.username;
                prepared_data.user.full_name = user.full_name;
                prepared_data.user.website = user.external_url || null;
                prepared_data.user.bio = user.biography || null;
                prepared_data.user.media = (user.edge_owner_to_timeline_media && user.edge_owner_to_timeline_media.count) ? user.edge_owner_to_timeline_media.count : 0;
                prepared_data.user.followed_by = (user.edge_followed_by && user.edge_followed_by.count) ? user.edge_followed_by.count : 0;
                prepared_data.user.follows = (user.edge_follow && user.edge_follow.count) ? user.edge_follow.count : 0;
                prepared_data.user.profile_picture = profile_picture;
                prepared_data.user.private = (user.is_private) ? 'Y' : 'N';
                prepared_data.user.is_business_account = (user.hasOwnProperty('is_business_account')) ? user.is_business_account : null;
                prepared_data.user.is_verified = (user.hasOwnProperty('is_verified')) ? user.is_verified : null;

                //Daily count
                prepared_data.daily_count = {}
                prepared_data.daily_count.instagram_id = user.id;
                prepared_data.daily_count.date = moment().format("YYYY-MM-DD");
                prepared_data.daily_count.media = prepared_data.user.media;
                prepared_data.daily_count.follows = prepared_data.user.follows;
                prepared_data.daily_count.followed_by = prepared_data.user.followed_by;
            }
            
            if (user_data && user_data.graphql && user_data.graphql.user && user_data.graphql.user.edge_owner_to_timeline_media && user_data.graphql.user.edge_owner_to_timeline_media.edges && user_data.graphql.user.edge_owner_to_timeline_media.edges.length > 0) {
                let posts = user_data.graphql.user.edge_owner_to_timeline_media.edges;
                // run loop through posts...
                for (let post of posts) {
                    post = post.node;
                    let post_data = ig_workers.posts.prepare_post_object(post, prepared_data.user.followed_by, locations);
                    // pushing it to main array...
                    prepared_data.posts.push(post_data);
                }//EOL

            }

            return prepared_data;
        } catch (err) {
            throw err;
        }
    },