source: trunk/UTIL/PYTHON/bibweb/ads.py @ 1062

Last change on this file since 1062 was 968, checked in by aslmd, 12 years ago

UTIL PYTHON bibweb support for local pdf

  • Property svn:executable set to *
File size: 6.0 KB
Line 
1import os, re, urllib
2
3## -----------------------------------------------------------------
4## Purpose: make a nice publication page with an ADS database link
5## Author: Aymeric Spiga 19/05/2012
6## -----------------------------------------------------------------
7## NB: uses BIBTEX2HTML https://www.lri.fr/~filliatr/bibtex2html/doc/manual.html
8## ... and of course NASA ADS http://adsabs.harvard.edu/
9## -----------------------------------------------------------------
10
11def makepage(authorref,
12             bibstyle = "-s custom -nokeys",
13             listyear = [0],
14             customcond = None,
15             embedded = False,
16             linkads = None,
17             title = None,
18             retrieve = True,
19             addpdf = None,
20             addlink = None):
21
22    htmlcontent = ""
23   
24    ### HEADER
25    if embedded:
26     htmlfile = open('header.html','r')
27     htmlcontent = htmlfile.read()
28     htmlfile.close()
29    #else:
30    if title is None:
31      htmlcontent = htmlcontent + "<h2>"+authorref+"'s publications</h2>"
32    elif title == "":
33      pass
34    else:
35      htmlcontent = htmlcontent + title
36   
37    ### if linkads is None, we set it to "link.authorref"
38    if linkads is None: 
39      linkads = 'link.'+authorref
40
41    ### GET INFO FROM ADS
42    if retrieve:
43      print "retrieving info from ADS"
44      linkfile = open(linkads,'r')
45      url = linkfile.read()
46      linkfile.close()
47      html = urllib.urlopen(url).read()
48      bibfile = open(linkads+'.bib','w')
49      print >> bibfile,html
50      bibfile.close()
51   
52    ### if only one year and no customcond, make it useful. ask for years >= this value
53    if len(listyear) == 1 and customcond is None:
54        customcond = "-c 'year>=%s'" % (listyear[0])
55        listyear[0] = 99
56   
57    ### ADD LINK WITH YEARS IN HEADER
58    if customcond is None or len(listyear) > 1:
59        htmlcontent += "Year: "
60        for year in listyear:
61          htmlcontent += "<a href='#"+str(year)+"'>"+str(year)+"</a>.  "
62        if addlink is not None: htmlcontent += "<br>"+addlink
63   
64    ### YEAR LOOP
65    for year in listyear:
66   
67        author = authorref+str(year)
68        print author
69   
70        # 0. define condition
71        #    if not user-defined, make it simply year in each listyear instance
72        #    if user-defined, then customcond will be the condition (possibly several)
73        if customcond is None and len(listyear) > 1: cond = "-c 'year=%s'" % (year)
74        elif len(listyear) > 1: cond = customcond + " -c 'year=%s'" % (year)
75        else: cond = customcond
76   
77        # 1. select items ARTICLE in the big bib file
78        #    put those in a dedicated author.bib file
79        arg = \
80              cond,\
81              '"ARTICLE"',\
82              author+'.txt',\
83              author+'.bib',\
84              linkads+'.bib'
85        cmd = "bib2bib --quiet %s -c '$type=%s' -oc %s -ob %s %s" % (arg)
86        os.system(cmd)
87
88        # modify the bib file to insert pdf links
89        # the trick is to use the line adsurl and expect pdf to have the same name as ADS reference       
90        #        ... then besides this, it is necessary to link pdfs or rename those
91        #        ... the online repository is indicated by addpdf
92        if retrieve:
93         if addpdf is not None:
94            bibcontent = ''
95            for line in open(linkads+'.bib'):
96                bibcontent += line
97                if 'adsurl' in line:
98                    line = line.replace('adsurl','localpdf')
99                    line = line.replace('http://cdsads.u-strasbg.fr/abs/',addpdf)
100                    line = line.replace('},','.pdf},')
101                    line = line.replace('%','_')
102                    bibcontent += line
103            bibfile2 = open('temp','w')
104            print >> bibfile2,bibcontent
105            bibfile2.close()
106            os.system('mv temp '+linkads+'.bib')
107
108        # 2. make the html page from the author.bib file
109        if customcond is None or len(listyear) > 1:
110           header = '<a name="%.0f"></a>' % (year)
111           header += "<h3>%.0f <a href=''>.</a> </h3>" % (year)
112           if embedded: header += '<br>'
113        else:
114           header = ''
115   
116        header = '"'+header+'"'
117        arg = \
118              bibstyle,\
119              header,\
120              author+'.bib'
121        cmd = "bibtex2html -q \
122              --both \
123              -m ads.tex \
124              %s \
125              -nf adsurl 'ADS link' \
126              -nf localpdf 'PDF version' \
127              -r -d --revkeys \
128              -nofooter --nodoc \
129              --header %s -nokeywords \
130              %s" % (arg)
131        os.system(cmd)
132
133        # 3. load page content and delete intermediate HTML file
134        htmlfile = open(author+'.html','r')
135        htmlcontent = htmlcontent + htmlfile.read()
136        htmlfile.close()
137        os.system("rm -rf "+author+'.html')
138   
139    ## make a few corrections
140    ##     bibcontent = open(author+'.bib','r').read()
141    ##     bibcontent.replace('\grl','Yeah')
142   
143    find = re.compile(r'bib')
144    htmlcontent = find.sub('Bibtex entry',htmlcontent)
145    find = re.compile(r'Bibtex entry.html')
146    htmlcontent = find.sub('bib.html',htmlcontent)
147   
148    find = re.compile(r'DOI')
149    htmlcontent = find.sub('Journal website',htmlcontent)
150
151    #find = re.compile(r'.pdf')
152    #htmlcontent = find.sub('PDF version',htmlcontent)
153   
154    find = re.compile(r'<table>')
155    htmlcontent = find.sub('<table border="0" cellspacing="15">',htmlcontent)
156    find = re.compile(r'<td align="right">')
157    htmlcontent = find.sub('<td align="center" width=17% style="font-size: 75%;">',htmlcontent)
158
159    htmlcontent += '''<hr><p>Generated with
160    <a href='https://www.lri.fr/~filliatr/bibtex2html/doc/manual.html'>BibTeX2HTML</a>
161    and <a href='http://adsabs.harvard.edu/'>NASA ADS</a>
162    and a bit of <a href='http://www.python.org/'>Python</a></p>'''
163    if embedded:
164      htmlfile = open('footer.html','r')
165      htmlcontent += htmlfile.read()
166      htmlfile.close()
167     
168    htmlmain = open(authorref+'.html','w')
169    print >> htmlmain, htmlcontent
170    htmlmain.close()
Note: See TracBrowser for help on using the repository browser.