search.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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="", channel="", tags=[]):
  35. # The user might write the search argument right in the same
  36. # line as the work search.
  37. #
  38. # : search 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(typing_dots("What to search for?"))
  45. # For every tag that is given, turn it into the argument needed for the SDK
  46. for n, i in enumerate(tags):
  47. tags[n] = "--any_tags=" + i
  48. if channel:
  49. channel = ["--channel="+channel]
  50. else:
  51. channel = []
  52. # So we want to request a query to the SDK to search what ever
  53. # the user wants. The problem is it can be a very large output.
  54. # For example the "blender dumbass" query returns 1000 claims
  55. # on the LBRY network. And people will wait for a very long time
  56. # on something that might have a million claims.
  57. # So instead we are going to request only the first 20 and let
  58. # the user load more.
  59. w, h = tsize()
  60. page_size = h - 5
  61. page = 1
  62. while True:
  63. # Printing the search query and page number
  64. center( "SEARCH QUERY: "+args+" PAGE: "+str(page))
  65. out = check_output([lbrynet_binary["b"],
  66. "claim", "search", '--text="'+args+'"',
  67. *tags,
  68. *channel,
  69. '--page='+str(page),
  70. '--page_size='+str(page_size),
  71. "--no_totals",
  72. '--order_by=release_time'])
  73. # Now we want to parse the json
  74. try:
  75. out = json.loads(out)
  76. except:
  77. center("Connect to LBRY first.", "bdrd")
  78. return
  79. try:
  80. data_print = {"categories":["Type", "Channel", "Title"],
  81. "size":[1,1,5],
  82. "data":[]}
  83. # List what we found
  84. for n, i in enumerate(out["items"]):
  85. # This will make each new line a different shade
  86. if n % 2:
  87. b = "d"
  88. else:
  89. b = "b"
  90. title = "---!Failed Loading Title---"
  91. ftype = "claim"
  92. bywho = "[anonymous]"
  93. try:
  94. try:
  95. title = i["value"]["title"]
  96. except:
  97. title = i['name']
  98. try:
  99. ftype = what[i["value"]["stream_type"]]
  100. except:
  101. ftype = what[i["value_type"]]
  102. bywho = i["signing_channel"]["name"]
  103. except:
  104. pass
  105. data_print["data"].append([ftype,bywho,title])
  106. table(data_print)
  107. # Tell the user that they might want to load more
  108. center("---type 'more' to load more---")
  109. page = page +1
  110. # Error messages
  111. except Exception as e:
  112. if "code" in out:
  113. center("Error code: "+out["code"], "bdrd")
  114. if "message" in out:
  115. center("Error: "+out["message"], "bdrd")
  116. else:
  117. center("Error: "+e, "bdrd")
  118. return
  119. while True:
  120. # Making sure that we stop every time a new page is reached
  121. c = input(typing_dots())
  122. if c == "more":
  123. break
  124. try:
  125. c = int(c)
  126. except:
  127. return
  128. url.get(out["items"][c]["canonical_url"])
  129. # Print the list again
  130. table(data_print)
  131. center("---type 'more' to load more---")