add support to get matrix IDs from launchpad
This commit is contained in:
parent
02c12247ee
commit
dcac0c28c9
4 changed files with 57 additions and 2 deletions
|
@ -70,3 +70,4 @@ wadllib==1.3.6
|
||||||
wcwidth==0.2.13
|
wcwidth==0.2.13
|
||||||
yarl==1.9.4
|
yarl==1.9.4
|
||||||
zipp==1.0.0
|
zipp==1.0.0
|
||||||
|
bs4
|
||||||
|
|
|
@ -3,5 +3,6 @@ from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
#path('', views.list_facts, name='facts-list'),
|
#path('', views.list_facts, name='facts-list'),
|
||||||
path('api/groups/members/<str:group_name>/', views.group_members, name='get_group_members')
|
path('api/groups/members/<str:group_name>/', views.group_members, name='get_group_members'),
|
||||||
|
path('api/people/<str:profile_id>/socials/matrix', views.matrix_profiles, name='get_matrix_accounts')
|
||||||
]
|
]
|
||||||
|
|
|
@ -2,10 +2,47 @@ 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)
|
||||||
|
|
||||||
|
def fetch_matrix_accounts(profile_id):
|
||||||
|
try:
|
||||||
|
#Try to fetch from cache first
|
||||||
|
cached_result = cache.get(f"matrix_{profile_id}")
|
||||||
|
if cached_result:
|
||||||
|
return json.loads(cached_result)
|
||||||
|
|
||||||
|
# Fetch the page
|
||||||
|
url = "https://launchpad.net/~" + profile_id
|
||||||
|
response = requests.get(url)
|
||||||
|
page_content = response.content
|
||||||
|
|
||||||
|
# Parse the page content
|
||||||
|
soup = BeautifulSoup(page_content, 'html.parser')
|
||||||
|
|
||||||
|
# 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
|
||||||
|
matrix_ids = []
|
||||||
|
for element in matrix_ids_elements:
|
||||||
|
matrix_id = element.find('a').text.strip()
|
||||||
|
matrix_ids.append(matrix_id)
|
||||||
|
# Cache the result with expiration time of 30 minutes (1800 seconds)
|
||||||
|
cache.setex(f"matrix_{profile_id}", 1800, json.dumps(matrix_ids))
|
||||||
|
|
||||||
|
except KeyError as e:
|
||||||
|
print(f"Profule with name {profile_id} was not found. Error: {e}")
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred: {e}")
|
||||||
|
print(traceback.format_exc())
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def fetch_group_members(group_name, recurse=False):
|
def fetch_group_members(group_name, recurse=False):
|
||||||
try:
|
try:
|
||||||
|
@ -25,7 +62,6 @@ def fetch_group_members(group_name, recurse=False):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# MXIDs should be generated for individuals only
|
# MXIDs should be generated for individuals only
|
||||||
print(group_members)
|
|
||||||
mxids = ['@' + member + ':ubuntu.com' for member in group_members]
|
mxids = ['@' + 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}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ from datetime import datetime
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from .launchpad_singleton import get_launchpad
|
from .launchpad_singleton import get_launchpad
|
||||||
from .utils import fetch_group_members # Adjust the import path as necessary
|
from .utils import fetch_group_members # Adjust the import path as necessary
|
||||||
|
from .utils import fetch_matrix_accounts # Adjust the import path as necessary
|
||||||
import pytz
|
import pytz
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
@ -29,3 +30,19 @@ def group_members(self, group_name):
|
||||||
print(f"Error processing request for launchpad group {group_name}: {str(e)}")
|
print(f"Error processing request for launchpad group {group_name}: {str(e)}")
|
||||||
return Response({'error': 'An error occurred processing your request'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({'error': 'An error occurred processing your request'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
@api_view(['GET'])
|
||||||
|
#@cache_page(60 * 30) # Cache for 30 minutes
|
||||||
|
def matrix_profiles(self, profile_id):
|
||||||
|
try:
|
||||||
|
result = fetch_matrix_accounts(profile_id)
|
||||||
|
return Response(result)
|
||||||
|
except KeyError as e:
|
||||||
|
# Handle the case where the bug is not found
|
||||||
|
print(f"Profile with name {profile_id} was not found. Error: {e}")
|
||||||
|
return Response({'error': 'Group not found'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
except Exception as e:
|
||||||
|
# Handle other potential exceptions
|
||||||
|
print(f"An error occurred: {e}")
|
||||||
|
print(f"Error processing request for launchpad profile {profile_id}: {str(e)}")
|
||||||
|
return Response({'error': 'An error occurred processing your request'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
Loading…
Reference in a new issue