publish.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 publish a selected file into LBRY Network.
  30. from subprocess import *
  31. import json
  32. import os
  33. import random
  34. from flbry import url
  35. from flbry import channel
  36. from flbry import markdown
  37. from flbry.variables import *
  38. def upload(data):
  39. # This function actually will upload the file.
  40. # reference:
  41. # --name | lbry:// url to publish to
  42. # --bid | LBC to use while publishing
  43. # --fee_amount | How much does one need to pay for the publication
  44. # --file_path | The path to a file that's going to be uploaded
  45. # --title | Title of the publication on the LBRY network
  46. # --license | License of the publication
  47. # --license_url | Link to the license
  48. # --thumbnail_url | Link to a thumbnail image
  49. # --description | A string of text describing the publication
  50. # --tags | List of tags.
  51. # --channel_name | Name of a channel to which to upload
  52. # ( without it will be anonymous)
  53. # More commands to upload function you can see at:
  54. # https://lbry.tech/api/sdk#publish
  55. # An example of a working publish command:
  56. # ./lbrynet publish --name="testing_upload_via_sdk_v2" --bid=0.01
  57. # --file_path="/home/vcs/Desktop/upload_test_file.md"
  58. # --title="Testing Uploading Directly from the LBRY SDK v2"
  59. # --license="CC-BY-SA" --channel_name="@blenderdumbass"
  60. p = ["flbry/lbrynet", "publish", "--name="+data["name"],
  61. "--bid="+str(data["bid"]), "--file_path="+data["file_path"]]
  62. # If thumbnail is on the system and it's a file.
  63. if os.path.isfile(data["thumbnail_url"]):
  64. center("Uploading thumbnail to LBRY")
  65. rndname = ""
  66. good = "qwertyuiopasdfghjklzxcvbnm-_QWERTYUIOPASDFGHJKLZXCVBNM1234567890"
  67. for i in range(70):
  68. rndname = rndname + random.choice(good)
  69. try:
  70. out = upload({"name":rndname,
  71. "bid":0.001,
  72. "file_path":data["thumbnail_url"],
  73. "title":"",
  74. "license":"",
  75. "license_url":"",
  76. "thumbnail_url":"",
  77. "channel_id":"",
  78. "channel_name":"",
  79. "description":"",
  80. "fee_amount":0,
  81. "tags":[]
  82. })
  83. # Replacing the thumb data string
  84. data["thumbnail_url"] = out['outputs'][0]['permanent_url'].replace("lbry://","https://spee.ch/")
  85. except:
  86. data["thumbnail_url"] = ""
  87. center("Failed to upload thumbnail", "bdrd")
  88. for i in ["title", "license", "license_url", "thumbnail_url", "description", "channel_name", "fee-amount"]:
  89. if i in data:
  90. if data[i]:
  91. p.append("--"+i+"="+data[i])
  92. #if data["fee_amount"]:
  93. # p.append("fee_currency=LBC")
  94. if "tags" in data:
  95. if data["tags"]:
  96. for i in data["tags"]:
  97. p.append("--tags="+i) # THIS IS STUPID IK. BUT IT WORKS
  98. out = check_output(p)
  99. try:
  100. out = json.loads(out)
  101. except:
  102. print(" Connect to LBRY first.")
  103. return
  104. return out
  105. def view_data(data):
  106. # this function will print the data
  107. # LBRY URL
  108. d = {"categories": ["LBRY URL"],
  109. "size":[1],
  110. "data":[["lbry://"+data["name"]]]}
  111. table(d, False)
  112. # FILE PATH
  113. d = {"categories": ["File Path"],
  114. "size":[1],
  115. "data":[[data["file_path"]]]}
  116. table(d, False)
  117. # CHANNEL, BID AND PRICE
  118. if not data["channel_name"]:
  119. channel = "[anonymous]"
  120. else:
  121. channel = data["channel_name"]
  122. if not data["fee_amount"]:
  123. price = "Gratis"
  124. else:
  125. price = str(data["fee_amount"])+" LBC"
  126. d = {"categories": ["Channel ID", "LBC BID", "Price"],
  127. "size":[1, 1, 1],
  128. "data":[[channel, str(data["bid"])+" LBC", price]]}
  129. table(d, False)
  130. # TITLE AND THUMBNAIL
  131. if not data["thumbnail_url"]:
  132. thumb = "[no thumbnail]"
  133. else:
  134. thumb = data["thumbnail_url"]
  135. if not data["title"]:
  136. title = "[no title]"
  137. else:
  138. title = data["title"]
  139. d = {"categories": ["Thumbnail", "Title"],
  140. "size":[2, 3],
  141. "data":[[thumb, title]]}
  142. table(d, False)
  143. # LICENSING INFORMATION
  144. if not data["license"]:
  145. license = "[no license]"
  146. else:
  147. license = data["license"]
  148. if not data["license_url"]:
  149. lurl = "[no license url]"
  150. else:
  151. lurl = data["license_url"]
  152. d = {"categories": ["License", "URL"],
  153. "size":[3, 2],
  154. "data":[[license, lurl]]}
  155. table(d, False)
  156. # DESCRIPTION PREVIEW
  157. d = {"categories": ["DESCRIPTION PREVIEW"],
  158. "size":[1],
  159. "data":[[data["description"].replace("\n", " ")]]}
  160. table(d, False)
  161. # Tags PREVIEW
  162. d = {"categories": ["TAGS"],
  163. "size":[1],
  164. "data":[[data["tags"]]]}
  165. table(d, False)
  166. def configure(file_path=""):
  167. # This function will prepare the publication data, before sending it
  168. # to the upload() function above.
  169. while not os.path.exists(file_path):
  170. center("File '"+file_path+"' not found", "bdrd")
  171. file_path = input(" File path:")
  172. lbryname = ""
  173. good = "qwertyuiopasdfghjklzxcvbnm-_QWERTYUIOPASDFGHJKLZXCVBNM1234567890"
  174. for i in range(70):
  175. lbryname = lbryname + random.choice(good)
  176. center("Upload Manager of FastLBRY")
  177. data = {"name":lbryname,
  178. "bid":0.001,
  179. "file_path":file_path,
  180. "title":"",
  181. "license":"",
  182. "license_url":"",
  183. "thumbnail_url":"",
  184. "channel_id":"",
  185. "channel_name":"",
  186. "description":"",
  187. "fee_amount":0,
  188. "tags":[]
  189. }
  190. while True:
  191. # preview the data
  192. view_data(data)
  193. center("---type 'help' to read how it works---")
  194. # input
  195. c = input(typing_dots())
  196. if not c:
  197. break
  198. # Update file_path
  199. elif c.startswith("file"):
  200. if " " in c:
  201. file_path = c[c.find(" ")+1:]
  202. else:
  203. file_path = input(" File path:")
  204. while not os.path.exists(file_path):
  205. center("File '"+file_path+"' not found", "bdrd")
  206. file_path = input(" File path:")
  207. data["file_path"] = file_path
  208. # Update the bid info
  209. elif c.startswith("bid"):
  210. if " " in c:
  211. bid = c[c.find(" ")+1:]
  212. else:
  213. bid = input(" Bid: ")
  214. while True:
  215. try:
  216. bid = float(bid)
  217. if not bid > 0.0001:
  218. 1 / 0 # Fail switch
  219. break
  220. except:
  221. center("Bid cannot be: "+str(bid), "bdrd")
  222. bid = input(" Bid: ")
  223. data["bid"] = bid
  224. # Setup a price
  225. elif c.startswith("price"):
  226. if " " in c:
  227. price = c[c.find(" ")+1:]
  228. else:
  229. price = input(" Price: ")
  230. while True:
  231. try:
  232. price = float(price)
  233. if price < 0:
  234. 1 / 0 # Fail switch
  235. break
  236. except:
  237. center("Price cannot be: "+str(price), "bdrd")
  238. price = input(" Price: ")
  239. data["fee_amount"] = price
  240. # URL for the publication
  241. elif c.startswith("url"):
  242. if " " in c:
  243. url = c[c.find(" ")+1:]
  244. else:
  245. url = input(" LBRY URL : lbry://")
  246. name = ""
  247. for i in url:
  248. if i in good:
  249. name = name + i
  250. else:
  251. name = name + "-"
  252. data["name"] = name
  253. # Title
  254. elif c.startswith("title"):
  255. if " " in c:
  256. title = c[c.find(" ")+1:]
  257. else:
  258. title = input(" Title: ")
  259. data["title"] = title
  260. # License setting
  261. elif c == "license":
  262. lsnzs = []
  263. for i in licenses:
  264. lsnzs.append([i[0],i[2]])
  265. d = {"categories":["Name","Comment"],
  266. "size":[1,1],
  267. "data":lsnzs}
  268. table(d)
  269. center("---choose a license or press enter for custom---")
  270. lc = input(typing_dots())
  271. try:
  272. lselect = licenses[int(lc)]
  273. lname = lselect[0]
  274. llink = lselect[1]
  275. except:
  276. lname = input(" License name: ")
  277. llink = input(" License link: ")
  278. data["license"] = lname
  279. data["license_url"] = llink
  280. # Channel
  281. elif c == "channel":
  282. ch, chid = channel.select("Select from where to publish.", claim_id=True)
  283. data["channel_id"] = chid
  284. data["channel_name"] = ch
  285. # Tags
  286. elif c.startswith("tags"):
  287. if " " in c:
  288. th = c[c.find(" ")+1:]
  289. else:
  290. th = input(" Tags (separated by,): ")
  291. data["tags"] = th.split(",")
  292. elif c.startswith("thumbnail"):
  293. if " " in c:
  294. th = c[c.find(" ")+1:]
  295. else:
  296. th = input(" Thumbnail url: ")
  297. data["thumbnail_url"] = th
  298. # Description
  299. elif c.startswith("description"):
  300. if " " in c:
  301. df = c[c.find(" ")+1:]
  302. try:
  303. text = open(df, "r")
  304. text = text.read()
  305. except:
  306. text = open("/tmp/fastlbrydescriptiontwriter.txt", "w")
  307. text.write("Type your description here. Don't forget to save. Then return to FastLBRY.")
  308. text.close()
  309. os.system(df+" /tmp/fastlbrydescriptiontwriter.txt")
  310. center("Press Enter when the file is ready and saved.")
  311. input()
  312. text = open("/tmp/fastlbrydescriptiontwriter.txt", "r")
  313. text = text.read()
  314. else:
  315. text = input(" Description: ")
  316. data["description"] = text
  317. elif c == "help":
  318. markdown.draw("help/publish.md", "Publishing Help")
  319. # SAVE / LOAD OPTIONS
  320. elif c.startswith("save"):
  321. if " " in c:
  322. pn = c[c.find(" ")+1:]
  323. else:
  324. pn = input(" Preset Name: ")
  325. # Create the preset folder is it's not there
  326. try:
  327. os.mkdir("presets")
  328. except:
  329. pass
  330. # Write the json file
  331. with open("presets/"+pn+'.json', 'w') as f:
  332. json.dump(data, f, indent=4, sort_keys=True)
  333. elif c.startswith("load"):
  334. if " " in c:
  335. pn = c[c.find(" ")+1:]
  336. else:
  337. pn = input(" Preset Name: ")
  338. # loading the json file
  339. try:
  340. name = data["name"]
  341. file_path = data["file_path"]
  342. with open("presets/"+pn+'.json') as f:
  343. data = json.load(f)
  344. data["file_path"] = file_path
  345. data["name"] = name
  346. except:
  347. center("There is no '"+pn+"' preset!", "bdrd")
  348. # PUBLISHING
  349. elif c == "publish":
  350. out = upload(data)
  351. try:
  352. center("LBRY URL FULL TEXT:")
  353. print(out['outputs'][0]['permanent_url'])
  354. center("HTTPS URL FULL TEXT:")
  355. print(out['outputs'][0]['permanent_url'].replace("lbry://","https://spee.ch/"))
  356. center("Publishing is done successfully!", "bdgr")
  357. center("Confirming publication... It may take a few minutes.")
  358. except:
  359. center("Failed", "bdrd")
  360. center("================= ERROR! ================", "bdrd")
  361. try:
  362. for line in out["message"].split("\n"):
  363. center(line, "bdrd")
  364. except:
  365. print(out)
  366. center("=========================================", "bdrd")
  367. return