Github - 如何通过API搜索代码片段?

3
我知道可以使用Github API通过各种标准 搜索存储库,但如何搜索单个要点或找到其ID?
理想情况下,我想使用用户文件名,但它也可能是内容主体内的哈希值:
https://gist.github.com/search?q=sadjghasghdhjasgdfhs

1
具体来说,您想使用什么标准来搜索Gist? - Chris
2
GitHub搜索API(https://developer.github.com/v3/search/)目前不支持搜索代码片段的功能。 - jasonrudolph
2
非常希望GitHub能够允许搜索代码片段。与此同时,似乎只有两个选项:1)通过API索引公共代码片段并搜索您的索引;2)使用https://gist.github.com/search?q=foo执行查询并抓取结果。 - glortho
有没有人愿意承担维护公共Gist索引并将其包装成简单搜索API的任务呢? :-) - glortho
是的,先生 :) 通过curl请求使用爬虫方法搜索私有gists也是可能的。 - RafaSashi
显示剩余2条评论
2个回答

2
我刚才发现可以搜索指定的用户和/或文件名。我查看了github的高级搜索,其中使用了"user:some-user-name"和"path:some-file-name"。这些标准也适用于gist搜索条款。
例如:https://gist.github.com/search?q=user%3Amanuelvanrijn+path%3Aguid.js 希望这能帮到您,直到gists有一个可用的搜索api。

直到有可用的搜索API为止,将示例保留为1。 - RafaSashi
在这种情况下,有没有好的方法可以循环遍历所有返回的页面? - tomohulk

0

在GitHub社区的评论中给出的顶级答案不再适用。我从archive.org中提取了Ruby示例,如下所示:

module GistSearch
  class << self
    def search_public_gists(query:, page_limit: 10)
      docs = []
      base_url = "https://gist.github.com"
      next_path = "/search?q=#{query}"

      page_limit.times do
        url = "#{base_url}#{next_path}"
        html = fetch_cached(url)

        doc = Nokogiri(html)
        docs << doc

        if !doc.search("a[rel=next]").empty?
          next_path = doc.search("a[rel=next]").attr("href").value
        else
          break
        end
      end

      docs
    end

    def build_items(queries:)
      queries.flat_map do |query|
        search_public_gists(query: query).flat_map do |doc|
          doc.
            search(".gist-snippet").
            map do |gist_snippet|
              Item.new(
                creator: gist_snippet.search(".creator a:first").text,
                title: gist_snippet.search(".creator a").map(&:text).join(" / ") + " (Matched query: #{query.inspect})",
                link: gist_snippet.search(".link-overlay").attr("href").value,
                guid: gist_snippet.search(".link-overlay").attr("href").value,
                pub_date: Time.iso8601(gist_snippet.search("time-ago").attr("datetime").value),
                content: gist_snippet.search(".js-file-line").map(&:text).join("\n"),
              )
            end
        end
      end
    end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接