Example #1
0
    def setUp(self):
        httpretty.HTTPretty.enable()
        httpretty.register_uri(httpretty.GET,
                               'https://news.ycombinator.com/',
                               body=get_content('index.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL,
                                                         'best'),
                               body=get_content('best.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL,
                                                         'newest'),
                               body=get_content('newest.html'))

        # check py version
        PY2 = sys.version_info[0] == 2
        if not PY2:
            self.text_type = [str]
        else:
            self.text_type = [unicode, str]

        self.hn = HN()
        self.top_stories = [story for story in self.hn.get_stories()]
        self.newest_stories = [story for story in self.hn.get_stories(
            story_type='newest')]
        self.best_stories = [story for story in self.hn.get_stories(
            story_type='best')]
Example #2
0
	def setUp(self):
		# check py version
		#self.PY2 = sys.version_info[0] == 2
		self.hn = HN()
		httpretty.HTTPretty.enable()
		httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL, 'leaders'),
                           body=get_content('leaders.html'))
Example #3
0
def get_best_articles(n):
    """
    Retrieves n best articles
    returns a list of dictionaries, dicts represent article
    """
    hn = HN()
    type = 'best'
    stories = hn.get_stories(story_type=type, limit=n)
    article_list = []
    for article in stories:
        article_dict = {
            'id': article.story_id,
            'title': article.title,
            'points': article.points,
            'comments': article.comments_link,
            'submitter': article.submitter,
            'url': article.link,
            'self': article.is_self,
            'domain': article.domain,
            'profile': article.submitter_profile,
            'time': article.published_time,
            'num_comments': article.num_comments,
            'rank': article.rank
        }
        article_list.append(article_dict)
    return article_list
Example #4
0
def get_hn(qtd = 1,email=None, subject = None):
    #pega as Notícias do Hacker News
    from hn import HN

    novidades = HN()
    results = []
    for s in novidades.get_stories(story_type='newest', limit = qtd):
        results.append(s.link)

    if qtd > 1:
        return results
    else:
        return results[0]
Example #5
0
    def setUp(self):
        httpretty.HTTPretty.enable()
        httpretty.register_uri(httpretty.GET,
                               'https://news.ycombinator.com/',
                               body=get_content('index.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL,
                                                         'item?id=6115341'),
                               body=get_content('6115341.html'))

        # check py version
        self.PY2 = sys.version_info[0] == 2
        self.hn = HN()
        self.story = Story.fromid(6115341)
Example #6
0
    def setUp(self):
        # check py version
        PY2 = sys.version_info[0] == 2
        if not PY2:
            self.text_type = [str]
        else:
            self.text_type = [unicode, str]

        self.hn = HN()
        self.top_stories = [story for story in self.hn.get_stories()]
        self.newest_stories = [
            story for story in self.hn.get_stories(story_type='newest')
        ]
        self.best_stories = [
            story for story in self.hn.get_stories(story_type='best')
        ]
Example #7
0
    def setUp(self):
        httpretty.HTTPretty.enable()
        httpretty.register_uri(httpretty.GET, 'https://news.ycombinator.com/', 
            body=get_content('index.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL, 'best'), 
            body=get_content('best.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL, 'newest'), 
            body=get_content('newest.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL, 'x?fnid=WK2fLO5cPAJ9DnZbm8XOFR'), 
            body=get_content('best2.html'))
        httpretty.register_uri(httpretty.GET, '%s/%s' % (constants.BASE_URL, 'news2'), 
            body=get_content('news2.html'))

        # check py version
        self.PY2 = sys.version_info[0] == 2
        self.hn = HN()
Example #8
0
class HackernewsExtension(Extension):

    _preferences = Preferences()
    _screens = Screens()
    _hn = HN()
    _cache = Cache()

    def __init__(self):
        super(HackernewsExtension, self).__init__()

        # Subscribe to events
        self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
        self.subscribe(
            PreferencesEvent,
            PreferencesEventListener())  # Set preferences to inner members
        self.subscribe(PreferencesUpdateEvent,
                       PreferencesUpdateEventListener())
        self.subscribe(ItemEnterEvent, ItemEnterEventListener())

    def show_top_stories(self, load_page_number):

        # Check if the page is cached
        cached_page = self._cache.get_page(load_page_number)

        if cached_page:
            return RenderResultListAction(cached_page)
        else:
            stories = self._hn.load_top_stories(load_page_number)
            items = self._screens.render_top_stories(stories[0],
                                                     load_page_number)

            # Cache first item
            self._cache.add_page(load_page_number, items)

            # Cache items
            for i in range(1, len(stories)):
                page_number = load_page_number + i
                current_items = self._screens.render_top_stories(
                    stories[i], page_number)
                self._cache.add_page(page_number, current_items)

        return RenderResultListAction(items)

    def show_open_confirmation(self, number_of_links):
        confirmation = self._screens.render_open_confirmation(number_of_links)
        return RenderResultListAction(confirmation)
Example #9
0
    def setUp(self):
        # check py version
        # #self.PY2 = sys.version_info[0] == 2
        self.hn = HN()
        httpretty.HTTPretty.enable()
        httpretty.register_uri(httpretty.GET,
                               '%s/%s' % (constants.BASE_URL, 'leaders'),
                               body=get_content('leaders.html'))

        def tearDown(self):
            httpretty.HTTPretty.disable()

        def test_get_leaders_with_no_parameter(self):
            result = [leader for leader in self.hn.get_leaders()]
            self.assertEqual(len(result), 10)

        def test_get_leaders_with_parameter(self):
            value = 50
            result = [leader for leader in self.hn.get_leaders(value)]
            self.assertEqual(len(result), value)
Example #10
0
#!/usr/bin/env python

from hn import HN, Story

hn = HN()

# a generator over 30 stories from top page
top_iter = hn.get_stories(limit=30)


# print top stories from homepage
for story in top_iter:
    print((story.title.encode('utf-8')))
    # print('[{0}] "{1}" by {2}'.format(story.points, story.title,
    #                                   story.submitter))


# print 10 latest stories
for story in hn.get_stories(story_type='newest', limit=10):
    print((story.title.encode('utf-8')))
    print(('*' * 50))
    print('')


# for each story on front page, print top comment
for story in hn.get_stories():
    print((story.title.encode('utf-8')))
    comments = story.get_comments()
    print((comments[0] if len(comments) > 0 else None))
    print(('*' * 10))
Example #11
0
 def setUp(self):
     # check py version
     self.PY2 = sys.version_info[0] == 2
     self.hn = HN()
 def setUp(self):
     # check py version
     self.PY2 = sys.version_info[0] == 2
     self.hn = HN()
     self.story = Story.fromid(6374031)