From d0ca96191c10710a3d091d5cdedabe096f4c832b Mon Sep 17 00:00:00 2001
From: Remora
]*>([^<]+)', content, re.IGNORECASE) - if readme_match: - summary += f". {readme_match.group(1)[:100]}" - - elif link_type == "YouTube": - tag = "video" - summary = f"Video: {title}" - if description: - summary += f". {description[:80]}" - - elif link_type == "Reddit": - tag = "discussion" - summary = f"Reddit discussion: {title}" - - elif link_type == "Medium" or link_type == "Dev.to": - tag = "article" - summary = f"Article: {title}" - if description: - summary += f". {description[:80]}" - - elif link_type == "arXiv": - tag = "learning" - summary = f"Research paper: {title}" - - else: - # Generic web article + if response.status_code == 200: + result = response.json() + # Extract the summary from response + summary = result.get("message", "") or result.get("content", "") + if isinstance(summary, list): + summary = summary[0].get("text", "") if summary else "" + summary = summary.strip()[:300] + + logger.info(f" ✓ Got summary from gateway: {summary[:60]}") + + # Determine tag from link type tag = "to-read" - summary = title - if description: - summary += f". {description[:100]}" + if link_type == "GitHub": + tag = "project" + elif link_type == "YouTube": + tag = "video" + elif link_type == "Reddit": + tag = "discussion" + elif link_type in ["Medium", "Dev.to"]: + tag = "article" + elif link_type == "arXiv": + tag = "learning" + + return { + "summary": summary, + "tag": tag, + "relevance": "relevant" + } + else: + logger.warning(f" Gateway error {response.status_code}, falling back to heuristic") + # Fallback: use simple heuristic + return { + "summary": extract_simple_summary(clean_text, title, link_type), + "tag": get_tag_from_type(link_type), + "relevance": "relevant" + } - # Truncate summary to reasonable length - summary = summary[:200] - - logger.info(f" ✓ Analysis complete - Tag: {tag}, Summary: {summary[:60]}") - - result = { - "summary": summary, - "tag": tag, + except requests.Timeout: + logger.warning(f" Gateway timeout, using fallback") + return { + "summary": extract_simple_summary(content, title, link_type), + "tag": get_tag_from_type(link_type), "relevance": "relevant" } - logger.debug(f" Returning: {result}") - return result - except Exception as e: logger.error(f" Analysis error: {e}") import traceback logger.error(traceback.format_exc()) - # Return minimal analysis return { "summary": title, "tag": "interesting", "relevance": "relevant" } +def extract_simple_summary(text, title, link_type): + """Fallback: extract a simple summary from text""" + # Get first non-empty sentence/paragraph + sentences = re.split(r'[.!?]', text) + for sent in sentences: + sent = sent.strip() + if len(sent) > 20 and len(sent) < 300: + return sent[:200] + return title + +def get_tag_from_type(link_type): + """Get tag based on link type""" + tags = { + "GitHub": "project", + "YouTube": "video", + "Reddit": "discussion", + "Medium": "article", + "Dev.to": "article", + "arXiv": "learning", + "Twitter/X": "discussion" + } + return tags.get(link_type, "to-read") + # Send to Tududi inbox def add_to_tududi(title, url, link_type, summary="", tag=""): """Add to Tududi inbox with intelligent summary"""