format_telegram_card.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. """
  3. Format paper information into Telegram-friendly cards
  4. """
  5. import json
  6. import sys
  7. from urllib.parse import quote
  8. def format_paper_card(paper):
  9. """
  10. Format a single paper into a Telegram message card
  11. """
  12. title = paper['title']
  13. authors = ', '.join(paper['authors'][:3]) # First 3 authors
  14. if len(paper['authors']) > 3:
  15. authors += ' et al.'
  16. abstract = paper['abstract'][:500] + '...' if len(paper['abstract']) > 500 else paper['abstract']
  17. # Create tags
  18. tags = ' '.join([f"#{tag.replace('-', '').replace('_', '')[:20]}" for tag in paper.get('tags', [])[:5]])
  19. # Create DOI link
  20. doi_link = paper.get('doi', '')
  21. if doi_link:
  22. doi_url = f"https://doi.org/{doi_link}" if not doi_link.startswith('http') else doi_link
  23. doi_part = f"\n📄 [DOI链接]({doi_url})"
  24. else:
  25. doi_part = f"\n📄 [论文链接]({paper.get('url', '')})"
  26. # Format the card
  27. card = f"""📄 **{title}**
  28. ✍️ {authors}
  29. 📋 **摘要**: {abstract}
  30. 🏷️ **标签**: {tags}{doi_part}
  31. """
  32. return card
  33. def main():
  34. # Read JSON input from stdin
  35. input_text = sys.stdin.read().strip()
  36. if not input_text:
  37. print("No input provided", file=sys.stderr)
  38. sys.exit(1)
  39. try:
  40. papers = json.loads(input_text)
  41. except json.JSONDecodeError as e:
  42. print(f"Invalid JSON input: {e}", file=sys.stderr)
  43. sys.exit(1)
  44. # Format each paper as a card
  45. cards = []
  46. for i, paper in enumerate(papers):
  47. card = format_paper_card(paper)
  48. cards.append(card)
  49. # Combine all cards
  50. daily_digest = f"""🤖 每日AI前沿速递 - {papers[0]['published'][:10] if papers else ''}
  51. {'\n---\n'.join(cards)}
  52. ⏰ 每日定时推送,助您掌握最新研究动态
  53. """
  54. print(daily_digest)
  55. if __name__ == "__main__":
  56. main()