Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions data/test_mongo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import requests
from pymongo import MongoClient

# MongoDB Configuration
MONGO_URI = "mongodb://localhost:27017/"
DATABASE_NAME = "main_database"
COLLECTION_NAME = "graphql_data"

# GraphQL API Configuration
GRAPHQL_ENDPOINT = "http://localhost:7263/graphql"
GRAPHQL_QUERY = """
query {
videos {
edges {
node {
id
path
posterPath
width
height
url
posterUrl
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
"""

# No variables needed for this simple query
GRAPHQL_VARIABLES = {}

def fetch_graphql_data(endpoint, query, variables):
"""Fetch data from the GraphQL API."""
try:
headers = {"Content-Type": "application/json"}
payload = {
"query": query,
"variables": variables,
}
response = requests.post(endpoint, json=payload, headers=headers, timeout=10)
response.raise_for_status()
result = response.json()

# Check for GraphQL errors
if "errors" in result:
for error in result["errors"]:
print(f"GraphQL Error: {error['message']}")
raise Exception("GraphQL query returned errors.")

print("Raw GraphQL API Response:", result)
return result.get("data", {})
except requests.exceptions.RequestException as e:
raise Exception(f"Failed to fetch data from GraphQL API: {e}")

def store_data_in_mongodb(data):
"""Store data in MongoDB."""
try:
client = MongoClient(MONGO_URI)
db = client[DATABASE_NAME]
collection = db[COLLECTION_NAME]
if isinstance(data, list):
if data: # Only insert if there's data
collection.insert_many(data)
print(f"Successfully stored {len(data)} documents in MongoDB.")
else:
print("No data to store in MongoDB.")
else:
collection.insert_one(data)
print("Successfully stored 1 document in MongoDB.")
except Exception as e:
raise Exception(f"Failed to store data in MongoDB: {e}")

def main():
"""Main function to fetch data and store it in MongoDB."""
try:
# Fetch data from GraphQL API
print("Fetching data from GraphQL API...")
graphql_data = fetch_graphql_data(GRAPHQL_ENDPOINT, GRAPHQL_QUERY, GRAPHQL_VARIABLES)

# Extract videos from the response using the Relay Connection pattern
videos_connection = graphql_data.get("videos", {})
edges = videos_connection.get("edges", [])
videos = [edge["node"] for edge in edges if "node" in edge]

if not videos:
print("No video data found in the GraphQL response.")
return

# Print summary of found videos
print(f"Found {len(videos)} videos in the GraphQL response.")

# Store data in MongoDB
print("Storing video data in MongoDB...")
store_data_in_mongodb(videos)
print("Fetched and stored data successfully.")

# Print pagination info if available
page_info = videos_connection.get("pageInfo", {})
if page_info.get("hasNextPage"):
print(f"More videos available. End cursor: {page_info.get('endCursor')}")

except Exception as e:
print(f"An error occurred: {e}")

if __name__ == "__main__":
main()