尝试使用 curl 将日期特定的提交历史记录到原始 json 文本文件中。 Since、Until 和 Pagination 有问题吗?
Trying to get date specific commit history with curl into raw json text file. Issues with Since, Until, and Pagination?
所以我 运行 在我的终端里是这样的:
curl https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z
我想我得到了我想要的,但结果并不完整。我想我刚拿到第一页。我的下一步是将其备份到我能够提取的最早条目:
curl https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-01-26T21:07:19Z
我认为这会给我一组单独的结果(并手动连接)但它与第一行完全相同。添加 ?page=1,2,3... 也没有改变我的结果。
另外:我尝试将此请求定向到一个文本文件中,但当我打开它时它是空白的,而是在终端中输入命令 运行。
curl (url) > YearCommits.txt
我做错了什么?
在您的 url 周围添加引号,否则,您的 shell 将解释第一个 &
并将命令放在 &
运行 的左侧在后台(因此忽略您在其右侧添加的所有参数):
curl "https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z"
遍历分页参考this link
以下请求将检索第 2 页:
curl "https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z&page=2"
这是一个 bash 脚本,它使用 curl 和 jq
JSON 解析器将每个请求的 JSON 结果连接到一个文件 commit_list.json
:
#!/bin/bash
repo=d3/d3
output_file=commit_list.json
loop=0
index=1
tmp_file=tmpfile.txt
per_page=100
access_token=12345666799897950400303332323
rm -f $tmp_file
echo "[]" > $output_file
while [ "$loop" -ne 1 ]
do
data=$(curl -s "https://api.github.com/repos/$repo/commits?access_token=$access_token&since=2014-07-23T00:00:00Z&until=2017-07-23T23:59:59Z&page=$index&per_page=$per_page")
check_error=$(echo "$data" | jq 'type!="array" or length == 0')
if [ "$check_error" == "true" ]; then
exit 1
fi
if [ "$data" == "[]" ]; then
loop=1
else
echo "$data" > $tmp_file
concat=$(jq -s add $tmp_file $output_file)
echo "$concat" > $output_file
size=$(jq '. | length' $output_file)
echo "computed $index page - fetched total commit array size of : $size"
index=$((index+1))
fi
done
所以我 运行 在我的终端里是这样的:
curl https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z
我想我得到了我想要的,但结果并不完整。我想我刚拿到第一页。我的下一步是将其备份到我能够提取的最早条目:
curl https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-01-26T21:07:19Z
我认为这会给我一组单独的结果(并手动连接)但它与第一行完全相同。添加 ?page=1,2,3... 也没有改变我的结果。
另外:我尝试将此请求定向到一个文本文件中,但当我打开它时它是空白的,而是在终端中输入命令 运行。
curl (url) > YearCommits.txt
我做错了什么?
在您的 url 周围添加引号,否则,您的 shell 将解释第一个 &
并将命令放在 &
运行 的左侧在后台(因此忽略您在其右侧添加的所有参数):
curl "https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z"
遍历分页参考this link
以下请求将检索第 2 页:
curl "https://api.github.com/repos/d3/d3/commits?since=2016-07-23T00:00:00Z&until=2017-07-23T23:59:59Z&page=2"
这是一个 bash 脚本,它使用 curl 和 jq
JSON 解析器将每个请求的 JSON 结果连接到一个文件 commit_list.json
:
#!/bin/bash
repo=d3/d3
output_file=commit_list.json
loop=0
index=1
tmp_file=tmpfile.txt
per_page=100
access_token=12345666799897950400303332323
rm -f $tmp_file
echo "[]" > $output_file
while [ "$loop" -ne 1 ]
do
data=$(curl -s "https://api.github.com/repos/$repo/commits?access_token=$access_token&since=2014-07-23T00:00:00Z&until=2017-07-23T23:59:59Z&page=$index&per_page=$per_page")
check_error=$(echo "$data" | jq 'type!="array" or length == 0')
if [ "$check_error" == "true" ]; then
exit 1
fi
if [ "$data" == "[]" ]; then
loop=1
else
echo "$data" > $tmp_file
concat=$(jq -s add $tmp_file $output_file)
echo "$concat" > $output_file
size=$(jq '. | length' $output_file)
echo "computed $index page - fetched total commit array size of : $size"
index=$((index+1))
fi
done