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

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

UTIL PYTHON a tool to build automatic web page with ADS

  • Property svn:executable set to *
File size: 5.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             addlink = None):
20
21    htmlcontent = ""
22   
23    ### HEADER
24    if embedded:
25     htmlfile = open('header.html','r')
26     htmlcontent = htmlfile.read()
27     htmlfile.close()
28    #else:
29    if title is None:
30      htmlcontent = htmlcontent + "<h2>"+authorref+"'s publications</h2>"
31    elif title == "":
32      pass
33    else:
34      htmlcontent = htmlcontent + title
35   
36    ### if linkads is None, we set it to "link.authorref"
37    if linkads is None: 
38      linkads = 'link.'+authorref
39
40    ### GET INFO FROM ADS
41    if retrieve:
42      print "retrieving info from ADS"
43      linkfile = open(linkads,'r')
44      url = linkfile.read()
45      linkfile.close()
46      html = urllib.urlopen(url).read()
47      bibfile = open(linkads+'.bib','w')
48      print >> bibfile,html
49      bibfile.close()
50   
51    ### if only one year and no customcond, make it useful. ask for years >= this value
52    if len(listyear) == 1 and customcond is None:
53        customcond = "-c 'year>=%s'" % (listyear[0])
54        listyear[0] = 99
55   
56    ### ADD LINK WITH YEARS IN HEADER
57    if customcond is None or len(listyear) > 1:
58        htmlcontent += "Year: "
59        for year in listyear:
60          htmlcontent += "<a href='#"+str(year)+"'>"+str(year)+"</a>.  "
61        if addlink is not None: htmlcontent += "<br>"+addlink
62        if embedded: htmlcontent += "<br><br /><hr><br>"
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        # 2. make the html page from the author.bib file
89        if customcond is None or len(listyear) > 1:
90           header = '<a name="%.0f"></a>' % (year)
91           header += "<h3>%.0f <a href=''>.</a> </h3>" % (year)
92           if embedded: header += '<br>'
93        else:
94           header = ''
95   
96        header = '"'+header+'"'
97        arg = \
98              bibstyle,\
99              header,\
100              author+'.bib'
101        cmd = "bibtex2html -q \
102              --both \
103              -m ads.tex \
104              %s \
105              -nf adsurl 'ADS link' \
106              -r -d --revkeys \
107              -nofooter --nodoc \
108              --header %s -nokeywords \
109              %s" % (arg)
110        os.system(cmd)
111   
112        # 3. load page content and delete intermediate HTML file
113        htmlfile = open(author+'.html','r')
114        htmlcontent = htmlcontent + htmlfile.read()
115        htmlfile.close()
116        os.system("rm -rf "+author+'.html')
117   
118    ## make a few corrections
119    ##     bibcontent = open(author+'.bib','r').read()
120    ##     bibcontent.replace('\grl','Yeah')
121   
122    find = re.compile(r'bib')
123    htmlcontent = find.sub('Bibtex entry',htmlcontent)
124    find = re.compile(r'Bibtex entry.html')
125    htmlcontent = find.sub('bib.html',htmlcontent)
126   
127    find = re.compile(r'DOI')
128    htmlcontent = find.sub('Journal website',htmlcontent)
129    find = re.compile(r'.pdf')
130    htmlcontent = find.sub('PDF version',htmlcontent)
131   
132    find = re.compile(r'<table>')
133    htmlcontent = find.sub('<table border="0" cellspacing="15">',htmlcontent)
134    find = re.compile(r'<td align="right">')
135    htmlcontent = find.sub('<td align="center" width=17% style="font-size: 75%;">',htmlcontent)
136   
137    htmlcontent += '''<hr><p>Generated with
138    <a href='https://www.lri.fr/~filliatr/bibtex2html/doc/manual.html'>BibTeX2HTML</a>
139    and <a href='http://adsabs.harvard.edu/'>NASA ADS</a>
140    and a bit of <a href='http://www.python.org/'>Python</a></p>'''
141    if embedded:
142      htmlfile = open('footer.html','r')
143      htmlcontent += htmlfile.read()
144      htmlfile.close()
145     
146    htmlmain = open(authorref+'.html','w')
147    print >> htmlmain, htmlcontent
148    htmlmain.close()
Note: See TracBrowser for help on using the repository browser.