Last active
June 4, 2021 15:12
-
-
Save alexeygrigorev/dfd535aea26709582f8ac3ead3766adb to your computer and use it in GitHub Desktop.
Running giveaway campaigns on twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from glob import glob | |
from random import shuffle | |
import requests | |
coupon_codes = [ | |
'mlbookcamp-1', | |
'mlbookcamp-2', | |
'mlbookcamp-3', | |
'mlbookcamp-4', | |
'mlbookcamp-5', | |
'mlbookcamp-6', | |
'mlbookcamp-7', | |
'mlbookcamp-8', | |
] | |
top = len(coupon_codes) | |
tweet_id = '1235296848627273735' | |
handle = 'Al_Grigor' | |
key = 'KEY' | |
secret = 'SECRET' | |
auth_url = "https://api.twitter.com/oauth2/token" | |
data = {'grant_type': 'client_credentials'} | |
auth_resp = requests.post(auth_url, auth=(key, secret), data=data) | |
token = auth_resp.json()['access_token'] | |
headers = {'Authorization': 'Bearer %s' % token} | |
# get followers | |
all_followers = set() | |
next_cursor = -1 | |
while next_cursor != 0: | |
followers_url = 'https://api.twitter.com/1.1/followers/ids.json?cursor=%s&' + \ | |
'screen_name=%s&count=5000' | |
followers_url = followers_url % (next_cursor, handle) | |
followers_resp = requests.get(followers_url, headers=headers).json() | |
followers_list = followers_resp['ids'] | |
all_followers.update(followers_list) | |
print('num_followers', len(all_followers)) | |
next_cursor = followers_resp['next_cursor'] | |
print('next_cursor =', next_cursor) | |
# get retweeters | |
retweeter_files = sorted(glob('./results/retweeters-ids-%s-*.txt' % tweet_id)) | |
all_retweeters = {} | |
for f in retweeter_files: | |
with open(f, 'r') as f_in: | |
for line in f_in: | |
screen_name, user_id = line.strip().split(',') | |
user_id = int(user_id) | |
all_retweeters[user_id] = screen_name | |
print('num_retweeters', len(all_retweeters)) | |
# selecting winners | |
valid = list(all_retweeters.keys() & all_followers) | |
shuffle(valid) | |
winners_ids = valid[:top] | |
winners = [all_retweeters[i] for i in winners_ids] | |
print('winners:') | |
for i in winners: | |
print('@%s' % i) | |
template = """ | |
Hi @{name}, | |
Thank you for participating in the giveaway of my book. | |
Here's the code that you can use to get your free copy of the book: {code}. | |
To get it | |
1) go to http://bit.ly/mlbookcamp | |
2) choose the ebook option | |
3) enter the code on the checkout | |
I hope you find it useful! | |
Please get in touch if you have any questions. | |
Alexey | |
""".strip() | |
print('messages:') | |
for n, c in zip(winners, coupon_codes): | |
message = template.format(name=n, code=c) | |
print() | |
print(message) | |
print() | |
print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /home/agrigorev/anaconda3/bin/python /home/agrigorev/notebooks/twitter/retweeters-script.py | |
import requests | |
from time import time | |
import os | |
dir_path = os.path.dirname(os.path.realpath(__file__)) | |
print('running from', dir_path) | |
tweet_id = '1235296848627273735' | |
key = 'KEY' | |
secret = 'SECRET' | |
ts = int(time()) | |
auth_url = "https://api.twitter.com/oauth2/token" | |
data = {'grant_type': 'client_credentials'} | |
auth_resp = requests.post(auth_url, auth=(key, secret), data=data) | |
token = auth_resp.json()['access_token'] | |
url = 'https://api.twitter.com/1.1/statuses/retweets/%s.json?count=100' % tweet_id | |
headers = {'Authorization': 'Bearer %s' % token} | |
retweets_resp = requests.get(url, headers=headers) | |
retweets = retweets_resp.json() | |
retweeters = [(r['user']['screen_name'], r['user']['id']) for r in retweets] | |
with open('%s/results/retweeters-ids-%s-%s.txt' % (dir_path, tweet_id, ts), 'w') as f_out: | |
for r, i in retweeters: | |
f_out.write('%s,%s\n' % (r, i)) | |
print('done') | |
# crontab -e | |
# 30 * * * * /home/agrigorev/anaconda3/bin/python /home/agrigorev/notebooks/twitter/retweeters-script.py | |
# verify it's running | |
# crontab -l |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment