Search code examples
pythoncsvfilegithubrepository

Download Public Github repository using Python function


I have a CSV file with one column. This column consists of around 100 GitHub public repo addresses (for example, NCIP/c3pr-docs)

I want to know if there is any way to download all these 100 public repo inside my computer using Python. I don't want to use any command on the terminal, I need a function for it.

I use a very simple code to access to user and repo. Here it is:

import csv
import requests
#replace the name with your actual csv file name
file_name = "dataset.csv"
f = open(file_name)
csv_file = csv.reader(f)
second_column = [] #empty list to store second column values
for line in csv_file:
    if line[1] == "Java":
     second_column.append(line[0])
     print(line[0]) #index 1 for second column

So by doing this I read a CSV file and get access to the users and repo. I need a piece of code to help me download all these repo


Solution

  • Try this:

    import requests
    
    def download(user_and_repo, branch):
        URL = f"https://github.com/{user_and_repo}/archive/{branch}.tar.gz"
        response = requests.get(URL)
        open(f"{user_and_repo.split('/')[1]}.tar.gz", "wb").write(response.content)
    
    download("AmazingRise/hugo-theme-diary", "main")
    

    Tested under Python 3.9.