fetch.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 is responsible for fetching / connecting to the LBRY SDK
  30. # it will take a more complex approach than simply talking to the SDK
  31. # binary directly.
  32. import os
  33. import json
  34. from subprocess import *
  35. from urllib import request, parse
  36. # EXAMPLE USAGE CODE
  37. # Get:
  38. # out = fetch.lbrynet("get", {"uri":url} )
  39. # Resolve:
  40. # out = fetch.lbrynet("resolve", {"urls":[url]} )
  41. def lbrynet(method="", params={}):
  42. # First we will make an attempt to use the request module
  43. try:
  44. # To test the SDK subprocess thing I have this little switch
  45. # 1/0 # Division by zero will trigger and error in this 'try'
  46. # and will skip it, to test the other method.
  47. data = {"method":method,
  48. "params":params}
  49. # The port of the LBRY SDK could be different for each user
  50. # so here I'm trying to solve this issue
  51. sdk_url = "http://localhost:5279"
  52. try:
  53. settings = open(os.path.expanduser('~/.local/share/lbry/lbrynet/daemon_settings.yml'))
  54. for line in settings:
  55. if line.startswith("api:"):
  56. sdk_url = "http://"+line[5:]
  57. except:
  58. pass
  59. data = str(json.dumps(data))
  60. data = data.encode('utf-8')
  61. req = request.Request(sdk_url, data=data)
  62. resp = json.loads(request.urlopen(req).read())
  63. try:
  64. resp = resp["result"]
  65. except:
  66. pass
  67. return resp
  68. except:
  69. print("USING SDK VIA TERMINAL")
  70. # Attempt number 2: Fetch it regularly using the lbrynet via subprocess
  71. method = method.replace("_", " ")
  72. # TODO: make dynamic lbrynet executable
  73. sdk = "flbry/lbrynet"
  74. c = [sdk, method]
  75. # TODO: This is a mess. Things like 'resolve' or 'get' do not want to
  76. # act nicely. And there needs to be logic to translate them into a
  77. # proper ways. I added only 'resolve' and 'get' thus far. We need to
  78. # work with everything.
  79. for i in params:
  80. if type(params[i]) == list:
  81. for b in params[i]:
  82. if method == "resolve" and i == "urls":
  83. c.append(str(b))
  84. else:
  85. c.append("--"+i+"="+str(b))
  86. elif method == "get" and i == "uri":
  87. c.append(str(params[i]))
  88. else:
  89. c.append("--"+i+"="+str(params[i]))
  90. print(c)
  91. out = check_output(c)
  92. try:
  93. out = json.loads(out)
  94. except Exception as e:
  95. out = {}
  96. return out