For the handful of people that keep up with my astro-ph postings, I figured to share my python script that automates this process because manual posting would be extremely tedious. The script asks for an arXiv number, and then retrieves the title, authors, and PDF. It asks the user for categories for the WordPress site and a filename to save the PDF. After this, then it creates some HTML for the post and uploads it to this site.
import xmlrpclib
import os, urllib
papers_dir = "/path/to/local/papers"
user = 'wordpress_user'
passwd = '12345' # WP password
server = xmlrpclib.ServerProxy('http://url.to/xmlrpc.php')
class ArXiv:
def __init__(self, url):
self.title = None
self.pdf_name = None
self.authors = []
self.url = url
self.pdf_url = None
sock = urllib.urlopen(self.url)
self.raw_html = sock.read()
sock.close()
def parse(self):
lines = self.raw_html.split("\n")
for l in lines:
if l.find("/head") >= 0: break
if l.find("citation_author") >= 0:
author = l.split("\"")[3]
self.authors.append(author)
if l.find("citation_title") >= 0:
self.title = l.split("\"")[3]
if l.find("citation_pdf_url") >= 0:
self.pdf_url = l.split("\"")[3]
ac = self.pdf_url.find("arxiv")
#self.pdf_url = self.pdf_url[:ac] + "lanl." + \
# self.pdf_url[ac:]
nauth = len(self.authors)
if nauth == 1:
self.author_list = self.authors[0].split(",")[0]
elif nauth == 2:
self.author_list = self.authors[0].split(",")[0]
self.author_list += " & " + self.authors[1].split(",")[0]
else:
self.author_list = self.authors[0].split(",")[0] + " et al."
def meta_query(self, tags):
print "%s (%s)" % (self.title, self.author_list)
print "-"*72
this_tag = None
while this_tag != "":
this_tag = raw_input("Enter a paper category (blank to end): ")
if this_tag != "":
tags.append(this_tag)
self.pdf_name = raw_input("Enter PDF filename: ")
return tags
def return_html(self):
return "<li>(<a href=\"%s\">abs</a>, <a href=\"%s\">pdf</a>) %s, <em>%s</em></li>\n" % \
(self.url, self.pdf_url, self.author_list, self.title)
def save_pdf(self):
abs_path = "%s/%s" % (papers_dir, self.pdf_name)
if os.path.exists(abs_path):
overwrite = raw_input("%s exists. Overwrite (y/n)? " % self.pdf_name)
if overwrite.lower() == "y":
os.remove(abs_path)
urllib.urlretrieve(self.pdf_url, abs_path)
else:
urllib.urlretrieve(self.pdf_url, abs_path)
return
blog_title = raw_input("Blog title: ")
post_text = "<ul>\n"
number = None
tags = []
while number != "":
number = raw_input("Enter ArXiV number (blank to end): ")
if number != "":
url = "http://arxiv.org/abs/%s" % number
entry = ArXiv(url)
entry.parse()
tags = entry.meta_query(tags)
entry.save_pdf()
post_text += "\t" + entry.return_html()
post_text += "</ul>\n"
blog_id = 0
blog_content = { "title": blog_title,
"description": post_text,
"mt_keywords": tags }
categories = [{'categoryId' : '4', 'isPrimary' : 1}]
post_id = int(server.metaWeblog.newPost(blog_id, user, passwd, blog_content,0))
server.mt.setPostCategories(post_id, user, passwd, categories) # not work
server.mt.publishPost(post_id, user, passwd)
I can find a use for this.