Skip to content

Instantly share code, notes, and snippets.

@kbabioch
Created June 7, 2018 09:46
Show Gist options
  • Save kbabioch/94751f2c7b5005e2aa4c36aca1889dc0 to your computer and use it in GitHub Desktop.
Save kbabioch/94751f2c7b5005e2aa4c36aca1889dc0 to your computer and use it in GitHub Desktop.
Prototype of a hook for urlwatch to retrieve releases from GitHub via official API
# Copyright (c) 2018 Karol Babioch <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This is a very basic hook for urlwatch [1], that will retrieve
# information about releases from a GitHub repository. An effort is
# ongoing [2] to include this functionality into the upstream project.
#
# To monitor a repostory, add the following to your urls.yml (e.g. by
# invoking `urlwatch --edit`:
#
# ---
# kind: github
# repo: thp/urlwatch
# ---
#
# This will retrieve the release information via the official GitHub
# API [3].
#
# NOTE: The public API is rate limited. Without an authorization
# token you can only perform 60 requests per hour. You can create
# a token at [4]. This token only requires the `public_repo`
# permission. Put it into the `__token__` attribute.
# [1]: https://github.com/thp/urlwatch
# [2]: https://github.com/thp/urlwatch/issues/246
# [3]: https://developer.github.com/
# [4]: https://github.com/settings/tokens
import requests
from urlwatch import jobs
class GitHubJob(jobs.Job):
__API__ = 'https://api.github.com'
# FILL IN YOUR API token here
# -> https://github.com/settings/tokens
# only public_repo permission is required
__token__ = ''
__kind__ = 'github'
__required__ = ('repo',)
def get_location(self):
return '{} (GitHub)'.format(self.repo)
def retrieve(self, job_state):
headers = {}
if self.__token__:
headers['Authorization'] = 'token {}'.format(self.__token__)
r = requests.get('{}/repos/{}/releases'.format(self.__API__, self.repo), headers=headers)¬
r.raise_for_status()¬
return r.text¬
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment