2

I want to get data from a text file in someone's github repository. Can I do this using nodejs (like with the help of octokit.js by api calling).

3 Answers 3

5

You could try using node-fetch and get the text from the raw.

For an example:

const fetch = require('node-fetch');

const getNames = () => {
  fetch('https://raw.githubusercontent.com/jeanphorn/wordlist/master/usernames.txt')
    .then(res => res.text()).then(data => {
      console.log(data);
    }).catch(err => console.log('fetch error', err));
};

Or you can try it this way.

const fetch = require('node-fetch');

const getNames = async() => {
  try {
    const names = await fetch('https://raw.githubusercontent.com/jeanphorn/wordlist/master/usernames.txt');
    const textData = await names.text();
    return textData;
  } catch (err) {
    console.log('fetch error', err);
  }
};

(async () => {
  const getText = await getNames();
  console.log(getText)
})();

If you are not sure on how to get the raw link. Go to the github project and click on the text file you want to get the text from And than there should be a button top right of the text named RAW.

Sign up to request clarification or add additional context in comments.

Comments

3

With Octokit you can get the content of a file using this:

octokit.rest.repos.getContent({
 owner,
 repo,
 path,
});

Reference: https://octokit.github.io/rest.js/v18#repos-get-contents

Comments

1

with superface sdk it is super easy to read the text file content , not only from github but also from bitbucket and gitlab as well. for example lets retrieve the README.md file from https://github.com/superfaceai/one-sdk-js

npm install @superfaceai/one-sdk

npx @superfaceai/cli install vcs/single-file-content

const { SuperfaceClient } = require('@superfaceai/one-sdk');

const sdk = new SuperfaceClient();

async function run() {
  // Load the installed profile
  const profile = await sdk.getProfile('vcs/single-file-content');

  // Use the profile
  const result = await profile
    .getUseCase('SingleFileContent')
    .perform({
      owner: 'superfaceai',
      repo: 'one-sdk-js',
      path: '/README.md',
      ref: 'c5e4d76'
    });

  return result.unwrap();
}

run();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.