Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for 'YC sponsored' posts #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions hnapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ def getStoryScore(self, source):
"""
scoreStart = source.find('>', source.find('>')+1) + 1
scoreEnd = source.find(' ', scoreStart)
return int(source[scoreStart:scoreEnd])
score = source[scoreStart:scoreEnd]
if not score.isdigit():
return -1

return int(score)

def getSubmitter(self, source):
"""
Expand Down Expand Up @@ -155,11 +159,14 @@ def getHNID(self, source):
"""
Gets the Hacker News ID of a story.
"""
urlStart = source.find('score_') + 6
idPrefix = 'score_'
urlStart = source.find(idPrefix) + len(idPrefix)
if urlStart <= len(idPrefix):
return -1

urlEnd = source.find('"', urlStart)
return int(source[urlStart:urlEnd])


def getCommentsURL(self, source):
"""
Gets the comment URL of a story.
Expand Down Expand Up @@ -225,7 +232,14 @@ def getStories(self, source):
newsStories[i].commentCount = storyCommentCounts[i]
newsStories[i].commentsURL = storyCommentURLs[i]
newsStories[i].id = storyIDs[i]


# Fix for 'YC sponsored' posts without comments
if newsStories[i].id < 0:
idStart = newsStories[i].URL.find('item?id=') + 8
newsStories[i].id = int(newsStories[i].URL[idStart:])
newsStories[i].commentsURL = ''
newsStories[i].submitter = 'Y Combinator'

return newsStories

##### End of internal methods. #####
Expand Down