3

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

Cody
  • 329
  • 4
  • 16

3 Answers3

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.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
8

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

Ratto
  • 137
  • 1
  • 8
  • 2
    This is another great idea, thanks! Then I assume you can parse the HTML or Markdown after getting the raw data. – Cody Sep 22 '21 at 17:22
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.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93