Rewrote OSRC to use HTML scraping because the API has been changed

This commit is contained in:
Luke Rogers 2014-02-23 23:50:32 +13:00
parent c7d2535098
commit 807cbf3f07
3 changed files with 13 additions and 13 deletions

View File

@ -3,7 +3,6 @@ from bs4 import BeautifulSoup
from util import hook, http, web
api_url = "http://osrc.dfm.io/{}/stats"
user_url = "http://osrc.dfm.io/{}"
@ -12,18 +11,19 @@ def osrc(inp):
"""osrc <github user> -- Gets an Open Source Report Card for <github user>"""
user_nick = inp.strip()
url = api_url.format(user_nick)
url = user_url.format(user_nick)
try:
response = http.get_json(url)
soup = http.get_soup(url)
except (http.HTTPError, http.URLError):
return "Couldn't find any stats for this user."
response["nick"] = user_nick
soup = BeautifulSoup(response["summary"])
response["work_time"] = soup.find("a", {"href": "#day"}).contents[0]
report = soup.find("div", {"id": "description"}).find("p").get_text()
response["short_url"] = web.try_isgd(user_url.format(user_nick))
# Split and join to remove all the excess whitespace, slice the
# string to remove the trailing full stop.
report = " ".join(report.split())[:-1]
return "{nick} is a {lang_user}. {nick} is a {hacker_type} " \
"who seems to {work_time} - {short_url}".format(**response)
short_url = web.try_isgd(url)
return "{} - {}".format(report, short_url)

View File

@ -24,6 +24,4 @@ def suggest(inp):
soup = BeautifulSoup(out)
out = soup.get_text()
out = text.truncate_str(out, 200)
return out
return text.truncate_str(out, 200)

View File

@ -144,7 +144,9 @@ def truncate_words(content, length=10, suffix='...'):
# from <http://stackoverflow.com/questions/250357/smart-truncate-in-python>
def truncate_str(content, length=100, suffix='...'):
"""Truncates a string after a certain number of chars."""
"""Truncates a string after a certain number of chars.
@rtype : str
"""
if len(content) <= length:
return content
else: