Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import os, json, datetime, re, csv
- # only necessary if you are using the drive CLI tool.
- from contextlib import contextmanager
- from subprocess import call
- # only necessary if you are using the drive CLI tool.
- # general settings
- txtFile = os.path.join(os.path.expanduser("~"), "article-list.txt") # Target file name list.
- databaseFile = "fcm-database.json"
- # article-list creation
- searchPaths = ['/home/lswest/Documents/GDrive/FCM', '/home/lswest/Dropbox/Articles/Full Circle Magazine/C&C'] # Must be list!
- # for database creation
- dateStart = {100: datetime.date(2015, 8, 28)}
- entryTemplate = {-1: {}} # Template for the entry format - used in the update_database function.
- if os.stat(databaseFile).st_size == 0:
- db = {} # create a blank database if the json file is empty.
- else:
- with open(databaseFile, 'r') as jsonFile:
- db = json.load(jsonFile) # load the json file into a database, if not empty.
- print("Database Loaded")
- @contextmanager
- def cd(newdir):
- """ This function is used to call a python equivalent of cd - just required for update_drive """
- prevdir = os.getcwd()
- os.chdir(os.path.expanduser(newdir))
- try:
- yield
- finally:
- os.chdir(prevdir)
- def update_drive(drvPath):
- """ This function is used to call the drive pull command - to update any changes to the folder. If you don't use drive, just comment out the call to this function in main() """
- with cd(drvPath):
- call(["drive", "pull"])
- return True
- def dateFind(issue, startPoint):
- """This function calculates the date based off the number issues to/from 100. I.e. subtracts 2 months if considering issue 98."""
- dateDiff = 100 - int(issue.replace("FCM", ""))
- return (startPoint[100] - datetime.timedelta((dateDiff * 365) / 12)).isoformat()
- def createArticleList(target, paths):
- """This generates the text file containing all file names that should be included in the database. Also does some basic cleanup (deletes .odt and changes Command & Conquer into C&C)"""
- regexp = re.compile("FCM[0-9]+")
- output = []
- for path in paths:
- files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) and regexp.search(f) is not None and ".desktop" not in f]
- output.append(files)
- output = [item for sublist in output for item in sublist]
- output = [w.replace(".odt", "") for w in output]
- output = [w.replace("Command & Conquer", "C&C") for w in output]
- output = set(output)
- with open(target, 'w') as f:
- for line in output:
- f.write(line+"\n")
- f.close()
- return True
- def update_database(database, txtPath, entry):
- """This function uses the text file created in createArticleList, and splits the file names into a dict object, which is added to the JSON database."""
- with open(txtPath, 'r') as f:
- for line in f.readlines():
- if line != "\n":
- items = line.split(" - ")
- number = items[0].replace("FCM", "")
- entry[number] = {}
- entry[number]["issue"] = number
- if len(items) > 2:
- entry[number]["article"] = items[2].replace("\n", "")
- else:
- entry[number]["article"] = "Unknown"
- entry[number]["column"] = items[1]
- entry[number]["date"] = dateFind(items[0], dateStart)
- database.update(entry)
- f.close()
- del database[-1]
- return database
- def write_database(database, dbPath):
- """Writes the database to the json file."""
- with open(dbPath, 'w') as f:
- f.write(json.dumps(database, indent=4))
- return True
- def write_csv_database(database, dbPath):
- """For easy import into some spreadsheets programs, you can save the database as a CSV file as well."""
- f = csv.writer(open(dbPath, "w"))
- allKeys = list(database["100"].keys()) # used to ensure values and keys match up.
- f.writerow(allKeys)
- for key in database:
- f.writerow([database[key][allKeys[0]], database[key][allKeys[1]], database[key][allKeys[2]], database[key][allKeys[3]]])
- def main():
- #update_drive(searchPaths[0])
- createArticleList(txtFile, searchPaths)
- dbN = update_database(db, txtFile, entryTemplate)
- write_database(dbN, databaseFile)
- #write_csv_database(db, "fcm-database.csv")
- if __name__ == "__main__":
- main() # Function is called, instead of inserting methods directly into this section. Useful for eventual imports of the script.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement