You know how many servers are federated with your Pleroma server but you don't know much about them. What software are they running? How many users? Are they still alive? These Python scripts will help you to get all that information and much more! https://libretux.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.5 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import getpass
  4. from mastodon import Mastodon
  5. from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError, MastodonIllegalArgumentError
  6. import fileinput,re
  7. import os
  8. import sys
  9. def create_dir():
  10. if not os.path.exists('secrets'):
  11. os.makedirs('secrets')
  12. def create_file():
  13. if not os.path.exists('secrets/secrets.txt'):
  14. with open('secrets/secrets.txt', 'w'): pass
  15. print(secrets_filepath + " created!")
  16. def create_config():
  17. if not os.path.exists(config_filepath):
  18. print(config_filepath + " created!")
  19. with open('config.txt', 'w'): pass
  20. def write_params():
  21. with open(secrets_filepath, 'a') as the_file:
  22. print("Writing secrets parameter names to " + secrets_filepath)
  23. the_file.write('uc_client_id: \n'+'uc_client_secret: \n'+'uc_access_token: \n')
  24. def write_config():
  25. with open(config_filepath, 'a') as the_file:
  26. the_file.write('pleroma_hostname: \n')
  27. print("adding parameter name 'pleroma_hostname' to "+ config_filepath)
  28. def read_client_lines(self):
  29. client_path = 'app_clientcred.txt'
  30. with open(client_path) as fp:
  31. line = fp.readline()
  32. cnt = 1
  33. while line:
  34. if cnt == 1:
  35. print("Writing client id to " + secrets_filepath)
  36. modify_file(secrets_filepath, "uc_client_id: ", value=line.rstrip())
  37. elif cnt == 2:
  38. print("Writing client secret to " + secrets_filepath)
  39. modify_file(secrets_filepath, "uc_client_secret: ", value=line.rstrip())
  40. line = fp.readline()
  41. cnt += 1
  42. def read_token_line(self):
  43. token_path = 'app_usercred.txt'
  44. with open(token_path) as fp:
  45. line = fp.readline()
  46. print("Writing access token to " + secrets_filepath)
  47. modify_file(secrets_filepath, "uc_access_token: ", value=line.rstrip())
  48. def read_config_line():
  49. with open(config_filepath) as fp:
  50. line = fp.readline()
  51. modify_file(config_filepath, "pleroma_hostname: ", value=hostname)
  52. def log_in():
  53. error = 0
  54. try:
  55. global hostname
  56. hostname = input("Enter Pleroma hostname: ")
  57. user_name = input("User name, ex. user@" + hostname +"? ")
  58. user_password = getpass.getpass("User password? ")
  59. app_name = input("This app name? ")
  60. Mastodon.create_app(app_name, scopes=["read","write"],
  61. to_file="app_clientcred.txt", api_base_url=hostname)
  62. mastodon = Mastodon(client_id = "app_clientcred.txt", api_base_url = hostname)
  63. mastodon.log_in(
  64. user_name,
  65. user_password,
  66. scopes = ["read", "write"],
  67. to_file = "app_usercred.txt"
  68. )
  69. except MastodonIllegalArgumentError as i_error:
  70. error = 1
  71. if os.path.exists("secrets/secrets.txt"):
  72. print("Removing secrets/secrets.txt file..")
  73. os.remove("secrets/secrets.txt")
  74. if os.path.exists("app_clientcred.txt"):
  75. print("Removing app_clientcred.txt file..")
  76. os.remove("app_clientcred.txt")
  77. sys.exit(i_error)
  78. except MastodonNetworkError as n_error:
  79. error = 1
  80. if os.path.exists("secrets/secrets.txt"):
  81. print("Removing secrets/secrets.txt file..")
  82. os.remove("secrets/secrets.txt")
  83. if os.path.exists("app_clientcred.txt"):
  84. print("Removing app_clientcred.txt file..")
  85. os.remove("app_clientcred.txt")
  86. sys.exit(n_error)
  87. except MastodonReadTimeout as r_error:
  88. error = 1
  89. if os.path.exists("secrets/secrets.txt"):
  90. print("Removing secrets/secrets.txt file..")
  91. os.remove("secrets/secrets.txt")
  92. if os.path.exists("app_clientcred.txt"):
  93. print("Removing app_clientcred.txt file..")
  94. os.remove("app_clientcred.txt")
  95. sys.exit(r_error)
  96. except MastodonAPIError as a_error:
  97. error = 1
  98. if os.path.exists("secrets/secrets.txt"):
  99. print("Removing secrets/secrets.txt file..")
  100. os.remove("secrets/secrets.txt")
  101. if os.path.exists("app_clientcred.txt"):
  102. print("Removing app_clientcred.txt file..")
  103. os.remove("app_clientcred.txt")
  104. sys.exit(a_error)
  105. finally:
  106. if error == 0:
  107. create_dir()
  108. create_file()
  109. #Your client id and secret are the two lines in `app_clientcred.txt`, your access
  110. #token is the line in `app_usercred.txt`.
  111. write_params()
  112. client_path = 'app_clientcred.txt'
  113. read_client_lines(client_path)
  114. token_path = 'app_usercred.txt'
  115. read_token_line(token_path)
  116. if os.path.exists("app_clientcred.txt"):
  117. print("Removing app_clientcred.txt temp file..")
  118. os.remove("app_clientcred.txt")
  119. if os.path.exists("app_usercred.txt"):
  120. print("Removing app_usercred.txt temp file..")
  121. os.remove("app_usercred.txt")
  122. print("Secrets setup done!\n")
  123. print("\n")
  124. print("Now you can run pleroma-fed.py!")
  125. def modify_file(file_name,pattern,value=""):
  126. fh=fileinput.input(file_name,inplace=True)
  127. for line in fh:
  128. replacement=pattern + value
  129. line=re.sub(pattern,replacement,line)
  130. sys.stdout.write(line)
  131. fh.close()
  132. # Returns the parameter from the specified file
  133. def get_parameter( parameter, file_path ):
  134. # Check if secrets file exists
  135. if not os.path.isfile(file_path):
  136. print("File %s not found, creating it."%file_path)
  137. log_in()
  138. # Find parameter in file
  139. with open( file_path ) as f:
  140. for line in f:
  141. if line.startswith( parameter ):
  142. return line.replace(parameter + ":", "").strip()
  143. # Cannot find parameter, exit
  144. print(file_path + " Missing parameter %s "%parameter)
  145. sys.exit(0)
  146. # Returns the parameter from the specified file
  147. def get_hostname( parameter, config_filepath ):
  148. # Check if secrets file exists
  149. if not os.path.isfile(config_filepath):
  150. print("File %s not found, creating it."%config_filepath)
  151. create_config()
  152. # Find parameter in file
  153. with open( config_filepath ) as f:
  154. for line in f:
  155. if line.startswith( parameter ):
  156. return line.replace(parameter + ":", "").strip()
  157. # Cannot find parameter, exit
  158. print(config_filepath + " Missing parameter %s "%parameter)
  159. write_config()
  160. read_config_line()
  161. print("hostname setup done!")
  162. sys.exit(0)
  163. # Load secrets from secrets file
  164. secrets_filepath = "secrets/secrets.txt"
  165. uc_client_id = get_parameter("uc_client_id", secrets_filepath)
  166. uc_client_secret = get_parameter("uc_client_secret", secrets_filepath)
  167. uc_access_token = get_parameter("uc_access_token", secrets_filepath)
  168. # Load configuration from config file
  169. config_filepath = "config.txt"
  170. pleroma_hostname = get_hostname("pleroma_hostname", config_filepath) # E.g., pleroma.site