Advertisement
Guest User

trustworthy_resolved

a guest
Aug 27th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import json
  2. import os
  3. import requests
  4. supbase_url = "https://pxidrgkatumlvfqaxcll.supabase.co/rest/v1/"
  5. supbase_anon_api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InB4aWRyZ2thdHVtbHZmcWF4Y2xsIiwicm9sZSI6ImFub24iLCJpYXQiOjE2Njg5OTUzOTgsImV4cCI6MTk4NDU3MTM5OH0.d_yYtASLzAoIIGdXUBIgRAGLBnNow7JG2SoaNMQ8ySg"
  6.  
  7. def gen_dict_extract(key, var):     #https://stackoverflow.com/questions/9807634/find-all-occurrences-of-a-key-in-nested-dictionaries-and-lists
  8.     if hasattr(var,'items'):
  9.         for k, v in var.items():
  10.             if k == key:
  11.                 yield v
  12.             if isinstance(v, dict):
  13.                 for result in gen_dict_extract(key, v):
  14.                     yield result
  15.             elif isinstance(v, list):
  16.                 for d in v:
  17.                     for result in gen_dict_extract(key, d):
  18.                         yield result
  19.  
  20. trustworthy_usernames = [
  21.   'Manifold',
  22.   'memestiny',
  23.   'BTE',
  24.   'jack',
  25.   'Yev',
  26.   'itsTomekK',
  27.   'MattP',
  28.   'egroj',
  29.   'dreev',
  30.   'MartinRandall',
  31.   'LivInTheLookingGlass',
  32.   'LarsDoucet',
  33.   'Conflux',
  34.   'NcyRocks',
  35.   'MichaelWheatley',
  36.   'dglid',
  37.   'yaboi69',
  38.   'TheSkeward',
  39.   'Duncn',
  40.   'a',
  41.   'NuñoSempere',
  42.   'CarsonGale',
  43.   'Tetraspace',
  44.   'BoltonBailey',
  45.   'MatthewBarnett',
  46.   'Jacy',
  47.   'Gabrielle',
  48.   'KatjaGrace',
  49.   'AndrewG',
  50.   'MarcusAbramovitch',
  51.   'KevinBurke',
  52.   'PeterWildeford',
  53.   'ScottLawrence',
  54.   'NoaNabeshima',
  55.   'Mira',
  56.   'evergreenemily',
  57.   'SneakySly',
  58.   'IsaacKing'       # added
  59. ]
  60.  
  61. def load_users():
  62.     local_file = f"buffer/users.json"
  63.     if os.path.isfile(local_file):
  64.         with (open(local_file, encoding="utf-8") as json_file):
  65.             return json.load(json_file)
  66.     assembled_url = supbase_url + (f"users"
  67.                                    f"?select=*"
  68.                                    f"&apikey={supbase_anon_api_key}")
  69.     loaded_users = requests.get(assembled_url).json()
  70.     with open(local_file, 'w', encoding="utf-8") as outfile:
  71.         json.dump(loaded_users, outfile)
  72.     return loaded_users
  73. users = load_users()
  74.  
  75. def get_user_by_username(username):
  76.     return [x for x in users if x["username"] == username][0]
  77.  
  78. def get_trustworthy_users():
  79.     return [x for x in users if x["username"] in trustworthy_usernames]
  80.  
  81. def get_user_by_id(user_id):
  82.     return [x for x in users if x["id"] == user_id][0]
  83.  
  84. def get_username_by_id(user_id):
  85.     return get_user_by_id(user_id)["username"]
  86.  
  87. def get_comments_by_user_id(user_id):
  88.     local_file = f"buffer/{user_id}_comments.json"
  89.     if os.path.isfile(local_file):
  90.         with (open(local_file, encoding="utf-8") as json_file):
  91.             return json.load(json_file)
  92.     assembled_url = supbase_url + (f"contract_comments"
  93.                                    f"?select=*"
  94.                                    f"&user_id=eq.{user_id}"
  95.                                    f"&apikey={supbase_anon_api_key}")
  96.     loaded_comments = requests.get(assembled_url).json()
  97.     with open(local_file, 'w', encoding="utf-8") as outfile:
  98.         json.dump(loaded_comments, outfile)
  99.     return loaded_comments
  100.  
  101. search_string = "unresolved markets policy"
  102. def find_valid_comments():
  103.     total_valid = 0
  104.     trustworthy_users = get_trustworthy_users()
  105.     for trustworthy_user in trustworthy_users:
  106.         user_comments = get_comments_by_user_id(trustworthy_user["id"])
  107.         comment_texts = [" ".join(gen_dict_extract("text", x["data"]["content"])) for x in user_comments if "data" in x and "content" in x["data"]]
  108.         valid_comments = [x for x in comment_texts if search_string in x]
  109.         print(f"Tound {len(valid_comments)} by {trustworthy_user['username']}.")
  110.         total_valid += len(valid_comments)
  111.     print(f"In total {total_valid}.")
  112.  
  113. if __name__ == '__main__':
  114.     find_valid_comments()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement