Created
December 9, 2017 15:49
-
-
Save thespacedoctor/04c9bbdf9c92ead12b2c4f8c88426802 to your computer and use it in GitHub Desktop.
[Pinboard Tag Homogeniser] Make all pinboard tags lowercase with underscores #tag #pinboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what module is this importing
from fundamentals import tools
Surprisingly difficult to google “python fundamentals module” :D