odysee.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #####################################################################
  2. # #
  3. # THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE #
  4. # LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet ) #
  5. # FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk ) #
  6. # WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
  7. # IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
  8. # OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE. #
  9. # #
  10. # ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS #
  11. # (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK. #
  12. # YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
  13. # THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER #
  14. # VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG #
  15. # WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ). #
  16. # #
  17. # THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE #
  18. # NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
  19. # THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT #
  20. # SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
  21. # THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT. #
  22. # #
  23. # THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL #
  24. # FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE #
  25. # THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD #
  26. # TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT. #
  27. # #
  28. #####################################################################
  29. # This is for features that are developed only for Odysee. Apart from
  30. # livestreams which are in a dedicated file.
  31. import os
  32. import json
  33. import urllib.request
  34. import urllib.parse
  35. from gi.repository import Gtk
  36. from flbry import ui
  37. from flbry import fetch
  38. # This will make odysee think that we are just a normal browser running windows ( lol )
  39. # and connecting from Odysee.com
  40. headers = {"Origin":"https://odysee.com",
  41. "Referer":"https://odysee.com/",
  42. "User-Agent":"Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"}
  43. def get_odysee_notifications(win):
  44. # This function will query the odysee api for notifications.
  45. auth = win.settings["auth_token"]
  46. if not auth:
  47. return False
  48. url = "https://api.odysee.com/notification/list?auth_token="+auth+"&is_app_readable=true&type=comment_replies%2Chyperchat_replies"
  49. req = urllib.request.Request(url, data=None, headers=headers)
  50. f = urllib.request.urlopen(req)
  51. data = json.loads(f.read().decode('utf-8'))
  52. if "data" in data:
  53. return data["data"]
  54. else:
  55. return False
  56. def get_odysee_views(win, claim_id):
  57. # This function will return the views of a particular claim ID
  58. auth = win.settings["auth_token"]
  59. if not auth:
  60. return False
  61. url="https://api.odysee.com/file/view_count?auth_token="+auth+"&claim_id="+claim_id
  62. req = urllib.request.Request(url, data=None, headers=headers)
  63. f = urllib.request.urlopen(req)
  64. data = json.loads(f.read().decode('utf-8'))
  65. if "data" in data:
  66. return data["data"]
  67. else:
  68. return False
  69. def get_odysee_subs(win, claim_id):
  70. # This function will return the views of a particular claim ID
  71. auth = win.settings["auth_token"]
  72. if not auth:
  73. return False
  74. url="https://api.odysee.com/subscription/sub_count?auth_token="+auth+"&claim_id="+claim_id
  75. req = urllib.request.Request(url, data=None, headers=headers)
  76. f = urllib.request.urlopen(req)
  77. data = json.loads(f.read().decode('utf-8'))
  78. if "data" in data:
  79. return data["data"]
  80. else:
  81. return False
  82. def sync_function(email, password):
  83. # This function will sync the odysee wallet.
  84. # This function is being possible thanks to TrueAuraCoral
  85. # on Notabug. https://notabug.org/jyamihud/FastLBRY-terminal/issues/17#issuecomment-29418
  86. # First we create a "new" user. It will generate an authentication token
  87. # and then we could active that token using the email and password. So the
  88. # server of Odysee could give us access to our wallet.
  89. url = "https://api.odysee.com/user/new"
  90. req = urllib.request.Request(url, data=None)
  91. f = urllib.request.urlopen(req)
  92. data = json.loads(f.read().decode('utf-8'))
  93. # We should have our auth token. If we don't, we abort the operation.
  94. try:
  95. auth_token = data["data"]["auth_token"]
  96. except:
  97. return "Failed to generate auth token."
  98. # Next step is authenticate this auth-token that we just got.
  99. # For some reason Odysee doesn't want a json. But a url string as a
  100. # payload. IDK what is wrong with them.
  101. login_data = "auth_token="+urllib.parse.quote_plus(auth_token)\
  102. +"&email="+urllib.parse.quote_plus(email)\
  103. +"&password="+urllib.parse.quote_plus(password)
  104. login_data = login_data.encode("utf-8")
  105. try:
  106. url = "https://api.odysee.com/user/signin"
  107. req = urllib.request.Request(url, data=login_data)
  108. f = urllib.request.urlopen(req)
  109. data = json.loads(f.read().decode('utf-8'))
  110. except Exception as e:
  111. print(e)
  112. return "Email or Password wrong."
  113. # Now we have the authenticated auth-token.
  114. # Now we can go to the next step.
  115. wallet_hash = fetch.lbrynet("sync_hash")
  116. # Next step. To retrieve the wallet from Odysee.
  117. hash_data = "auth_token="+urllib.parse.quote_plus(auth_token)\
  118. +"&hash="+urllib.parse.quote_plus(wallet_hash)
  119. hash_data = hash_data.encode("utf-8")
  120. try:
  121. url = "https://api.lbry.com/sync/get"
  122. req = urllib.request.Request(url, data=hash_data)
  123. f = urllib.request.urlopen(req)
  124. data = json.loads(f.read().decode('utf-8'))
  125. except Exception as e:
  126. print(e)
  127. return "Could not recieve wallet."
  128. # And the last step. Applying the sync.
  129. sync_apply = fetch.lbrynet("sync_apply",{"password": "",
  130. "blocking": True,
  131. "data": data["data"]["data"]})
  132. return {"auth_token":auth_token}
  133. def sync_ui(win):
  134. # This is an odysee login window.
  135. dialogWindow = Gtk.Dialog("FastLBRY - Sync with Odysee",
  136. buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
  137. Gtk.STOCK_OK, Gtk.ResponseType.OK),
  138. )
  139. #dialogWindow.set_size_request(300,300)
  140. dialogWindow.set_default_icon_from_file("icon.png")
  141. mainbox = dialogWindow.get_content_area()
  142. # Email
  143. ebox = Gtk.HBox()
  144. mainbox.pack_start(ebox, 0,0,5)
  145. ebox.pack_start(Gtk.Label("Email : "), 0,0,5)
  146. email = Gtk.Entry()
  147. ebox.pack_start(email, 1,1,5)
  148. # Password
  149. ebox = Gtk.HBox()
  150. mainbox.pack_start(ebox, 0,0,5)
  151. ebox.pack_start(Gtk.Label("Password : "),0,0,5)
  152. passw, passe = ui.password_entry(win)
  153. ebox.pack_start(passw, 1,1,5)
  154. mainbox.show_all()
  155. response = dialogWindow.run()
  156. if response == Gtk.ResponseType.OK:
  157. # Before we start the sync function. We need to tell the user that it might take a while
  158. email = email.get_text()
  159. password = passe.get_text()
  160. for i in mainbox.get_children():
  161. i.destroy()
  162. mainbox.pack_start(Gtk.Label("Syncing ... ( may take a while )"), 1,1,1)
  163. mainbox.show_all()
  164. response = sync_function(email, password)
  165. if type(response) == dict:
  166. dialogWindow.destroy()
  167. return response
  168. else:
  169. for i in mainbox.get_children():
  170. i.destroy()
  171. mainbox.pack_start(Gtk.Label(response), 1,1,1)
  172. mainbox.show_all()