从GitHub repo下载单个文件有哪些技巧?

我不想要显示原始文件的URL;对于二进制文件,什么都没有。

http://support.github.com/discussions/feature-requests/41-download-single-file

是否有可能将GitHub用作“下载服务器”?

如果我们决定切换到GoogleCode,是否提供了上述功能?

或者开源项目是否有免费托管和VCS?


当前回答

您可以使用V3API获取如下原始文件(需要OAuth令牌):

curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: 
application/vnd.github.v3.raw' -O -L 
https://api.github.com/repos/*owner*/*repo*/contents/*path*

所有这些都必须在一条线上进行。-O选项将文件保存在当前目录中。可以使用-o filename指定其他文件名。

要获取OAuth令牌,请遵循以下说明。

我也把这个写下来作为要点。

其他回答

我最近发现了一个名为gitzip的服务,它也是开源的:

站点-http://kinolien.github.io/gitzip/

回购-https://github.com/KinoLien/gitzip

访问上述站点,输入repo或目录URL,您可以下载单个文件或整个目录作为zip文件。

现在,可以使用以下google chrome扩展名下载存储库中的任何文件或任何特定文件夹:

github的GitZip:链接:https://chrome.google.com/webstore/detail/gitzip-for-github/ffabmkklhbepgcgfonabamgnfafbdlkn

用法:

在任何GitHub公共repo页面中。只需双击所需的项目。单击右下角的下载按钮。查看进度仪表板并等待浏览器触发器下载。获取ZIP文件。

或者试试这个

const https = require('https');
const fs = require('fs');
const DOMAIN = 'raw.githubusercontent.com';

function writeFile(data, fileName) {
  fs.appendFile(fileName, data.toString(), err => {
    if (err) {
      console.log('error in writing file', err);
    }
  });
}

function EOF(data) {
  console.log('EOF');
}

function getFileName(pathToFile) {
  var result = pathToFile.split('/');
  var splitLength = result.length;
  return result[splitLength - 1];
}
function getFile(branchName, username, repoName, ...pathToFile) {
  pathToFile.forEach(item => {
    const path = `/${username}/${repoName}/${branchName}/${item}`;
    const URL = `${DOMAIN}${path}`;
    const options = {
      hostname: DOMAIN,
      path: path
    };
    var fileName = getFileName(item);

    https
      .get(options, function(res) {
        console.log(res.statusCode);
        /* if file not found */
        if (res.statusCode === 404) {
          console.log('FILE NOT FOUND');
        } else {
          /* if file found */
          res.on('data', data => writeFile(data, fileName));
          res.on('end', data => EOF(data));
        }
      })
      .on('error', function(res) {
        console.log('error in reading URL');
      });
  });
}
getFile('master', 'bansalAyush', 'InstagramClone', '.babelrc', 'README.md');

请使用“{host}/{user}/}repo}/branch}/{file}”的模式。要获得具体示例,请在Python中执行以下操作:

import pandas as pd
host = 'raw.github.com'
user = 'fivethirtyeight'
repo = 'data'
branch = 'master'
file = 'births/US_births_2000-2014_SSA.csv'
url = f'https://{host}/{user}/{repo}/{branch}/{file}'
df = pd.read_csv(url,sep=',',header=0)
df.head()

您可以尝试github文件获取器,它是一个命令行工具,可以从github repo下载单个文件夹或文件。

想象一个真实的场景:您正在访问以下网页,并想单独下载异步子目录。

https://github.com/reduxjs/redux/tree/master/examples

很抱歉不能发布图片。

使用github文件获取器,您应该首先复制该页面的urlhttps://github.com/reduxjs/redux/tree/master/examples/async,然后在命令行中运行以下命令:

提取器--url=https://github.com/reduxjs/redux/tree/master/examples/async