channel.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 perform a simple search on the LBRY network.
  30. from subprocess import *
  31. import json
  32. from flbry import url
  33. from flbry.variables import *
  34. def simple(args=""):
  35. # The user might write the search argument right in the same
  36. # line as the work search.
  37. #
  38. # : seach blenderdumbass
  39. #
  40. # Or they can type nothing. And be confused of what happened.
  41. # So I want to provide a catcher here. If they type nothing it
  42. # will ask them to provide a search query.
  43. if not args:
  44. args = input(" Channel url :: ")
  45. if not args.startswith("@") and not args.startswith("lbry://@"):
  46. args = "@"+args
  47. # So we want to request a query to the SDK to search what ever
  48. # the user wants. The problem is it can be a very large output.
  49. # For example the "blender dumbass" query returns 1000 claims
  50. # on the LBRY network. And people will wait for a very long time
  51. # on something that might have a million claims.
  52. # So instead we are going to request only the first 20 and let
  53. # the user load more.
  54. w, h = tsize()
  55. page_size = h - 5
  56. page = 1
  57. while True:
  58. # Printing the search query and page number
  59. center("CHANNEL: "+args+" PAGE:"+str(page))
  60. out = check_output(["flbry/lbrynet",
  61. "claim", "search", '--channel='+args,
  62. '--page='+str(page),
  63. '--page_size='+str(page_size),
  64. "--no_totals",
  65. '--order_by=release_time'])
  66. # Now we want to parse the json
  67. try:
  68. out = json.loads(out)
  69. except:
  70. print(" Connect to LBRY first.")
  71. return
  72. try:
  73. data_print = {"categories":["Type", "Title"],
  74. "size":[1,5],
  75. "data":[]}
  76. # List what we found
  77. for n, i in enumerate(out["items"]):
  78. title = "---!Failed Loading Title---"
  79. ftype = "claim"
  80. try:
  81. try:
  82. title = i["value"]["title"]
  83. except:
  84. title = i['name']
  85. try:
  86. ftype = what[i["value"]["stream_type"]]
  87. except:
  88. ftype = what[i["value_type"]]
  89. except:
  90. pass
  91. data_print["data"].append([ftype, title])
  92. table(data_print)
  93. # Tell the user that he might want to load more
  94. center("---type 'more' to load more---")
  95. page = page +1
  96. # Error messages
  97. except Exception as e:
  98. if "code" in out:
  99. print(" Error code: ", out["code"] )
  100. if out["code"] == -32500:
  101. print(" SDK is still starting. Patience!")
  102. else:
  103. print(" Error :", e)
  104. return
  105. # Making sure that we stop every time a new page is reached
  106. c = input(typing_dots())
  107. if c != "more":
  108. break
  109. try:
  110. c = int(c)
  111. except:
  112. return
  113. while True:
  114. url.get(out["items"][c]["canonical_url"])
  115. c = input(typing_dots())
  116. if not c:
  117. break
  118. try:
  119. c = int(c)
  120. except:
  121. return
  122. def select(message="", claim_id=False):
  123. # This fucntion will give users to select one of their channels.
  124. center(message)
  125. out = check_output(["flbry/lbrynet",
  126. "channel", "list"])
  127. # Now we want to parse the json
  128. try:
  129. out = json.loads(out)
  130. except:
  131. print(" Connect to LBRY first.")
  132. return
  133. d = {"categories":["lbry url", "title"],
  134. "size":[1,2],
  135. "data":[]}
  136. for n, i in enumerate(out["items"]):
  137. d["data"].append([i["name"], i["value"]["title"]])
  138. table(d)
  139. center("select a channel by typing it's number")
  140. select = input(typing_dots())
  141. try:
  142. select = int(select)
  143. if claim_id:
  144. return out["items"][select]["name"], out["items"][select]["claim_id"]
  145. return out["items"][select]["name"]
  146. except:
  147. if claim_id:
  148. return out["items"][0]["name"], out["items"][0]["claim_id"]
  149. return out["items"][0]["name"]