supeapp.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import requests
  2. import os
  3. from os.path import expanduser, join
  4. API_DOMAIN = "http://api.supe.io/"
  5. API_HEADERS = {
  6. 'User-Agent': 'okhttp/3.6.0',
  7. 'Platform-Name': 'Android 8.0.0',
  8. 'Application-Version': '1.1.2',
  9. 'Device-Name': 'OnePlus ONEPLUS A0001',
  10. 'Authorization': 'Bearer {}'.format(os.environ['SUPE_TOKEN']),
  11. 'Accept': 'application/vnd.api.v3+json',
  12. 'Content-Type': 'application/json'
  13. }
  14. # Standard code for making request \o/
  15. def request(endpoint, data={}, headers={}, method='POST'):
  16. headers.update(API_HEADERS)
  17. r = requests.request(method, API_DOMAIN+endpoint, data=data, headers=headers)
  18. if r.status_code == 200:
  19. return r.json()
  20. return None
  21. def download(media_url, cwd=expanduser("~")):
  22. filename = media_url.split("/")[-1]
  23. file_path = join(cwd, filename)
  24. if not os.path.isfile(file_path):
  25. r = requests.get(media_url, stream=True)
  26. if r.status_code == 403:
  27. raise Exception("Access Denied")
  28. with open(file_path, 'wb') as fh:
  29. for chunk in r.iter_content(chunk_size=1024):
  30. fh.write(chunk)
  31. def get_pubblishers():
  32. obj = request("me/inbox/publishers?type=broadcast", method='GET')
  33. users = obj['_embedded']['items']
  34. for user in users:
  35. print(user['id'],user['username'])
  36. profile_pic = user['_embedded']['profileAsset']['filename']
  37. cwd = join("./media",str(user['username']))
  38. if not os.path.isdir(cwd):
  39. os.mkdir(cwd)
  40. download(profile_pic, cwd=cwd)
  41. get_pubblisher_video(user['id'], cwd)
  42. def get_pubblisher_video(pub_id, cwd):
  43. obj = request("me/inbox/publisher/{}?type=broadcast".format(pub_id), method='GET')
  44. posts = obj['_embedded']['items']
  45. for post in posts:
  46. print(post['id'],post['type'])
  47. media = post['_embedded']['asset']['filename']
  48. if not os.path.isdir(cwd):
  49. os.mkdir(cwd)
  50. print("Downloading {}".format(post['_embedded']['asset']['type']))
  51. download(media, cwd=cwd)
  52. get_pubblishers()