search.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(" Searching for :: ")
  45. # So we want to request a query to the SDK to search what ever
  46. # the user wants. The problem is it can be a very large output.
  47. # For example the "blender dumbass" query returns 1000 claims
  48. # on the LBRY network. And people will wait for a very long time
  49. # on something that might have a million claims.
  50. # So instead we are going to request only the first 20 and let
  51. # the user load more.
  52. page_size = 20
  53. page = 1
  54. while True:
  55. # Printing the search query and page number
  56. print(" "+clr["bold"]+clr["bdma"], "SEARCH QUERY: ",
  57. wdth(args, 20), " PAGE :",
  58. wdth(page, 3),
  59. wdth(" ", 32)+clr["norm"])
  60. out = check_output(["flbry/lbrynet",
  61. "claim", "search", '--text="'+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. # Print the categories
  73. print(" "+clr["bold"]+clr["bdma"],wdth(" ", 3),
  74. "", wdth("TYPE", 8),
  75. "", wdth("CHANNEL", 20),
  76. "", wdth("TITLE", 20)+wdth(" ", 31)+clr["norm"])
  77. try:
  78. # List what we found
  79. for n, i in enumerate(out["items"]):
  80. # This will make each new line a different shade
  81. if n % 2:
  82. b = "d"
  83. else:
  84. b = "b"
  85. title = "---!Failed Loading Title---"
  86. ftype = "claim"
  87. bywho = "@Anonymous"
  88. try:
  89. try:
  90. title = i["value"]["title"]
  91. except:
  92. title = i['name']
  93. ftype = i["value_type"]
  94. bywho = i["signing_channel"]["name"]
  95. except:
  96. pass
  97. print(" "+clr["bold"]+clr["b"+b+"ma"],wdth(n, 3) ,
  98. clr["norm"]+clr["b"+b+"bu"]+clr["ital"],wdth(what[ftype], 8),
  99. clr["norm"]+clr["bold"]+clr["b"+b+"bu"], wdth(bywho, 20),
  100. clr["norm"]+clr["tbwh"]+clr["b"+b+"bu"], wdth(title, 50), clr["norm"])
  101. # Tell the user that he might want to load more
  102. print(" "+clr["bold"]+clr["bdma"],
  103. " ---type more to load more---",
  104. wdth(" ",40)+clr["norm"])
  105. page = page +1
  106. # Error messages
  107. except Exception as e:
  108. if "code" in out:
  109. print(" Error code: ", out["code"] )
  110. if out["code"] == -32500:
  111. print(" SDK is still starting. Patience!")
  112. else:
  113. print(" Error :", e)
  114. return
  115. # Making sure that we stop every time a new page is reached
  116. c = input(typing_dots())
  117. if c != "more":
  118. break
  119. try:
  120. c = int(c)
  121. except:
  122. return
  123. while True:
  124. url.get(out["items"][c]["canonical_url"])
  125. c = input(typing_dots())
  126. if not c:
  127. break
  128. try:
  129. c = int(c)
  130. except:
  131. return