search.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. w, h = tsize()
  53. page_size = h - 5
  54. page = 1
  55. while True:
  56. # Printing the search query and page number
  57. center( "SEARCH QUERY: "+args+" PAGE: "+str(page))
  58. out = check_output(["flbry/lbrynet",
  59. "claim", "search", '--text="'+args+'"',
  60. '--page='+str(page),
  61. '--page_size='+str(page_size),
  62. "--no_totals",
  63. '--order_by=release_time'])
  64. # Now we want to parse the json
  65. try:
  66. out = json.loads(out)
  67. except:
  68. print(" Connect to LBRY first.")
  69. return
  70. try:
  71. data_print = {"categories":["Type", "Channel", "Title"],
  72. "size":[1,1,5],
  73. "data":[]}
  74. # List what we found
  75. for n, i in enumerate(out["items"]):
  76. # This will make each new line a different shade
  77. if n % 2:
  78. b = "d"
  79. else:
  80. b = "b"
  81. title = "---!Failed Loading Title---"
  82. ftype = "claim"
  83. bywho = "@Anonymous"
  84. try:
  85. try:
  86. title = i["value"]["title"]
  87. except:
  88. title = i['name']
  89. try:
  90. ftype = what[i["value"]["stream_type"]]
  91. except:
  92. ftype = what[i["value_type"]]
  93. bywho = i["signing_channel"]["name"]
  94. except:
  95. pass
  96. data_print["data"].append([ftype,bywho,title])
  97. table(data_print)
  98. # Tell the user that he might want to load more
  99. center("---type 'more' to load more---")
  100. page = page +1
  101. # Error messages
  102. except Exception as e:
  103. if "code" in out:
  104. print(" Error code: ", out["code"] )
  105. if out["code"] == -32500:
  106. print(" SDK is still starting. Patience!")
  107. else:
  108. print(" Error :", e)
  109. return
  110. # Making sure that we stop every time a new page is reached
  111. c = input(typing_dots())
  112. if c != "more":
  113. break
  114. try:
  115. c = int(c)
  116. except:
  117. return
  118. while True:
  119. url.get(out["items"][c]["canonical_url"])
  120. c = input(typing_dots())
  121. if not c:
  122. break
  123. try:
  124. c = int(c)
  125. except:
  126. return