Skip to content

Commit

Permalink
Fix/remove type feature (#238)
Browse files Browse the repository at this point in the history
* remove type references

* keep basic hashtags
  • Loading branch information
graysonhicks committed Nov 7, 2022
1 parent e69939c commit f6a837a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 300 deletions.
84 changes: 1 addition & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,17 @@

</div>

Source plugin for sourcing data from Instagram. There are four ways to get information from instagram:
Source plugin for sourcing data from Instagram. There are two ways to get information from instagram:

- scraping the posts of an Instagram account. It can only get last 50 photos.
- scraping a hashtag page.
- scraping a user profile's informations.
- querying the Instagram Graph Api using a provided `access_token`

# Table of Contents

- [Install](#install)
- [How to use](#how-to-use)
- [Public scraping for posts](#public-scraping-for-posts)
- [Public scraping for a user's profile](#public-scraping-for-a-users-profile)
- [Graph API](#graph-api)
- [Hashtag scraping](#hashtag-scraping)
- [How to query](#how-to-query)
- [Posts](#posts)
- [User profile information](#user-profile-information)
Expand Down Expand Up @@ -56,29 +52,6 @@ plugins: [
},
]
```

### Public scraping for a user's profile

** Deprecated **

Due to instagram adding a login screen for scraping calls this is no longer working on Cloud builders.
I am currently researching a solution, ideas and PRs welcome.

If you want to source a user's profile from their username then you need the following:

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-instagram`,
options: {
type: `user-profile`,
username: `username`,
},
},
]
```

### Graph API

If you intend to use the Instagram Graph Api then you need to pass the instagram id and an access token
Expand Down Expand Up @@ -125,28 +98,6 @@ plugins: [

```

### Hashtag scraping

** Deprecated **

Due to instagram adding a login screen for scraping calls this is no longer working on Cloud builders.
I am currently researching a solution, ideas and PRs welcome.

If you want to source nodes from hashtags then you need the following:

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-instagram`,
options: {
type: `hashtag`,
hashtag: `snowing`,
},
},
]
```

## How to query

### Posts
Expand Down Expand Up @@ -219,39 +170,6 @@ query {
}
```

### User profile information

** Deprecated **

Due to instagram adding a login screen for scraping calls this is no longer working on Cloud builders.
I am currently researching a solution, ideas and PRs welcome.

Fields include:

- id
- username
- full_name
- biography
- edge_followed_by (followers)
- edge_follow (who the user follows)
- profile_pic_url
- profile_pic_url_hd

```graphql
query {
instaUserNode {
id
username
full_name
biography
edge_followed_by
edge_follow
profile_pic_url
profile_pic_url_hd
}
}
```

## Image processing

To use image processing you need gatsby-transformer-sharp, gatsby-plugin-sharp and their dependencies gatsby-image and gatsby-source-filesystem in your gatsby-config.js.
Expand Down
167 changes: 55 additions & 112 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,153 +1,96 @@
const _ = require(`lodash`)
const crypto = require(`crypto`)
const normalize = require(`./normalize`)
const _ = require(`lodash`);

const crypto = require(`crypto`);

const normalize = require(`./normalize`);

const {
apiInstagramPosts,
scrapingInstagramPosts,
apiInstagramHashtags,
scrapingInstagramHashtags,
scrapingInstagramUser,
} = require(`./instagram`)
} = require(`./instagram`);

const defaultOptions = {
type: `account`,
paginate: 100,
hashtags: false,
}
};

async function getInstagramPosts(options) {
let data

if (options.access_token && options.instagram_id) {
data = await apiInstagramPosts(options)
} else {
data = await scrapingInstagramPosts(options)
}

return data
}

async function getInstagramHashtags(options) {
let data
let data;

if (options.access_token && options.instagram_id) {
data = await apiInstagramHashtags(options)
data = await apiInstagramPosts(options);
} else {
data = await scrapingInstagramHashtags(options)
data = await scrapingInstagramPosts(options);
}

return data
}

async function getInstagramUser(options) {
const data = await scrapingInstagramUser(options)
return [data]
return data;
}

function createPostNode(datum, params) {
return {
type: params.type,
username:
params.type === `hashtag`
? params.hashtag
: datum.username || datum.owner.username || datum.owner.id,
username: datum.username || datum.owner.username || datum.owner.id,
id: datum.shortcode,
parent: `__SOURCE__`,
internal: {
type: `InstaNode`,
type: `InstaNode`
},
children: [],
likes:
_.get(datum, `edge_liked_by.count`) ||
_.get(datum, `edge_media_preview_like.count`) ||
datum.like_count,
caption:
_.get(datum, `edge_media_to_caption.edges[0].node.text`) || datum.caption,
likes: _.get(datum, `edge_liked_by.count`) || _.get(datum, `edge_media_preview_like.count`) || datum.like_count,
caption: _.get(datum, `edge_media_to_caption.edges[0].node.text`) || datum.caption,
thumbnails: datum.thumbnail_resources,
mediaType: datum.__typename || datum.media_type,
preview: datum.display_url || datum.thumbnail_url || datum.media_url,
original: datum.display_url || datum.media_url,
timestamp:
datum.taken_at_timestamp || new Date(datum.timestamp).getTime() / 1000,
timestamp: datum.taken_at_timestamp || new Date(datum.timestamp).getTime() / 1000,
dimensions: datum.dimensions,
comments:
_.get(datum, `edge_media_to_comment.count`) || datum.comments_count,
comments: _.get(datum, `edge_media_to_comment.count`) || datum.comments_count,
hashtags: datum.hashtags,
permalink: datum.permalink,
carouselImages: _.get(datum, `children.data`, []).map((imgObj) => {
carouselImages: _.get(datum, `children.data`, []).map(imgObj => {
return {
preview: imgObj.media_url,
...imgObj,
}
}),
}
}

function createUserNode(datum, params) {
return {
type: params.type,
id: datum.id,
full_name: datum.full_name,
biography: datum.biography,
edge_followed_by: datum.edge_followed_by,
edge_follow: datum.edge_follow,
profile_pic_url: datum.profile_pic_url,
profile_pic_url_hd: datum.profile_pic_url_hd,
username: datum.username,
internal: {
type: `InstaUserNode`,
},
}
...imgObj
};
})
};
}

function processDatum(datum, params) {
const node =
params.type === `user-profile`
? createUserNode(datum, params)
: createPostNode(datum, params)

// Get content digest of node. (Required field)
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(node))
.digest(`hex`)
node.internal.contentDigest = contentDigest
return node
const node = createPostNode(datum, params); // Get content digest of node. (Required field)

const contentDigest = crypto.createHash(`md5`).update(JSON.stringify(node)).digest(`hex`);
node.internal.contentDigest = contentDigest;
return node;
}

exports.sourceNodes = async (
{ actions, store, cache, createNodeId },
options
) => {
const { createNode, touchNode } = actions
const params = { ...defaultOptions, ...options }
let data
exports.sourceNodes = async ({
actions,
store,
cache,
createNodeId
}, options) => {
const {
createNode,
touchNode
} = actions;
const params = { ...defaultOptions,
...options
};
let data;

data = await getInstagramPosts(params);

if (params.type === `account`) {
data = await getInstagramPosts(params)
} else if (params.type === `hashtag`) {
data = await getInstagramHashtags(params)
} else if (params.type === `user-profile`) {
data = await getInstagramUser(params)
} else {
console.warn(`Unknown type for gatsby-source-instagram: ${params.type}`)
}

// Process data into nodes.
if (data) {
return Promise.all(
data.map(async (datum) => {
const res = await normalize.downloadMediaFile({
datum: processDatum(datum, params),
store,
cache,
createNode,
createNodeId,
touchNode,
})
createNode(res)
})
)
return Promise.all(data.map(async datum => {
const res = await normalize.downloadMediaFile({
datum: processDatum(datum, params),
store,
cache,
createNode,
createNodeId,
touchNode
});
createNode(res);
}));
}
}
};

0 comments on commit f6a837a

Please sign in to comment.