4

Recently, I started experimenting with the GitHub API getting specific data from public repos. Long story short, I want to grab specific parts of the README.md file from a repo.

For example, the master branch from Facebook's react repository I want to grab the text under the Documentation header the GitHub API. Is this possible? Any methods of achieving this are welcome. Thank you!

API : React README.md API Data

Public Github URL: React public repo

0

3 Answers 3

12

There is no way to do this with the API, but one easy way is with sed; try this from a Linux command line:

curl https://raw.githubusercontent.com/facebook/react/master/README.md | \
    sed -n '/## Documentation/,/##/p'

This will return everything between the Documentation header and the next one.

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

1 Comment

Perfection! Thank you, using that works just as well 👍😁
9

There is a very awesome way to use any GitHub repository MARKDOWN.md file with the use of API.

https://raw.githubusercontent.com/{owner}/{repo}/{branch}/README.md

the above API returns everything in your README.md file in raw MarkDown format. api-use-picture

1 Comment

This is another great idea, thanks! Then I assume you can parse the HTML or Markdown after getting the raw data.
0

Fetching content from the repository could be done like this:

curl -L \
-H 'Accept: application/vnd.github+json' \
-H 'Authorization: Bearer <TOKEN>' \
-H 'X-Github-Api-Version: 2022-11-28' \
https://api.github.com/repos/<OWNER>/<REPO>/contents/README.md

The respons will be an object and the actual data is held base64 encoded under a key named content. Here is an example using php:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/<OWNER>/<REPO>/contents/README.md");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Accept: application/vnd.github+json',
  'X-Github-Api-Version: 2022-11-28',
  'Authorization: Bearer <TOKEN>',
]);
curl_setopt($ch, CURLOPT_USERAGENT, 'my-user-agent');
$response = curl_exec($ch);
$response = \json_decode($response);
$readmeContent = base64_decode($response->content);

The above assumes that this is a private repository, so if that is the case you will need to generate a new private access token and set the correct header. Of course you will also have to change the OWNER and REPO in the above example.

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.