donate.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 file will handle donations and things related to it.
  30. import os
  31. import json
  32. import math
  33. from flbry.variables import *
  34. from flbry import channel
  35. from subprocess import *
  36. def check_devs_file(save_changes=False, user_check=False, diff=False):
  37. # This will check the devs file for being up to date
  38. # It will notify the developers ( and also users ) about
  39. # the need to update the file.
  40. # Getting data about commits from git
  41. try:
  42. git_response = check_output(["git", "shortlog", "-sn", "--no-merges", "--max-count=100", "-e"])
  43. git_response = git_response.decode("utf-8")
  44. git_response = git_response.split("\n")
  45. except:
  46. center("Git is not installed.", "bdrd")
  47. return
  48. # Getting the file for those who have not installed git
  49. try:
  50. with open("devs.json") as f:
  51. devs_data = json.load(f)
  52. except:
  53. center("'devs.json' is missing!", "bdrd")
  54. return
  55. # Parsing the git response and comparing it to devs.json
  56. changed = False
  57. devs = {}
  58. for i in git_response:
  59. s = i.split("\t")
  60. if s[0]:
  61. commits = int(s[0])
  62. devname = s[1]
  63. if "<" in devname: # if user allows email address
  64. devname = devname[devname.find("<")+1:devname.find(">")]
  65. if devname not in devs:
  66. devs[devname] = commits
  67. else:
  68. devs[devname] += commits
  69. for devname in devs:
  70. commits = devs[devname]
  71. if devname in devs_data:
  72. if commits != devs_data[devname]["commits"]:
  73. if diff:
  74. center(devname+" new commits "+str(commits - devs_data[devname]["commits"]))
  75. devs_data[devname]["commits"] = commits
  76. changed = True
  77. else:
  78. lbry = ""
  79. if save_changes:
  80. center("New Developer Found '"+devname+"'")
  81. lbry = input(typing_dots("Developers LBRY link", give_space=True))
  82. if diff:
  83. center("New Developer Found '"+devname+"'")
  84. devs_data[devname] = {"commits":commits, "lbry":lbry}
  85. # Filtering out zeros
  86. for i in devs_data:
  87. if i not in devs and devs_data[i]["commits"]:
  88. changed = True
  89. if diff:
  90. center(i+" no recent commits")
  91. devs_data[i]["commits"] = 0
  92. # Output if changed
  93. if not save_changes and changed:
  94. if not diff:
  95. center("Developers Donation Data 'devs.json' is outdated!", "bdrd")
  96. elif user_check:
  97. center("Developers Donation Data 'devs.json' is alright!", "bdgr")
  98. # Save
  99. elif save_changes:
  100. with open("devs.json", 'w') as f:
  101. json.dump(devs_data, f, indent=4, sort_keys=True)
  102. if changed:
  103. center("Developers Donation Data 'devs.json' is updated!", "bdgr")
  104. else:
  105. center("Developers Donation Data 'devs.json' didn't need updating.", "bdgr")
  106. def add():
  107. # This function will add a new user into the devs.json
  108. try:
  109. with open("devs.json") as f:
  110. devs_data = json.load(f)
  111. except:
  112. raise()
  113. center("'devs.json' is missing!", "bdrd")
  114. return
  115. devname = input(typing_dots("Git Email"), give_space=True)
  116. commits = 0
  117. lbrylink = input(typing_dots("LBRY Link to Transfer Support", give_space=True))
  118. devs_data[devname] = {"commits":commits, "lbry":lbrylink}
  119. with open("devs.json", 'w') as f:
  120. json.dump(devs_data, f, indent=4, sort_keys=True)
  121. center("Developers Donation Data 'devs.json' is updated!", "bdgr")
  122. def donate():
  123. # This is going to be the funtiona to auto-donate fractions
  124. # of your wealth to the people that contribute to the project.
  125. # First we need to show the user the amount he has.
  126. balance = check_output([lbrynet_binary["b"],
  127. "wallet", "balance"])
  128. try:
  129. balance = json.loads(balance)
  130. except:
  131. center("Connect to LBRY first.", "bdrd")
  132. return
  133. available = float(balance["available"])
  134. center("You have "+str(available)+" LBC available to Donate.")
  135. # Now let's ask the user how mush they want to donate.
  136. amount = input(typing_dots("How much to donate?"))
  137. try:
  138. amount = float(amount)
  139. except:
  140. center("Amount should be a number!", "bdrd")
  141. return
  142. if amount > available:
  143. center("You don't have so much available!", "bdrd")
  144. return
  145. # Now let's get out data
  146. check_devs_file()
  147. try:
  148. with open("devs.json") as f:
  149. devs_data = json.load(f)
  150. except:
  151. raise()
  152. center("'devs.json' is missing!", "bdrd")
  153. return
  154. # Now let's make the math
  155. predevs = []
  156. sumoflogs = 0
  157. for devname in devs_data:
  158. if devs_data[devname]["lbry"] and devs_data[devname]["commits"]:
  159. devs_log = math.log1p(devs_data[devname]["commits"])
  160. sumoflogs = sumoflogs + devs_log
  161. predevs.append([devs_data[devname]["lbry"].replace("lbry://", ""), devs_log])
  162. devs = []
  163. for dev in predevs:
  164. devs.append([dev[0], round(amount/sumoflogs*dev[1], 8)])
  165. while True:
  166. d = {"categories":["Developer's Address", "Sending LBC"],
  167. "size":[2,1],
  168. "data":devs}
  169. table(d)
  170. center("Type 'donate' to proceed, or select a number to modify.")
  171. c = input(typing_dots())
  172. if not c:
  173. return
  174. elif c == "donate":
  175. break
  176. try:
  177. c = int(c)
  178. devs[c]
  179. except:
  180. return
  181. new_amount = input(typing_dots("New Donate Amount for "+devs[c][0]))
  182. try:
  183. new_amount = float(new_amount)
  184. except:
  185. new_amount = 0
  186. if new_amount:
  187. devs[c] = [devs[c][0], new_amount]
  188. # Ask the user for a channel
  189. post_as, from_channel = channel.select("Donate as:", claim_id=True, anonymous=True)
  190. # If the user gets to here it's donation time.
  191. print()
  192. progress_bar(0, len(devs)+1, "Resolving Donation Urls...")
  193. resolve = [lbrynet_binary["b"],"resolve"]
  194. for dev in devs:
  195. resolve.append(dev[0])
  196. resolve = check_output(resolve)
  197. try:
  198. resolve = json.loads(resolve)
  199. except:
  200. return
  201. errors = []
  202. for n, dev in enumerate(devs):
  203. progress_bar(n+1, len(devs)+2, "Sending "+str(dev[1])+" to "+dev[0])
  204. try:
  205. claim_id = resolve[dev[0]]["claim_id"]
  206. support_command = [lbrynet_binary["b"],
  207. "support",
  208. "create",
  209. "--claim_id="+claim_id,
  210. "--amount="+str(dev[1]),
  211. "--tip"]
  212. if from_channel:
  213. support_command.append("--channel_id="+from_channel)
  214. test = check_output(support_command)
  215. #print(test)
  216. except Exception as e:
  217. errors.append([dev[0],str(e)])
  218. # Reporting donation to the team ( aka sending a comment to @FastLBRY:f )
  219. if post_as:
  220. progress_bar(len(devs)+1, len(devs)+2, "Commenting on @FastLBRY:f about this...")
  221. text = "Just **donated** "+str(amount)+" LBC to FastLBRY contributors by typing `donate` in [FastLBRY - Terminal](https://notabug.org/jyamihud/FastLBRY-terminal).\n"
  222. for dev in devs:
  223. text = text + "\n - **"+str(dev[0]).replace("lbry://", "")+"** got `"+str(dev[1])+"` LBC from me."
  224. post_comment = [lbrynet_binary["b"],
  225. "comment", "create",
  226. text,
  227. '--claim_id=fb4db67b2a79396f4ba0a52a12e503d0a736f307']
  228. post_comment.append('--channel_name='+post_as)
  229. check_output(post_comment)
  230. # Finishing
  231. progress_bar(len(devs)+2, len(devs)+2, "Done!")
  232. print()
  233. if errors:
  234. center("","bdrd")
  235. d = {"categories":["Developer's Address", "Error"],
  236. "size":[1,1],
  237. "data":errors}
  238. table(d)
  239. center("","bdrd")