Get content of random file for given language? #24597
-
I want to fetch the contents of a random file written in a given programming language to use for a typing game. Using JavaScript as an example, I’ve used the search API to find repositories containing JavaScript:
But I’m having trouble figuring out how to list a repo’s JavaScript files and fetch their contents. Is there a way to do this? Is there a better way to get the contents of a random JavaScript file (preferrably from a highly-starred repo)? Thanks for any help anyone can give! |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
how about this one ? https://docs.github.com/en/rest/repos/contents#get-repository-content do the search first then once you have a target file, read the content |
Beta Was this translation helpful? Give feedback.
-
Hi @jdevstatic that’s helpful, thank you! I’m still not sure how to find a file of a particular language in a repo. My search query above lists repos that have a file using the language somewhere, but I can’t figure out a way to list those files and their paths. |
Beta Was this translation helpful? Give feedback.
-
I think there is a similar question on how to find the list of supported language by GitHub and this is the particular repo, I think so, you can also ask them by raising an issue
GitHub - github/linguist: Language Savant. If your repository's language is...Language Savant. If your repository's language is being reported incorrectly, send us a pull request! - GitHub - github/linguist: Language Savant. If your repository's language is being rep... |
Beta Was this translation helpful? Give feedback.
-
I think there may be a miscommunication, I am not looking for a list of supported languages. What I want to do is:
I already know how to do step 1 using the search query in my original post. Your first comment showed me how to do step 3 (thanks again!). What I still don’t know is how to do step 2. |
Beta Was this translation helpful? Give feedback.
-
how about this one ?
Get all file names from a Github repo through the Github API
github, github-api
asked by
Anton Antonov
on 06:02PM - 29 Jul 14 UTC
it includes the file name extension, so you would know the language |
Beta Was this translation helpful? Give feedback.
-
Hey, thanks again for your help! I ended up using the search API to find the files as well. So my sequence is:
This way I don’t have to crawl the Git tree to find files and can keep API requests few. The search also handles finding files by language even when the files may have one of multiple extensions (JavaScript has many). One weird thing is that the Search API requires a string to search for, so I’ve just given it a space – that’s the |
Beta Was this translation helpful? Give feedback.
Hey, thanks again for your help! I ended up using the search API to find the files as well. So my sequence is:
api.github.com/search/repositories?q=language:${codeLanguage}&sort=stars&order=desc
api.github.com/search/code?q=%20+language:${codeLanguage}+repo:${repoName}
This way I don’t have to crawl the Git tree to find files and can keep API requests few. The search also handles finding files by language even when the files may have one of multiple extensions (JavaScript has many).
One weird thing is that the Search API requires a string to s…