(request: DocumentRequest, current_user: User = Depends(get_current_user))
| 112 | tags=["Documents"] |
| 113 | ) |
| 114 | async def add_document(request: DocumentRequest, current_user: User = Depends(get_current_user)): |
| 115 | |
| 116 | headers = { |
| 117 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134" |
| 118 | } |
| 119 | # Assuming you have a Neo4j driver instance |
| 120 | driver = GraphDatabase.driver(AppConfig.NEO4J_URI, auth=(AppConfig.NEO4J_USER, AppConfig.NEO4J_PASSWORD)) |
| 121 | |
| 122 | logging.basicConfig(level=logging.INFO) |
| 123 | |
| 124 | logging.info(f"Fetching document from {request.url}") |
| 125 | url_str = str(request.url) |
| 126 | logging.info(f"Fetching document from {url_str}") |
| 127 | |
| 128 | async with httpx.AsyncClient() as client: |
| 129 | response = await client.get(url_str, headers=headers) |
| 130 | if response.status_code != 200: |
| 131 | logging.error(f"Failed to fetch document from {url_str}: Status {response.status_code}") |
| 132 | raise HTTPException(status_code=400, detail=f"Could not fetch document from {url_str}") |
| 133 | |
| 134 | soup = BeautifulSoup(response.content, 'html.parser') |
| 135 | |
| 136 | |
| 137 | documentId=str(uuid.uuid4()) |
| 138 | title = extract_title(soup, documentId) |
| 139 | text = extract_full_text(soup) |
| 140 | imageurl = extract_primary_image(soup) |
| 141 | publisher = extract_publisher(soup, url_str) |
| 142 | thumbnail = extract_thumbnail(soup) |
| 143 | wordcount = len(text.split()) |
| 144 | note=request.note |
| 145 | logging.info(f"Document {documentId} has {wordcount} words") |
| 146 | utc_now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M') + 'Z' # No seconds or microseconds, append 'Z' for UTC |
| 147 | |
| 148 | query = """ |
| 149 | CREATE (d:Document { |
| 150 | uuid: $uuid, |
| 151 | name: $name, |
| 152 | url: $url, |
| 153 | text: $text, |
| 154 | note: $note, |
| 155 | imageurl: $imageurl, |
| 156 | publisher: $publisher, |
| 157 | addeddate: $addeddate, |
| 158 | thumbnail: $thumbnail, |
| 159 | wordcount: $wordcount, |
| 160 | type: "Document" |
| 161 | }) |
| 162 | with d |
| 163 | MATCH (u:User {uuid: $useruuid}) |
| 164 | MERGE (ua:UserAction {useruuid: u.uuid}) |
| 165 | ON CREATE SET ua.name = u.username, ua.uuid=randomUUID() |
| 166 | MERGE (u)-[r:HAS_ACTION]->(ua) |
| 167 | MERGE (ua)-[:ADDED]-(d) set r.dateadded= datetime() |
| 168 | """ |
| 169 | with driver.session() as session: |
| 170 | def create_document(tx: Transaction): |
| 171 | return tx.run(query, { |
nothing calls this directly
no test coverage detected