~romuloalves/get-instagram-feed

ref: 72bfe2905ec83ec7e479b55c07773b1606062f81 get-instagram-feed/src/get.ts -rw-r--r-- 1.2 KiB
72bfe290 — romuloalves Renames functions 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import getInstagramData from '@romuloalves/get-instagram-data';

const parse = (data: any): Array<Object> => {
  if (data == null || data.entry_data == null ||
    data.entry_data.ProfilePage == null || data.entry_data.ProfilePage.length === 0) {
    return [];
  }

  const [profilePage, ..._] = data.entry_data.ProfilePage;

  return profilePage.graphql.user.edge_owner_to_timeline_media.edges.map((edge: any) => {
    const { node: media } = edge;
    let caption = '';
    let likes = 0;

    if (media.edge_media_to_caption && media.edge_media_to_caption.edges && media.edge_media_to_caption.edges.length > 0) {
      caption = media.edge_media_to_caption.edges[0].node.text;
    }

    if (media.edge_media_preview_like && media.edge_media_preview_like.count) {
      likes = media.edge_media_preview_like.count;
    }

    return {
      id: media.id,
      thumbnail: media.thumbnail_src,
      isVideo: media.is_video,
      likes,
      mediaUrl: media.display_url,
      dimensions: media.dimensions,
      takenAt: media.taken_at_timestamp,
      owner: media.owner,
      caption
    };
  });
}

export default async user => {
  const data = await getInstagramData(user);

  return parse(data);
};