python [Pinboard Tag Homogeniser]使用下划线#tag #pinboard将所有插板标签设为小写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [Pinboard Tag Homogeniser]使用下划线#tag #pinboard将所有插板标签设为小写相关的知识,希望对你有一定的参考价值。
#!/usr/local/bin/python
# encoding: utf-8
"""
*Tidy up Pinboard Tags*
:Author:
David Young
:Date Created:
November 8, 2017
Usage:
pinboard_tag_tidy_up
Options:
-h, --help show this help message
"""
################# GLOBAL IMPORTS ####################
import sys
import os
from fundamentals import tools
import pinboard
import re
from time import sleep
# FIND YOUR TOKEN HERE: https://pinboard.in/settings/password
PINBOARDAPITOKEN = "xxxxxxxxx"
def main(arguments=None):
"""
*The main function used when ``pinboard_tag_tidy_up.py`` is run as a single script from the cl*
"""
# SETUP THE COMMAND-LINE UTIL SETTINGS
su = tools(
arguments=arguments,
docString=__doc__,
logLevel="DEBUG",
options_first=False,
projectName=False
)
arguments, settings, log, dbConn = su.setup()
# UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
# AUTOMATICALLY
for arg, val in arguments.iteritems():
if arg[0] == "-":
varname = arg.replace("-", "") + "Flag"
else:
varname = arg.replace("<", "").replace(">", "")
if isinstance(val, str) or isinstance(val, unicode):
exec(varname + " = '%s'" % (val,))
else:
exec(varname + " = %s" % (val,))
if arg == "--dbConn":
dbConn = val
log.debug('%s = %s' % (varname, val,))
# LOG INTO PINABOARD AND GRAB TAGS
pb = pinboard.Pinboard(PINBOARDAPITOKEN)
tags = pb.tags
# MAKE ALL TAGS LOWERCASE
regex = re.compile(r'^[A-Z].*')
for t in tags.get():
matchObject = regex.match(t.name.encode("utf-8"))
if matchObject:
print matchObject.group()
pb.tags.rename(old=t.name, new=t.name.lower() + "xxx")
# REMOVE THE xxx FROM RENAMED TAGS
regex = re.compile(r'.*xxx$')
tags = pb.tags
for t in tags.get():
matchObject = regex.match(t.name.encode("utf-8"))
if matchObject:
print matchObject.group()
pb.tags.rename(old=t.name, new=t.name.replace("xxx", ""))
# REPLACE HYPHEN WITH UNDERSCORES
regex = re.compile(r'.*-.*')
tags = pb.tags
for t in tags.get():
matchObject = regex.match(t.name.encode("utf-8"))
if matchObject:
print matchObject.group()
pb.tags.rename(old=t.name, new=t.name.replace("-", "_"))
# REPLACE PERIOD WITH UNDERSCORES
regex = re.compile(r'.*\..*')
tags = pb.tags
for t in tags.get():
matchObject = regex.match(t.name.encode("utf-8"))
if matchObject:
print matchObject.group()
pb.tags.rename(old=t.name, new=t.name.replace(".", "_"))
return
if __name__ == '__main__':
main()
以上是关于python [Pinboard Tag Homogeniser]使用下划线#tag #pinboard将所有插板标签设为小写的主要内容,如果未能解决你的问题,请参考以下文章