This code gets all peers from running Mastodon, Pleroma or Lemmy host server and then all peers from host server's peers. Goal is to collect maximum number of alive fediverse's servers by querying their API and then post servers and registered users.
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.

160 lines
4.4 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import getpass
  4. import os
  5. import sys
  6. from mastodon import Mastodon
  7. from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError
  8. import psycopg2
  9. from psycopg2 import sql
  10. from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  11. # Returns the parameter from the specified file
  12. def get_parameter( parameter, file_path ):
  13. # Check if secrets file exists
  14. if not os.path.isfile(file_path):
  15. print("File %s not found, asking."%file_path)
  16. write_parameter( parameter, file_path )
  17. #sys.exit(0)
  18. # Find parameter in file
  19. with open( file_path ) as f:
  20. for line in f:
  21. if line.startswith( parameter ):
  22. return line.replace(parameter + ":", "").strip()
  23. # Cannot find parameter, exit
  24. print(file_path + " Missing parameter %s "%parameter)
  25. sys.exit(0)
  26. def write_parameter( parameter, file_path ):
  27. if not os.path.exists('config'):
  28. os.makedirs('config')
  29. print("Setting up fediverse parameters...")
  30. print("\n")
  31. fediverse_db = input("fediverse db name: ")
  32. fediverse_db_user = input("fediverse db user: ")
  33. with open(file_path, "w") as text_file:
  34. print("fediverse_db: {}".format(fediverse_db), file=text_file)
  35. print("fediverse_db_user: {}".format(fediverse_db_user), file=text_file)
  36. def create_table(db, db_user, table, sql):
  37. conn = None
  38. try:
  39. conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432")
  40. cur = conn.cursor()
  41. print("Creating table.. "+table)
  42. # Create the table in PostgreSQL database
  43. cur.execute(sql)
  44. conn.commit()
  45. print("Table "+table+" created!")
  46. print("\n")
  47. except (Exception, psycopg2.DatabaseError) as error:
  48. print(error)
  49. finally:
  50. if conn is not None:
  51. conn.close()
  52. #############################################################################################
  53. # Load configuration from config file
  54. config_filepath = "config/db_config.txt"
  55. fediverse_db = get_parameter("fediverse_db", config_filepath)
  56. fediverse_db_user = get_parameter("fediverse_db_user", config_filepath)
  57. ############################################################
  58. # create database
  59. ############################################################
  60. conn = None
  61. try:
  62. conn = psycopg2.connect(dbname='postgres',
  63. user=fediverse_db_user, host='',
  64. password='')
  65. conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  66. cur = conn.cursor()
  67. print("Creating database " + fediverse_db + ". Please wait...")
  68. cur.execute(sql.SQL("CREATE DATABASE {}").format(
  69. sql.Identifier(fediverse_db))
  70. )
  71. print("Database " + fediverse_db + " created!")
  72. except (Exception, psycopg2.DatabaseError) as error:
  73. print(error)
  74. finally:
  75. if conn is not None:
  76. conn.close()
  77. #############################################################################################
  78. try:
  79. conn = None
  80. conn = psycopg2.connect(database = fediverse_db, user = fediverse_db_user, password = "", host = "/var/run/postgresql", port = "5432")
  81. except (Exception, psycopg2.DatabaseError) as error:
  82. print(error)
  83. # Load configuration from config file
  84. os.remove("db_config.txt")
  85. print("Exiting. Run db-setup again with right parameters")
  86. sys.exit(0)
  87. if conn is not None:
  88. print("\n")
  89. print("fediverse parameters saved to db-config.txt!")
  90. print("\n")
  91. ############################################################
  92. # Create needed tables
  93. ############################################################
  94. print("Creating table...")
  95. ########################################
  96. db = fediverse_db
  97. db_user = fediverse_db_user
  98. table = "world"
  99. sql = "create table "+table+" (server varchar(200) PRIMARY KEY, federated_with varchar(200), updated_at timestamptz, saved_at timestamptz), checked boolean"
  100. create_table(db, db_user, table, sql)
  101. table = "fediverse"
  102. sql = "create table "+table+" (server varchar(200) PRIMARY KEY, users INT, updated_at timestamptz, software varchar(50), version varchar(100))"
  103. create_table(db, db_user, table, sql)
  104. table = "totals"
  105. sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, total_servers INT, total_users INT)"
  106. create_table(db, db_user, table, sql)
  107. table = "evo"
  108. sql = "create table "+table+" (datetime timestamptz PRIMARY KEY, servers INT, users INT)"
  109. create_table(db, db_user, table, sql)
  110. #####################################
  111. print("Done!")
  112. print("Now you can run setup.py!")
  113. print("\n")