本文整理汇总了Python中hackernews.HackerNews.top_stories方法的典型用法代码示例。如果您正苦于以下问题:Python HackerNews.top_stories方法的具体用法?Python HackerNews.top_stories怎么用?Python HackerNews.top_stories使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hackernews.HackerNews
的用法示例。
在下文中一共展示了HackerNews.top_stories方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_news
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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
示例2: Hacker
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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)
示例3: TestTopStories
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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)
示例4: TestTopStories
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
class TestTopStories(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_top_stories(self):
top_stories = self.hn.top_stories(limit=10)
self.assertIsInstance(top_stories, list)
self.assertIsInstance(top_stories[0], Item)
self.assertIsNotNone(top_stories)
def test_top_stories_raw(self):
top_stories = self.hn.top_stories(raw=True)
self.assertIsInstance(top_stories, list)
self.assertIsInstance(top_stories[0], str)
self.assertIsNotNone(top_stories)
def tearDown(self):
self.hn.session.close()
示例5: get_hackernews_article
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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
示例6: getHN_stories
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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
示例7: updateHackerNews
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
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"
示例8: sync_with_hacker_news
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
def sync_with_hacker_news():
hn = HackerNews()
for story_id in hn.top_stories(limit=90):
story = hn.get_item(story_id)
persisted_news_item = NewsItem.query.get(story_id)
if persisted_news_item:
print "Updating story:", story_id
persisted_news_item.upvotes = story.score
persisted_news_item.comments = comment_count(story)
else:
print "Adding story:", story_id
news_item = NewsItem(
id=story_id, url=story.url, posted_on=story.submission_time,
upvotes=story.score,
comments=comment_count(story))
db.session.add(news_item)
for user in User.query.all():
db.session.add(UserNewsItem(user=user, news_item=news_item))
db.session.commit()
示例9: HackerNewsTestCase
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
class HackerNewsTestCase(unittest.TestCase):
def setUp(self):
self.hn = HackerNews()
def test_top_story_count(self):
top_stories = self.hn.top_stories()
self.assertEqual(len(top_stories), 100)
def test_max_item_positive_integer(self):
max_item = self.hn.max_item()
self.assertGreaterEqual(max_item, 0)
def test_updates_result_is_dict(self):
updates = self.hn.updates()
self.assertIsInstance(updates, dict)
def test_item_result_is_dict(self):
item = self.hn.item(1)
self.assertIsInstance(item, Item)
def test_user_result_is_dict(self):
item = self.hn.user('pg')
self.assertIsInstance(item, User)
def test_user_created_is_datetime(self):
item = self.hn.user('pg')
self.assertIsInstance(item.created, datetime)
def test_item_time_is_datetime(self):
item = self.hn.item('1')
if item.get('time'):
self.assertIsInstance(item.time, datetime)
def test_raises_connection_timeout(self):
hn = HackerNews(timeout=1)
hn.url = "http://192.0.2.0" # RFC 5735 TEST-NET-1
self.assertRaises(ConnectTimeout, hn.top_stories)
def test_object_to_dict(self):
item = self.hn.item('1')
self.assertIsInstance(item.__dict__, dict)
示例10: HackerNews
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
from hackernews import HackerNews
from .models import Story
hn = HackerNews()
for stories in hn.top_stories(limit=10):
stories = hn.get_item(stories)
print stories.title
print stories.url
print stories.score
print stories.by
print stories.submission_time
print stories.item_id
Story.objects.create(title=stories.title, url = stories.url, \
score = stories.score, submitter = stories.by, \
timestamp = stories.submission_time, hn_id = stories.item_id)
示例11: __init__
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
class HackNews:
def __init__(self):
self.hn = HackerNews()
self.jsonObj = []
def displayHackNews(self, jobsOrHeadlines):
if jobsOrHeadlines == "headlines":
return self.topStories()
elif jobsOrHeadlines == "jobs":
return self.jobAds()
else:
resp.message("Oops, wrong catagory! Text us: 'HACKNEWS: jobs' or 'HACKNEWS: headlines'")
def topStories(self):
uncleanHeadline = ""
cleanHeadline = ""
textReturn = ""
for story_id in self.hn.top_stories(limit=10):
uncleanHeadline = str(self.hn.get_item(story_id))
uncleanHeadline = uncleanHeadline.split(' - ', 1 )
cleanHeadline = uncleanHeadline[1][:-1]
textReturn += cleanHeadline + '\n\n'
self.jsonObj.append({ "title" : cleanHeadline })
if(cleanHeadline and cleanHeadline != ""):
self.jsonObj.append({ "sucess" : "true" })
else:
self.jsonObj.append({ "sucess" : "false" })
return textReturn
def jobAds(self):
textReturn = ""
numLoops = 0
maxLoops = 10
for story_id in self.hn.top_stories():
numLoops += 1
story = self.hn.get_item(story_id)
if numLoops >= 10:
break
if story.item_type == 'job':
uncleanHeadline = str(story)
uncleanHeadline = uncleanHeadline.split(' - ', 1 )
cleanHeadline = uncleanHeadline[1][:-1]
textReturn += cleanHeadline + '\n'
if cleanHeadline and cleanHeadline != "":
self.jsonObj.append({ "title" : cleanHeadline })
if textReturn == "":
textReturn += "No jobs have been posted in Top Stories, try again tomorrow!"
self.jsonObj.append({ "sucess" : "false" })
else:
self.jsonObj.append({ "sucess" : "true" })
return textReturn
def convertToJson(self):
return self.jsonObj
示例12: Scraper
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
class Scraper(object):
"""
Scrapes various services, namely Reddit and HackerNews
used to gather data to feed into the SentenceGenerator
model.
"""
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
def gather_reddit_data(self):
"""
Gathers comments and submission titles from Reddit.
Returns an updated list of pharses after the Reddit data has been gathered.
"""
# split the list of subreddits to allow for parallel processing
subreddit_sublists = Scraper._split_into_sublists(DEFAULT_SUBREDDITS, \
NUM_SUBREDDITS_PROCESSOR)
# setup processes, run, and collect results
reddit_processes = [mp.Process(target=self._gather_reddit_data, args=(subreddits,)) \
for subreddits in subreddit_sublists]
self._execute_and_collect_processes(reddit_processes)
def gather_hn_data(self):
"""
Gathers comments and submission titles from HN.
Returns an updated list of pharses after the HN data has been gathered.
"""
# get top stories from HN and split the list
top_stories = self.hackernews.top_stories()[:3]
stories_sublists = Scraper._split_into_sublists(top_stories, NUM_HN_THREAD_PROCESSOR)
hn_processes = [mp.Process(target=self._gather_hn_data, args=(stories,))
for stories in stories_sublists]
self._execute_and_collect_processes(hn_processes)
def _execute_and_collect_processes(self, processes):
"""
Executes and collects the results of the phrases the scraper has gathered.
"""
for p_num, process in enumerate(processes):
self.logger.debug("Starting process %d " % p_num)
process.start()
for p_num, process in enumerate(processes):
self.logger.debug("Joining process %d " % p_num)
process.join()
self.logger.debug("Combining results")
while self.output.qsize():
phrase = self.output.get()
try:
phrase = phrase.decode("utf-8").encode("ascii", "ignore")
self.phrases.append(phrase)
except UnicodeEncodeError:
self.logger.warning("Phrase %s could not be decoded " %phrase)
@classmethod
def _split_into_sublists(cls, lst, size):
"""
Splits the list, lst, into sublists of size 'size'.
Returns a new list consisting of len(l) / size sublists
of size 'size'.
"""
sublists = []
for i in xrange(0, len(lst), size):
sublists.append(lst[i : i + size])
return sublists
def _gather_reddit_data(self, subreddits):
"""
Gathers data from the Reddit API. The param, subreddits,
are all the subreddits this process will gather data from
and output represents the joint result of multiple threads.
"""
reddit = praw.Reddit(user_agent="Scrum Generator")
for subreddit in subreddits:
# force lazy eval by converting to list
top_submissions = list(reddit.get_subreddit(subreddit).get_top(limit=2))
titles = [entry.title.encode("utf8", "ignore") for entry in top_submissions]
comments = sum([[c.body for c in submission.comments \
if not isinstance(c, praw.objects.MoreComments)] \
for submission in top_submissions], [])
for comment in comments:
#.........这里部分代码省略.........
示例13: str
# 需要导入模块: from hackernews import HackerNews [as 别名]
# 或者: from hackernews.HackerNews import top_stories [as 别名]
places.append("Donut Touch")
places.append("New York Bagels")
places.append("Karl Strauss")
number = random.randint(0,len(places) - 1)
sc.rtm_send_message(chan, "You should go to %s to for food." % places[number])
####JIRA STUFF
elif "!helpdesk" in message:
request = message[10:]
new_issue = j.create_issue(project="IT", summary=request, description="Created by Slack", issuetype={'name':'Service Request'}, reporter={"name": email}) #edit project ID to match.
sc.rtm_send_message(chan, "Your helpdesk ticket for '%s' has been created." % request)
####Hacker News Stuff
elif "!hn" in message:
n=0
sc.rtm_send_message(chan,"Top 2 HackerNews Stories:")
for story_id in hn.top_stories(limit=2):
derp = hn.get_item(story_id)
derp = str(derp)
print "derp is:"
print derp
herp = derp
print "herp is:"
print herp
derpy = derp.split(":")[1]
print "derpy is:"
print derpy
derpy = derpy.split("-")[0]
print "derpy is"
print derpy
derpy = derpy.strip()
print "derpy is"