本文整理汇总了Python中hackernews.HackerNews类的典型用法代码示例。如果您正苦于以下问题:Python HackerNews类的具体用法?Python HackerNews怎么用?Python HackerNews使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HackerNews类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestGetUser
class TestGetUser(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_get_user(self):
user = self.hn.get_user('pg')
self.assertIsInstance(user, User)
self.assertEqual(user.user_id, 'pg')
self.assertEqual(user.created,
datetime.datetime.fromtimestamp(1160418092))
self.assertEqual(repr(user), '<hackernews.User: pg>')
def test_get_invalid_user(self):
self.assertRaises(InvalidUserID, self.hn.get_user, 'a')
def test_get_user_expand(self):
user = self.hn.get_user('avinassh', expand=True)
self.assertIsInstance(user, User)
self.assertEqual(user.user_id, 'avinassh')
self.assertIsInstance(user.comments[0], Item)
self.assertIsInstance(user.stories[0], Item)
def tearDown(self):
self.hn.session.close()
示例2: get_news
def get_news(from_num, num_headlines):
hn = HackerNews()
print "Starting HN"
news_lst = []
for story_id in hn.top_stories(limit=num_headlines):
news_lst.append(hn.get_item(story_id).title + "\n")
return news_lst
示例3: Hacker
class Hacker(object):
def __init__(self, vim):
self.vim = vim
self.hn = HackerNews()
self.urls = None
@neovim.command("Test")
def test(self):
self.vim.command("vsplit")
@neovim.command('HackerNews')
def fill_buffer(self):
stories = []
urls = {}
for story in self.hn.top_stories()[0:30]:
item = self.hn.get_item(story)
stories.append(item.title)
urls[item.title] = item.url
self.vim.command("split HackerNews")
self.vim.command("buffer HackerNews")
self.vim.command("set buftype=nofile")
self.vim.command("set bufhidden=hide")
self.vim.command("setlocal noswapfile")
self.vim.current.buffer[:] = stories
self.urls = urls
@neovim.command('HackerOpen')
def autocmd_handler(self):
url = self.urls[self.vim.current.line]
webbrowser.open_new_tab(url)
示例4: get_hackernews_article
def get_hackernews_article():
hn_wrapper = HackerNews()
index = random.choice(hn_wrapper.top_stories())
story = hn_wrapper.get_item(index)
result = story.title
if story.url is not None:
result += "\n" + story.url
return result
示例5: getHN_stories
def getHN_stories(self, article_limit):
hn = HackerNews()
articles_to_retrieve = int(article_limit*1.5)
top_story_ids = hn.top_stories(limit=articles_to_retrieve)
stories = []
for story_id in top_story_ids:
stories.append(hn.get_item(story_id))
return stories
示例6: TestGetMaxItem
class TestGetMaxItem(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_get_max_item(self):
max_item_id = self.hn.get_max_item()
self.assertIsInstance(max_item_id, int)
def test_get_max_item_expand(self):
max_item = self.hn.get_max_item(expand=True)
self.assertIsInstance(max_item, Item)
def tearDown(self):
self.hn.session.close()
示例7: refresh
def refresh(self, widget=None, no_timer=False, chrome_data_directory=None, firefox_data_directory=None):
"""Refreshes the menu """
try:
# Create an array of 20 false to denote matches in History
searchResults = [False] * 20
data = list(reversed(HackerNews.getHomePage()[0:20]))
urls = [item['url'] for item in data]
if chrome_data_directory:
searchResults = self.mergeBoolArray(searchResults, Chrome.search(urls, chrome_data_directory))
if firefox_data_directory:
searchResults = self.mergeBoolArray(searchResults, Firefox.search(urls, firefox_data_directory))
# Remove all the current stories
for i in self.menu.get_children():
if hasattr(i, 'url'):
self.menu.remove(i)
# Add back all the refreshed news
for index, item in enumerate(data):
item['history'] = searchResults[index]
if item['url'].startswith('item?id='):
item['url'] = "https://news.ycombinator.com/" + item['url']
self.addItem(item)
# Catch network errors
except requests.exceptions.RequestException as e:
print "[+] There was an error in fetching news items"
finally:
# Call every 10 minutes
if not no_timer:
gtk.timeout_add(10 * 30 * 1000, self.refresh, widget, no_timer, chrome_data_directory)
示例8: updateHackerNews
def updateHackerNews():
sql = 'truncate discussion'
database_execute(sql)
hn = HackerNews()
id=1
stories=hn.top_stories(limit=30)
for story_id in stories:
item=hn.get_item(story_id)
id=story_id
url="https://news.ycombinator.com/item?id="+str(story_id)
title=item.title.replace("'","")
score=item.score
sql = "insert into discussion values('%s','%s','%s','%s')"%(id,title,url,score)
#FL.debug(sql)
database_execute(sql)
return "success"
示例9: TestGetMaxItem
class TestGetMaxItem(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_get_max_item(self):
max_item_id = self.hn.get_max_item()
self.assertIsInstance(max_item_id, int)
示例10: __init__
def __init__(self, logger):
"""
Initializes an instance of Scraper. Requires that a logger
to denote the progress of the Scraper to be passed in.
"""
self.phrases = []
self.hackernews = HackerNews()
self.output = mp.Queue()
self.logger = logger
示例11: TestGetAsync
class TestGetAsync(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
self.url = 'https://hacker-news.firebaseio.com/v0/item/8863.json'
self.err_url = 'https://hacker-news.firebaseio.com/v0/items/8863.json'
def test_get_async(self):
response = self.hn._run_async([self.url])
self.assertEqual(response[0]['id'], 8863)
self.assertEqual(response[0]['by'], 'dhouston')
def test_get_async_error(self):
response = self.hn._run_async([self.err_url])
self.assertEqual(response, [None])
def tearDown(self):
self.hn.session.close()
示例12: TestTopStories
class TestTopStories(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_top_stories(self):
top_stories = self.hn.top_stories()
self.assertIsInstance(top_stories, list)
self.assertIsNotNone(top_stories)
示例13: TestjobStories
class TestjobStories(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_job_stories(self):
job_stories = self.hn.job_stories()
self.assertIsInstance(job_stories, list)
self.assertIsNotNone(job_stories)
示例14: TestshowStories
class TestshowStories(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_show_stories(self):
show_stories = self.hn.show_stories()
self.assertIsInstance(show_stories, list)
self.assertIsNotNone(show_stories)
示例15: TestaskStories
class TestaskStories(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_ask_stories(self):
ask_stories = self.hn.ask_stories()
self.assertIsInstance(ask_stories, list)
self.assertIsNotNone(ask_stories)