Create a gist now

Instantly share code, notes, and snippets.

@livibetter /COPYING
Last active Mar 23, 2017

What would you like to do?
Listing new trending repos on GitHub

Listing new trending repo on GitHub

gh-trend.py uses GitHub Trend RSS as the data source and lists new trending repos. Each repo will only be listed once in output.

Usage

gh-trend.py [-h] [-j JSON] [-p {today,this-week,this-month} language [language ...]
  • -j is the saved JSON file, which is used to check if a repo has been listed before.
  • -p the trending period, one of today, this-week, or this-month.
  • language, the names of interested languages.

Examples:

$ gh-trend.py -p this-week all python bash
$ gh-trend.py -j $HOME/.gh-trend.json objective-c common-lisp csharp cpp

You may want to check the RSS URLs for correct language names.

Output looks like:

https://github.com/user/repo
Description
(language)

With cron

It could be a nice idea to use with cron, e.g.

@daily      /path/to/gh-trend.py -j /path/to/saved.json               all python bash >> /path/to/output.txt
@weekly     /path/to/gh-trend.py -j /path/to/saved.json -p this-week  all python bash >> /path/to/output.txt
@monthly    /path/to/gh-trend.py -j /path/to/saved.json -p this-month all python bash >> /path/to/output.txt

Your cron's syntax may differ, check with its manual pages.

Converting to HTML

The following AWK code converts the output into simple HTML code:

gh-trend.py [options] | awk "
/^https/ {
  print(\"<a href='\" \$0 \"'>\"\
        gensub(\"https://github.com/\", \"@\", \"\", \$0)\
        \"</a><br/>\");
  next;
}
/^.+$/ {
  print(\"<span>\" \$0 \"</span><br/>\");
  next;
}
/^$/ {
  print(\"<br/>\");
}
" > output.html

Output looks like:

@user/repo
Description
(language)

Where @user/repo is a hyperlink. Note that there is no specification of encoding since it's only a partial HTML, therefore some Unicode characters may not be displayed correct until manually setting the encoding to UTF-8 in web browser.

Or a more complete HTML output:

echo "<html><head><title>gh-trend.py" > output.html
echo -e '</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="background-color:#000;color:#aaa;font-family:Inconsolata;font-weight:bold">\n' >> output.html
gh-trend.py [options] | awk "
/^https/ {
  print(\"<a href='\" \$0 \"' style='color:lawngreen;text-decoration:none'>\"\
        gensub(\"https://github.com/\", \"@\", \"\", \$0)\
        \"</a><br/>\");
  next;
}
/^\(.*\)$/ {
  print(\"<span style='color:lightblue'>\" \$0 \"</span><br/>\");
  next;
}
/^.+$/ {
  print(\"<span style='color:lightgrey'>\" \$0 \"</span> \");
  next;
}
/^$/ {
  print(\"<br/>\");
}
" >> output.html
echo '</body></html>' >> output.html

The output will look like:

gh-trend html

Bugs and Suggestions

The script is very basic, not even any error checking or messages, like for bozo feeds or incorrect language names.

Feel free to comment below, or fork this Gist and notify me with your modifications.

Related Links

License

This project is licensed under the MIT License, see COPYING.

Copyright (c) 2013, 2015 Yu-Jie Lin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#!/usr/bin/env python3
# Listing new trending repo on GitHub
# Copyright (c) 2013, 2015 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Gist: https://gist.github.com/livibetter/7140262
# Blog: http://blog.yjl.im/2013/10/checking-new-trending-repos-on-github.html
import argparse
import json
import sys
from os import path
import feedparser as fp
fp.PREFERRED_XML_PARSERS.remove('drv_libxml2')
# default JSON file is in current working directory
DEFAULT_JSON = path.abspath(path.basename(sys.argv[0]) + '.json')
REPO_BASE = 'https://github.com/%s'
TREND_BASE = 'http://github-trends.ryotarai.info/rss/github_trends_%s_%s.rss'
def get_trend(lang, freq):
for repo in fp.parse(TREND_BASE % (lang, freq)).entries:
yield (repo.title.split(' ', 1)[0], # "(#n - lang - freq)" is stripped
repo.description or '') # "\n(lang)" is kept
def main():
p = argparse.ArgumentParser(description='List new trending repo')
p.add_argument('-j', '--json', default=DEFAULT_JSON,
help='checked repos save file in JSON (default: %(default)s)')
p.add_argument('-p', '--period',
choices=['today', 'this-week', 'this-month'],
default='today',
help='time period of trending (default: %(default)s)')
p.add_argument('language', nargs='+',
help='language names, all lower case, spaces to dashes')
args = p.parse_args()
# get checked list
LIST = {}
LIST_CHANGED = False
if path.exists(args.json):
with open(args.json) as f:
LIST = json.load(f)
PERIODS = {'today': 'daily', 'this-week': 'weekly', 'this-month': 'monthly'}
period = PERIODS[args.period]
all_language = [lang.replace('cpp', 'c++') for lang in args.language]
for lang in args.language:
for repo, description in get_trend(lang, period):
if repo in LIST:
continue
lang_desc = description.rsplit('\n', 1)[-1].strip('()').lower()
if lang_desc and lang_desc not in all_language:
continue
print(REPO_BASE % repo)
print(description)
print()
LIST[repo] = description
LIST_CHANGED = True
# save checked list
if LIST_CHANGED:
with open(args.json, 'w') as f:
# save a JSON like
# {
# "user1/repo1": "description1\n(lang1)",
# "user2/repo2": "description2\n(lang2)"
# }
json.dump(LIST, f, indent=0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment