|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import requests |
| 4 | +from Crypto.Cipher import AES |
| 5 | +from hashlib import sha256 |
| 6 | +from time import time |
| 7 | +from urlparse import urljoin |
| 8 | + |
| 9 | +URL = 'https://feelinsonice-hrd.appspot.com/bq/' |
| 10 | +SECRET = 'iEk21fuwZApXlz93750dmW22pw389dPwOk' |
| 11 | +STATIC_TOKEN = 'm198sOkJEn37DjqZ32lpRu76xmw288xSQ9' |
| 12 | +BLOB_ENCRYPTION_KEY = 'M02cnQ51Ji97vwT4' |
| 13 | +HASH_PATTERN = ('00011101111011100011110101011110' |
| 14 | + '11010001001110011000110001000110') |
| 15 | + |
| 16 | + |
| 17 | +def make_request_token(a, b): |
| 18 | + hash_a = sha256(SECRET + a).hexdigest() |
| 19 | + hash_b = sha256(b + SECRET).hexdigest() |
| 20 | + result = [None] * len(HASH_PATTERN) |
| 21 | + for i, c in enumerate(HASH_PATTERN): |
| 22 | + result[i] = hash_b[i] if c == '1' else hash_a[i] |
| 23 | + return ''.join(result) |
| 24 | + |
| 25 | + |
| 26 | +def pkcs5_pad(data, blocksize=16): |
| 27 | + pad_count = blocksize - len(data) % blocksize |
| 28 | + return data + (chr(pad_count) * pad_count) |
| 29 | + |
| 30 | + |
| 31 | +def is_video(data): |
| 32 | + return len(data) > 1 and data[0] == chr(0x00) and data[1] == chr(0x00) |
| 33 | + |
| 34 | + |
| 35 | +def is_image(data): |
| 36 | + return len(data) > 1 and data[0] == chr(0xFF) and data[1] == chr(0xD8) |
| 37 | + |
| 38 | + |
| 39 | +def decrypt(data): |
| 40 | + cipher = AES.new(BLOB_ENCRYPTION_KEY, AES.MODE_ECB) |
| 41 | + return cipher.decrypt(pkcs5_pad(data)) |
| 42 | + |
| 43 | + |
| 44 | +def encrypt(data): |
| 45 | + cipher = AES.new(BLOB_ENCRYPTION_KEY, AES.MODE_ECB) |
| 46 | + return cipher.encrypt(pkcs5_pad(data)) |
| 47 | + |
| 48 | + |
| 49 | +def timestamp(): |
| 50 | + return int(round(time() * 1000)) |
| 51 | + |
| 52 | + |
| 53 | +class Snapchat(object): |
| 54 | + |
| 55 | + def __init__(self, username, password): |
| 56 | + self.username = username |
| 57 | + self.password = password |
| 58 | + |
| 59 | + def _request(self, endpoint, data=None): |
| 60 | + now = timestamp() |
| 61 | + if data is None: |
| 62 | + data = {} |
| 63 | + data.update({ |
| 64 | + 'username': self.username, |
| 65 | + 'timestamp': now, |
| 66 | + 'req_token': make_request_token( |
| 67 | + getattr(self, 'auth_token', STATIC_TOKEN), str(now)) |
| 68 | + }) |
| 69 | + return requests.post(urljoin(URL, endpoint), data=data) |
| 70 | + |
| 71 | + def login(self): |
| 72 | + r = self._request('login', {'password': self.password}) |
| 73 | + result = r.json() |
| 74 | + if 'auth_token' in result: |
| 75 | + self.auth_token = result['auth_token'] |
| 76 | + return result |
| 77 | + |
| 78 | + def logout(self): |
| 79 | + r = self._request('logout') |
| 80 | + return r.status_code == 200 |
| 81 | + |
| 82 | + def get_updates(self, update_timestamp=0): |
| 83 | + r = self._request('updates', {'update_timestamp': update_timestamp}) |
| 84 | + result = r.json() |
| 85 | + if 'auth_token' in result: |
| 86 | + self.auth_token = result['auth_token'] |
| 87 | + return result |
| 88 | + |
| 89 | + def get_blob(self, snap_id): |
| 90 | + r = self._request('blob', {'id': snap_id}) |
| 91 | + data = decrypt(r.content) |
| 92 | + return data if is_image(data) or is_video(data) else None |
0 commit comments