use API to get matrix IDs
This commit is contained in:
parent
b112692d9d
commit
cf2ed9c75e
1 changed files with 22 additions and 21 deletions
|
@ -2,8 +2,6 @@ import redis
|
||||||
import json
|
import json
|
||||||
import traceback
|
import traceback
|
||||||
from .launchpad_singleton import get_launchpad
|
from .launchpad_singleton import get_launchpad
|
||||||
import requests
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
|
|
||||||
# Connect to Redis
|
# Connect to Redis
|
||||||
cache = redis.Redis(host='localhost', port=6379, db=0)
|
cache = redis.Redis(host='localhost', port=6379, db=0)
|
||||||
|
@ -15,28 +13,28 @@ def fetch_matrix_accounts(profile_id):
|
||||||
if cached_result:
|
if cached_result:
|
||||||
return json.loads(cached_result)
|
return json.loads(cached_result)
|
||||||
|
|
||||||
# Fetch the page
|
# Fetch Launchpad API data
|
||||||
url = "https://launchpad.net/~" + profile_id
|
launchpad = get_launchpad()
|
||||||
response = requests.get(url)
|
person = launchpad.people[profile_id]
|
||||||
page_content = response.content
|
|
||||||
|
|
||||||
# Parse the page content
|
# Fetch social accounts by platform (Matrix)
|
||||||
soup = BeautifulSoup(page_content, 'html.parser')
|
matrix_accounts = person.getSocialAccountsByPlatform(platform='Matrix platform')
|
||||||
|
|
||||||
# Locate the elements containing the Matrix IDs
|
|
||||||
matrix_ids_elements = soup.find_all('dd', class_='user-social-accounts__item matrix-account')
|
|
||||||
|
|
||||||
# Extract the Matrix IDs
|
# Extract the Matrix IDs
|
||||||
matrix_ids = []
|
matrix_ids = []
|
||||||
for element in matrix_ids_elements:
|
for account in matrix_accounts:
|
||||||
matrix_id = element.find('a').text.strip()
|
username = account['identity']['username']
|
||||||
|
homeserver = account['identity']['homeserver']
|
||||||
|
matrix_id = f"@{username}:{homeserver}"
|
||||||
matrix_ids.append(matrix_id)
|
matrix_ids.append(matrix_id)
|
||||||
|
|
||||||
# Cache the result with expiration time of 30 minutes (1800 seconds)
|
# Cache the result with expiration time of 30 minutes (1800 seconds)
|
||||||
cache.setex(f"matrix_{profile_id}", 1800, json.dumps(matrix_ids))
|
cache.setex(f"matrix_{profile_id}", 1800, json.dumps(matrix_ids))
|
||||||
|
|
||||||
return matrix_ids
|
return matrix_ids
|
||||||
|
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
print(f"Profule with name {profile_id} was not found. Error: {e}")
|
print(f"Profile with name {profile_id} was not found. Error: {e}")
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
return False
|
return False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -52,18 +50,21 @@ def fetch_group_members(group_name, recurse=False):
|
||||||
if cached_result:
|
if cached_result:
|
||||||
return json.loads(cached_result)
|
return json.loads(cached_result)
|
||||||
|
|
||||||
|
# Fetch the group from the Launchpad API
|
||||||
launchpad = get_launchpad()
|
launchpad = get_launchpad()
|
||||||
group = launchpad.people[group_name]
|
group = launchpad.people[group_name]
|
||||||
|
|
||||||
group_members = set()
|
group_members = set()
|
||||||
for person in group.members:
|
for person in group.members:
|
||||||
print(person)
|
|
||||||
if not person.is_team:
|
if not person.is_team:
|
||||||
group_members.add(person.name)
|
group_members.add(person.name)
|
||||||
continue
|
# Optional recursion to get sub-teams' members
|
||||||
|
elif recurse:
|
||||||
|
sub_group_members = fetch_group_members(person.name, recurse=True)
|
||||||
|
group_members.update(sub_group_members['group_members'])
|
||||||
|
|
||||||
# MXIDs should be generated for individuals only
|
# Generate MXIDs for individual members
|
||||||
mxids = ['@' + member + ':ubuntu.com' for member in group_members]
|
mxids = [f"@{member}:ubuntu.com" for member in group_members]
|
||||||
result = {'group_members': tuple(group_members), 'group_name': group_name, 'mxids': mxids}
|
result = {'group_members': tuple(group_members), 'group_name': group_name, 'mxids': mxids}
|
||||||
|
|
||||||
# Cache the result with expiration time of 30 minutes (1800 seconds)
|
# Cache the result with expiration time of 30 minutes (1800 seconds)
|
||||||
|
|
Loading…
Reference in a new issue