Skip to content

Instantly share code, notes, and snippets.

@bartoszhernas
Created July 18, 2016 17:57
Show Gist options
  • Save bartoszhernas/d2a50180ed9e7b21e4f5a8a96d7940e3 to your computer and use it in GitHub Desktop.
Save bartoszhernas/d2a50180ed9e7b21e4f5a8a96d7940e3 to your computer and use it in GitHub Desktop.
How to send tweets to all ProductHunt upvoters
# -*- coding: utf-8 -*-
# I was lazy, so go and set up Twitter command line tool: https://github.com/sferik/t
# so when you type `t update Hi` it posts to Twitter
# Then this script will send the same twitt every 5 secs to each upvoter
last_user = None # In case you stop the script in middle, just put the last username here and all users up to this one will be skipped
from __future__ import absolute_import, unicode_literals
import logging
import os
import subprocess
import time
import requests
def get_ph_voters(offset):
results = requests.get('https://www.producthunt.com/posts/26686/voters?offset={}&limit=25'.format(offset))
data = results.json()
if not data:
return None
return [{'t': x['twitter_username'], 'name': x['name']} for x in data if x['twitter_username']]
has_next = True
handles = []
page = 0
while has_next:
tmp_handles = get_ph_voters(25 * page)
page += 1
if not tmp_handles:
has_next = False
break
handles += tmp_handles
def send_tweet(username, fname):
msg = '''@{username} Hi {fname}, thx for upvoting STAMP last year ❤️. We've just added Tidal, check it out -> https://www.producthunt.com/tech/stamp-tidal-to-apple-music'''.format(username=username, fname=fname)
try:
subprocess.call(["t", "update", msg])
except Exception:
pass
reached_last = False
for handle in handles:
username = handle['t']
name = handle['name']
fname = name.split(" ")[0]
if not last_user or username == last_user:
reached_last = True
if not reached_last or username == last_user:
print "Skipping {}".format(username)
continue
print "Sending twit to {}".format(username)
send_tweet(username, fname)
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment