Created
November 9, 2016 16:54
-
-
Save tburke/0391b6a71c9fa987c311cbfd2b8395fb to your computer and use it in GitHub Desktop.
Hash and Load a file to Snapchat
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
import requests | |
import json | |
import hashlib | |
import sys | |
import time | |
class SnapImport: | |
def __init__(self, client_id, client_secret, code): | |
self.client_id = client_id | |
self.client_secret = client_secret | |
self.code = code | |
self.token = None | |
self.expire = time.time() | |
def SnapToken(self): | |
if time.time() > self.expire: | |
refr = {"grant_type":"refresh_token", "client_id":self.client_id, "client_secret":self.client_secret, "code":self.code} | |
r = requests.post('https://accounts.snapchat.com/login/oauth2/access_token',data=refr) | |
j = r.json() | |
self.expire = time.time() + j['expires_in'] | |
self.token = j['access_token'] | |
return self.token | |
def Header(self): | |
return {'Content-Type':'application/json', 'Authorization':'Bearer {}'.format(self.SnapToken())} | |
def ListSegments(self, adact): | |
seguri = 'https://adsapi.snapchat.com/v1/adaccounts/{ad_acount_id}/segments'.format(ad_acount_id=adact) | |
r = requests.get(seguri, headers={"Authorization":"Bearer {}".format(self.SnapToken())}) | |
return r | |
def AddUsers(self, segment, users): | |
usruri = 'https://adsapi.snapchat.com/v1/segments/{}/users'.format(segment) | |
d = {'users':[{'id':segment,'schema':['EMAIL_SHA256'],'data':[ | |
[u] for u in users]}]} | |
r = requests.post(usruri, headers=self.Header(), data=json.dumps(d)) | |
return r | |
def Segment(self, segment): | |
seguri = 'https://adsapi.snapchat.com/v1/segments/{}/'.format(segment) | |
r = requests.get(seguri, headers=self.Header()) | |
return r | |
def NewSegment(self, adact, name, descr): | |
d = {'segments': [{'name': name, 'description': descr, 'source_type': 'FIRST_PARTY', 'retention_in_days': 180, 'ad_account_id': adact}]} | |
seguri = 'https://adsapi.snapchat.com/v1/adaccounts/{ad_acount_id}/segments'.format(ad_acount_id=adact) | |
r = requests.post(seguri, headers=self.Header(), data=json.dumps(d)) | |
return r.json()['segments'][0]['segment']['id'] | |
def UploadUsers(self, segment, users): | |
increment = 100000 | |
i = 0 | |
while i < len(users): | |
print i, i+increment | |
r = self.AddUsers(segment, users[i:i+increment]) | |
print r, r.content | |
i += increment | |
return True | |
def LoadUsers(self, segment, filename): | |
f = open(filename) | |
users = [l.strip() for l in f] | |
f.close() | |
return self.UploadUsers(segment, users) | |
def LoadAndHashUsers(self, segment, filename): | |
f = open(filename) | |
users = [hashlib.sha256(l.strip().lower()).hexdigest() for l in f] | |
f.close() | |
return self.UploadUsers(segment, users) | |
if __name__ == '__main__': | |
if len(sys.argv) < 3: | |
print "Usage: python snapload.py client_id client_secret code ad_account segment_name file" | |
else: | |
client_id = sys.argv[1] | |
client_secret = sys.argv[2] | |
code = sys.argv[3] | |
ad_account = sys.argv[4] | |
segment_name = sys.argv[5] | |
filename = sys.argv[6] | |
s = SnapImport(client_id, client_secret, code) | |
segment = s.NewSegment(ad_account, segment_name, segment_name) | |
print segment | |
s.LoadAndHashUsers(segment, filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment