【问题标题】:GitHub Repository Redirect and trying to find it through the GitHub Search APIGitHub Repository Redirect 并尝试通过 GitHub Search API 找到它
【发布时间】:2017-02-12 14:25:37
【问题描述】:

我有一个 GitHub 存储库的链接,我正在使用 github3 和 Python 来尝试搜索它。

以这个链接为例:

https://github.com/GabrielGrimberg/OOP-Assignment1-UI

如果你去它,你会看到它重定向到

https://github.com/GabrielGrimberg/RuneScape-UI

因此,我不知道如何构建一个可以找到这个特定 repo 的搜索查询。

我试过了:

GabrielGrimberg/OOP-Assignment1-UI in:url
GabrielGrimberg/OOP-Assignment1-UI
GabrielGrimberg/OOP-Assignment1-UI in:full_name

【问题讨论】:

标签: python-3.x api github


【解决方案1】:

根据Github blog,如果repo被重命名,旧地址将重定向到新地址!

我们很高兴地宣布,从今天开始,在这种情况下,我们将自动将所有对以前存储库位置的请求重定向到他们的新家。你没有什么特别的事情要做。只需重命名,剩下的交给我们。

此外,您可以检查 Gabriel Grimberg 没有任何名为“OOP-Assignment1-UI”的存储库。

正确答案:

如果我们可以先检查 repo 详细信息以确保它存在/它已移动到哪里! 查看以下查询:

curl -i https://github.com/GabrielGrimberg/OOP-Assignment1-UI

您可以从标题中获取它移动的网址

HTTP/1.1 301 Moved Permanently
Server: GitHub.com
Date: Sun, 12 Feb 2017 18:19:25 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Status: 301 Moved Permanently
Cache-Control: no-cache
Vary: X-PJAX
Location: https://github.com/GabrielGrimberg/RuneScape-UI
X-UA-Compatible: IE=Edge,chrome=1

如果仓库已经存在,它会给你内容而不是标题! 例如,试试这个:

curl -i https://github.com/GabrielGrimberg/RuneScape-UI

【讨论】:

  • 我明白这一点,但是如何通过对 GitHub API 的查询找到新的存储库?
  • @Majiick 没有查询会找到“OOP-Assignment1-UI”。它总是会找到/重定向到新的“RuneScape-UI”。您是否尝试查询查找新存储库或什么?请澄清!
  • 我正在使用 GitHub API v3 尝试从用户 GabrielGrimberg 那里查询“OOP-Assignment1-UI”存储库。显然,它不再存在,因此查询不返回任何结果。但是,我知道它会重定向到新的仓库。那么,如何使用旧名称构造查询以找到新的 repo?
【解决方案2】:

如果第一次搜索没有结果,基本上你需要自己提出请求并检查重定向。

def get_redirection(full_name):
    try:
        json_object = json.loads(urllib.request.urlopen('https://api.github.com/repos/{0}'.format(full_name)).read().decode('utf-8'))
    except urllib.error.HTTPError:
        return None

    return json_object["full_name"]  # Will return the new full-name of the project

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 2016-06-03
    • 2020-01-08
    • 1970-01-01
    • 2022-11-10
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    相关资源
    最近更新 更多