Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from solus-impar/plugins
Plugins
- Loading branch information
Showing
7 changed files
with
111 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Contributing to `tb2k` | ||
|
||
`tb2k` keeps plugins in the [`topics.functions`][topic functions] module. If | ||
you'd like to contribute a topic source of your own you can submit a pull | ||
request to this repository adding a function to that module. Topic functions | ||
**MUST** conform to the following specification: | ||
|
||
1. Begin with the `topic_` prefix. | ||
2. Take no arguments. | ||
3. Return a `topic` string and a `link` string as a tuple. | ||
- The `link` _can_ be the empty string. | ||
|
||
An example topic function might look like this: | ||
```python | ||
def topic_wwucs(): | ||
topic = 'WWU CS rules!' | ||
link = 'https://wwucs.slack.com' | ||
return topic, link | ||
``` | ||
|
||
[topic functions]: https://github.com/solus-impar/tb2k/blob/master/topics/functions.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import sys | ||
import random | ||
|
||
import wikipedia | ||
|
||
from topics.utils import fetch_json | ||
|
||
|
||
def topic_man_page(): | ||
"""Randomly select a man page from /usr/share/man/.""" | ||
sec = random.randrange(1, 9) | ||
man_dir = "/usr/share/man/man{}/".format(sec) | ||
man_url = "http://man7.org/linux/man-pages/man{}/".format(sec) | ||
|
||
try: | ||
page = random.choice(os.listdir(man_dir)) | ||
except IndexError: | ||
sys.exit("tb2k: error: no man pages in section {}".format(sec)) | ||
except FileNotFoundError: | ||
sys.exit("tb2k: error: {} does not exist".format(man_dir)) | ||
except PermissionError: | ||
sys.exit("tb2k: error: {}: permission denied".format(man_dir)) | ||
|
||
man_url = "{}{}.{}.html".format(man_url, page.split('.')[0], sec) | ||
return "{}({})".format(page.split('.')[0], sec), man_url | ||
|
||
|
||
def topic_wikipedia_programming_language(): | ||
"""Randomly select a programming language from Wikipedia.""" | ||
title = 'List of programming languages' | ||
page = wikipedia.page(title=title) | ||
lang = random.choice(page.links) | ||
lang_url = "https://en.wikipedia.org/wiki/{}".format(lang.replace(' ','_')) | ||
return lang, lang_url | ||
|
||
|
||
def topic_top_hacker_news_story(): | ||
"""Select the current top Hacker News story.""" | ||
hn_api = 'https://hacker-news.firebaseio.com/v0' | ||
|
||
stories_url = "{}/topstories.json".format(hn_api) | ||
stories = fetch_json(stories_url) | ||
|
||
story_url = "{}/item/{}.json".format(hn_api, stories[0]) | ||
story = fetch_json(story_url) | ||
|
||
return story['title'], story['url'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
import requests | ||
|
||
|
||
def fetch_json(url): | ||
"""Fetch data from a URL and attempt to parse it as JSON.""" | ||
try: | ||
response = requests.get(url) | ||
except requests.ConnectionError: | ||
sys.exit("tb2k: error: cannot connect to {}".format(url)) | ||
|
||
status_code = response.status_code | ||
if status_code == 200: | ||
try: | ||
return response.json() | ||
except ValueError: | ||
sys.exit("tb2k: error: invalid JSON at {}".format(url)) | ||
else: | ||
sys.exit("tb2k: error: {} fetching {}".format(status_code, url)) |