我正在关注 Twitter 的开发人员,并希望对他们的过滤器实时 API 进行非常基本的调用。下面的代码几乎让我到达那里:
curl -s -u twitterusername:twitterpassword https://stream.twitter.com/1/statuses/sample.json -o "somefile.txt"
Run Code Online (Sandbox Code Playgroud)
我希望动态命名文件,以便捕获数据的每小时日志。
编辑:我希望这个命令保持打开状态,并且我收到的数据是连续的,这是毫无价值的。我希望每小时将输出重定向到不同的文件。
我对命令行和 ubuntu 完全陌生,所以我什至不知道从哪里开始。任何帮助都感激不尽。
curl -s -u twitterusername:twitterpassword https://stream.twitter.com/1/statuses/sample.json -o "somefile $(date + format).txt"
Run Code Online (Sandbox Code Playgroud)
其中,format可以是以下任何一项:
%a : Abbreviated weekday name (Sun..Sat)
%b : Abbreviated month name (Jan..Dec)
%B : Full month name, variable length (January..December)
%d : Day of month (01..31)
%e : Day of month, blank padded ( 1..31)
%m : Month (01..12)
%Y : Year
%d : Day of month (e.g, 01)
%H : 24 hour format (00..23)
%I : 12 hour format (01..12)
%M : Minutes of the current time (00...59)
%j : day of year (001..366)
%D : date; same as %m/%d/%y
%F : full date; same as %Y-%m-%d
Run Code Online (Sandbox Code Playgroud)
因此,对于您来说,这将保存文件并动态添加当前时间的小时 (%H) 和分钟 (%M)
curl -s -u twitterusername:twitterpassword https://stream.twitter.com/1/statuses/sample.json -o "somefile $(date +\"%H:%M\").txt"
Run Code Online (Sandbox Code Playgroud)
因为您想要curl获取 1 小时的数据并将该数据保存到文件中,您至少需要使用一个小脚本来恢复操作,这将完成这项工作:
#! /bin/bash
while true; do
curl -s -m 3600 -u twitterusername:twitterpassword https://stream.twitter.com/1/statuses/sample.json -o "somefile $(date +%H:%M).txt"
done
Run Code Online (Sandbox Code Playgroud)
当你让脚本运行时,它会执行命令,每 3600 秒(1 小时,-m 3600参数)curl 将关闭,命令将被执行。
请注意,这不仅会切断流,它实际上会关闭 curl 并重新打开它,不要认为在 curl 运行时切断流是可能的。
您需要在某处使用相同的脚本,即在终端上使用它之前~/curl_script.sh使其可执行chmod 755 ~/curl_script.sh,以使用它移动保存脚本的文件夹并键入./curl_script.sh.
要中断脚本,请按Ctrl+ c。
如果您中断脚本并在同一分钟恢复它,默认情况下它会覆盖之前收集的数据,所以要小心。
如果您想对脚本进行其他修改,请告诉我。对于更多 curl 参数,我建议阅读 curl 手册页(man curl在终端上)。
玩得开心。