这是我在python中的代码。
#!/usr/bin/env python
'''
Created on Sep 28, 2013
@author: dataq <http://stackoverflow.com/users/2585246/dataq>
This is a simple code to rename the paper based on the ORIGINAL FILENAME and their website.
Your are free to use this code, but don't blame me for the error.
I am not writing any documentation, so please read my mind in this code.
USE ON YOUR OWN RISK *evil smirk*
'''
import urllib2, re, time, random
from os import listdir, rename
from os.path import isfile, join
# for every publisher we have different way of scraping
IEEE = 1
SCIENCEDIRECT = 2
# yes, I know, this very bad and stupid web scraping. But it's work at least.
# get title for IEEE paper
# the IEEE paper filename is looks like this '06089032.pdf'
def getIEEETitle(fname):
# get url
number = int(fname.split('.')[0])
targeturl = 'http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber='+str(number)
# open and read from those url
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
ieeePage = opener.open(targeturl).read()
# split every tag in the html. this is the stupid part :p
ieeePageSplit = ieeePage.replace('<','>').split('>')
title = None
# find a tag that start with 'meta name="citation_title" content="'
for i in ieeePageSplit:
if i.startswith('meta name="citation_title" content="'):
# get the paper title
title = i.split('"')[3]
break
# a file name cannot be longer than 255 character (theoretically)
# http://msdn.microsoft.com/en-us/library/aa365247.aspx
return title.strip()[:150]
# get title for Science Direct paper
# the Science Direct paper filename is looks like this '1-s2.0-0031320375900217-main.pdf'
def getScienceDirectTittle(fname):
# get url
number = fname.split('-')[2]
targeturl = 'http://www.sciencedirect.com/science/article/pii/'+number
# open and read from those url
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
sdPage = opener.open(targeturl).read()
# split every tag in the html. this is the stupid part :p
sdPageSplit = sdPage.replace('<','>').split('>')
title = None
for i in range(len(sdPageSplit)):
if sdPageSplit[i].startswith('title'):
title = sdPageSplit[i+1]
break
# a file name cannot be longer than 255 character (theoretically)
# http://msdn.microsoft.com/en-us/library/aa365247.aspx
return title.strip()[:150]
def batchRename(workingdir, site):
# list all file in working directory
files = [ fInput for fInput in listdir(workingdir) if isfile(join(workingdir,fInput)) ]
# compiled regular expression for illegal filename character
reIlegalChar = re.compile(r'([<>:"/\\|?*])')
# rename all files
for f in files:
try:
# find title
if site == IEEE:
title = getIEEETitle(f)
elif site == SCIENCEDIRECT:
title = getScienceDirectTittle(f)
else:
title = None
if title:
# remove illegal file name character
fnew = reIlegalChar.sub(r' ', title) + '.pdf'
print '{} --> {}'.format(f, fnew)
# rename file
rename((workingdir + f), (workingdir + fnew))
print 'Success'
else:
print '{}\nFailed'.format(f)
except:
print '{}\nERROR'.format(f)
# give some random delay, so we will not be blocked (hopefully) :p
time.sleep(random.randrange(10))
if __name__ == '__main__':
print 'Please be patient, it takes time depending on your internet connection speed...'
workingdir = 'C:\\Users\\dataq\\Downloads\\paper\\'
batchRename(workingdir, IEEE)
此代码适用于 IEEE 和 Science Direct 文章。您可以将您的文章放在workingdir
. 当然,您可以将 的值更改workingdir
为您自己的文件夹。
在该代码中,我将文件夹中的 IEEE 文章重命名C:\Users\dataq\Downloads\paper\
。如果您想重命名 Science Direct 文章,您必须更改batchRename(workingdir, IEEE)
为batchRename(workingdir, SCIENCEDIRECT)
您必须确保文章文件名是原始的(原始 IEEE 文章看起来像这样:06089032.pdf
而对于 Science Direct 文章看起来像这样1-s2.0-0031320375900217-main.pdf
:)
我不保证这些工具可以正常工作,所以使用它需要您自担风险。