Skip to content

Commit

Permalink
major update: api and recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Hk669 committed Jul 3, 2024
1 parent e359070 commit ba75c76
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 57 deletions.
5 changes: 4 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .search import (
Octokit,
main
)
from .user_data import get_repos
from .api import (
User,
)
from .octokit import Octokit
40 changes: 22 additions & 18 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
from fastapi import FastAPI, HTTPException
import uvicorn
from pydantic import BaseModel
from src.user_data import get_repos
from src.search import main
from chroma.db import recommend
from .user_data import get_repos
from chroma.db import recommend, get_topic_based_recommendations
import asyncio

app = FastAPI()

class User(BaseModel):
username:str
extra_topics: list = []
username: str
extra_topics: list[str] = []
languages: list[str] = []


@app.post('/recommendations/')
async def get_recommendations(user: User) -> dict:
username = user.username
extra_topics = user.extra_topics or []

try:
print(f'Fetching recommendations for {username}')
user_details, language_topics = await get_repos(username)
print(f'--------\n{user_details}')
print(f'--------\n{language_topics}')
unique_repos = await main(language_topics, extra_topics)
print(f'--------\n{unique_repos}')
# urls = recommend(user_details,unique_repos)
return {'recommendations': unique_repos}
if user.username:
# Existing logic for users with GitHub repos
user_details, language_topics = await get_repos(user.username)
if not user_details:
print("No repos found for user")
# Fall back to topic-based recommendations if no repos found
return get_topic_based_recommendations(user)
urls = recommend(user_details)
else:
raise Exception("No username provided")

if not urls:
return {'recommendations': [], 'message': 'No recommendations found'}

return {'recommendations': urls}
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail = 'Error generating recommendatoins')
print(f"Error: {str(e)}")
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")


async def run_server():
Expand Down
30 changes: 30 additions & 0 deletions src/octokit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import asyncio
from datetime import datetime
from typing import Optional, List


class Octokit:
def __init__(self, auth: str, session):
self.auth = auth
self.session = session

async def request(self,
method: str,
url: str,
params: Optional[dict]=None):
headers = {
'Authorization': 'BEARER ' + self.auth,
'Accept': 'application/vnd.github+json',
}

url = 'https://api.github.com' + url

while True:
async with self.session.request(method, url, headers=headers, params=params) as response:
if response.status == 403:
reset_time = datetime.fromtimestamp(int(response.headers["X-RateLimit-Reset"]))
sleep_time = (reset_time - datetime.now()).total_seconds() + 5
await asyncio.sleep(sleep_time)
else:
response.raise_for_status()
return await response.json()
68 changes: 31 additions & 37 deletions src/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,37 @@
from aiohttp import ClientSession
from typing import Optional, List
from dotenv import load_dotenv
from chroma.db import get_or_create_chromadb_collection, upsert_to_chroma_db
from .octokit import Octokit
load_dotenv()

GPAT = os.getenv('GPAT')


class Octokit:
def __init__(self, auth: str, session):
self.auth = auth
self.session = session


async def request(self, method: str, url: str, params: Optional[dict]=None):
headers = {
'Authorization': 'BEARER ' + self.auth,
'Accept': 'application/vnd.github+json',
}

url = 'https://api.github.com' + url

while True:
async with self.session.request(method, url, headers=headers, params=params) as response:
if response.status == 403:
reset_time = datetime.fromtimestamp(int(response.headers["X-RateLimit-Reset"]))
sleep_time = (reset_time - datetime.now()).total_seconds() + 5
await asyncio.sleep(sleep_time)
else:
response.raise_for_status()
return await response.json()


async def search_repositories(octokit: Octokit, params: Optional[dict]):
response = await octokit.request('GET', '/search/repositories', params)
async def search_repositories(octokit: Octokit,
params: Optional[dict]):
unique_repos = {}

try:
response = await octokit.request('GET', '/search/repositories', params)
except Exception as e:
raise Exception(f"Error fetching data: {e}")

while len(response['items']) > 0 and params['page'] <= 3:
for item in response['items']:
if item['id'] not in unique_repos:
language = params["q"].split('language:')[1].split(' ')[0] if 'language:' in params["q"] else ""
topic = params["q"].split('topic:')[1].split(' ')[0] if 'topic:' in params["q"] else ""

if language:
related = language
elif topic:
related = topic
else:
related = "Others"

unique_repos[item['id']] = {
"full_name": item['full_name'],
"description": item['description']
"description": item['description'],
"related_language_or_topic": related,
}

params['page'] += 1
Expand Down Expand Up @@ -105,7 +96,14 @@ async def main(language_topics,
for result in results:
unique_repos.update(result)

print(f"Found {len(unique_repos)} unique repositories")
print(f"Found {len(unique_repos)} unique repositories\n--------")

chroma_db = get_or_create_chromadb_collection()
try:
upsert_to_chroma_db(chroma_db, unique_repos)
except Exception as e:
raise Exception(f"Error upserting data to ChromaDB: {e}")

return unique_repos


Expand All @@ -122,9 +120,5 @@ async def main(language_topics,
end = time.time()
print(f"Time taken: {end - start:.2f} seconds")


for repo_id, repo_info in result.items():
print(f"Repository ID: {repo_id}")
print(f"Full Name: {repo_info['full_name']}")
print(f"Description: {repo_info['description']}")
print("---------------------------")
print('------')
print(result)
3 changes: 2 additions & 1 deletion src/user_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import aiohttp
import asyncio
from src.search import Octokit
from .octokit import Octokit
from datetime import datetime
from aiohttp import ClientSession
from dotenv import load_dotenv
Expand Down Expand Up @@ -36,6 +36,7 @@ async def get_repos(username: str):
user_repo = {
'project_name' : repo['name'],
'description' : repo['description'],
"related_language_or_topic": list(languages_data.keys()),
}
user_details.append(user_repo)

Expand Down

0 comments on commit ba75c76

Please sign in to comment.