File size: 1,853 Bytes
e6b89d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import axios from 'axios';
import { XMLParser } from 'fast-xml-parser';

export interface ArxivPaper {
  id: string;
  title: string;
  summary: string;
  authors: string[];
  published: string;
  updated: string;
  link: string;
  pdfUrl: string;
}

const parser = new XMLParser();

/**
 * Fetches paper metadata from Arxiv API.
 * Supports both direct Arxiv IDs and search queries.
 */
export async function fetchArxivPaper(query: string): Promise<ArxivPaper | null> {
  try {
    let id = '';
    
    // Extract ID if it's a URL
    if (query.includes('arxiv.org/abs/')) {
      id = query.split('arxiv.org/abs/')[1].split('?')[0];
    } else if (query.includes('arxiv.org/pdf/')) {
      id = query.split('arxiv.org/pdf/')[1].replace('.pdf', '').split('?')[0];
    } else if (/^\d{4}\.\d{4,5}$/.test(query)) {
      id = query;
    }

    const apiUrl = id 
      ? `http://export.arxiv.org/api/query?id_list=${id}`
      : `http://export.arxiv.org/api/query?search_query=all:${encodeURIComponent(query)}&max_results=1`;

    console.log(`[arxiv] Fetching: ${apiUrl}`);
    const response = await axios.get(apiUrl);
    const jsonObj = parser.parse(response.data);

    const entry = jsonObj.feed?.entry;
    if (!entry) return null;

    const paperEntry = Array.isArray(entry) ? entry[0] : entry;
    
    return {
      id: paperEntry.id,
      title: paperEntry.title.trim().replace(/\n/g, ' '),
      summary: paperEntry.summary.trim(),
      authors: Array.isArray(paperEntry.author) 
        ? paperEntry.author.map((a: any) => a.name)
        : [paperEntry.author.name],
      published: paperEntry.published,
      updated: paperEntry.updated,
      link: paperEntry.id,
      pdfUrl: paperEntry.id.replace('abs', 'pdf') + '.pdf'
    };
  } catch (error) {
    console.error('[arxiv] Error fetching paper:', error);
    return null;
  }
}