Skip to content

Commit

Permalink
Add product hunt.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfs committed Mar 9, 2018
1 parent 24afff9 commit f589218
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions scripts/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def main():
(crawlers.RedditCrawler(), FIVE_MINUTES),
(crawlers.MediumCrawler(), FIVE_MINUTES),
(crawlers.HackerNewsCrawler(), FIVE_MINUTES),
(crawlers.ProductHuntCrawler(), FIVE_MINUTES),
(crawlers.GithubCrawler(), THIRTY_MINUTES),
(crawlers.NyTimesCrawler(), THIRTY_MINUTES)
)
Expand Down
30 changes: 30 additions & 0 deletions woid/apps/services/crawlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,33 @@ def update_top_stories(self):

except Exception, e:
logging.error(e)


class ProductHuntCrawler(AbstractBaseCrawler):
def __init__(self):
super(ProductHuntCrawler, self).__init__('producthunt', wrappers.ProductHuntClient())

def update_top_stories(self):
try:
posts = self.client.get_top_posts()
today = timezone.now()
for post in posts:
code = post['slug']
story, created = Story.objects.get_or_create(
service=self.service,
code=code,
date=timezone.datetime(today.year, today.month, today.day, tzinfo=timezone.get_current_timezone())
)

if created:
story.title = post['name']
story.description = post['tagline']
story.url = u'{0}{1}'.format(self.service.story_url, code)

story.score = post['votes_count']
story.comments = post['comments_count']
story.status = Story.OK
story.save()

except Exception, e:
logging.error(e)
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
"model": "services.Service",
"pk": 6,
"fields": {
"name": "Digg",
"slug": "digg",
"url": "http://www.digg.com",
"story_url": "http://digg.com"
"name": "Product Hunt",
"slug": "producthunt",
"url": "https://www.producthunt.com",
"story_url": "https://www.producthunt.com/posts/"
}
}
]
2 changes: 0 additions & 2 deletions woid/apps/services/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Service(models.Model):
last_run = models.DateTimeField(null=True, blank=True)
status = models.CharField(max_length=1, default=GOOD, choices=CURRENT_STATUS)


class Meta:
verbose_name = 'service'
verbose_name_plural = 'services'
Expand Down Expand Up @@ -72,7 +71,6 @@ class Story(models.Model):
nsfw = models.BooleanField(default=False)
description = models.CharField(max_length=2000, null=True, blank=True)


class Meta:
verbose_name = 'story'
verbose_name_plural = 'stories'
Expand Down
19 changes: 19 additions & 0 deletions woid/apps/services/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from firebase import firebase

from django.conf import settings
from django.utils import timezone


requests.packages.urllib3.disable_warnings()
Expand Down Expand Up @@ -117,3 +118,21 @@ def get_most_popular_stories(self):
data['mostshared'] = json_data['results']

return data


class ProductHuntClient(AbstractBaseClient):
def __init__(self):
super(ProductHuntClient, self).__init__()
extra_headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer %s' % settings.PRODUCT_HUNT_TOKEN,
'Host': 'api.producthunt.com'
}
self.headers.update(extra_headers)

def get_top_posts(self):
today = timezone.now().strftime('%Y-%m-%d')
r = requests.get('https://api.producthunt.com/v1/posts?day=%s' % today, headers=self.headers)
data = r.json()
return data['posts']
2 changes: 1 addition & 1 deletion woid/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@
TEMPLATE_CONTEXT_PROCESSORS += ('django.core.context_processors.request',)

NYTIMES_API_KEY = config('NYTIMES_API_KEY')

PRODUCT_HUNT_TOKEN = config('PRODUCT_HUNT_TOKEN')
1 change: 1 addition & 0 deletions woid/templates/includes/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<li><a href="{% url 'services:index' 'github' %}"{% if request.path|startswith:'/github/' %} class="active"{% endif %}>github</a></li>
<li><a href="{% url 'services:index' 'medium' %}"{% if request.path|startswith:'/medium/' %} class="active"{% endif %}>medium</a></li>
<li><a href="{% url 'services:index' 'nytimes' %}"{% if request.path|startswith:'/nytimes/' %} class="active"{% endif %}>nytimes</a></li>
<li><a href="{% url 'services:index' 'producthunt' %}"{% if request.path|startswith:'/producthunt/' %} class="active"{% endif %}>product hunt</a></li>
</ul>
15 changes: 15 additions & 0 deletions woid/templates/services/includes/producthunt_story.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<li>
<a href="{{ story.url }}" target="_blank" rel="nofollow noopener">{{ story.title }}</a>
{% if append_service %}
<small><strong>{{ story.service.name }}</strong></small>
{% endif %}
<p>
{{ story.score }} vote{{ story.score|pluralize }}
&bullet;
{{ story.comments }} comment{{ story.comments|pluralize }}
&bullet;
{{ story.description }}
&bullet;
{{ story.date|date:'M d, Y'|lower }}
</p>
</li>

0 comments on commit f589218

Please sign in to comment.