Skip to content

Instantly share code, notes, and snippets.

@grassmunk
Created May 18, 2020 17:13
Show Gist options
  • Save grassmunk/8c7eb1e28a70aacb1a29edcbbf8a1499 to your computer and use it in GitHub Desktop.
Save grassmunk/8c7eb1e28a70aacb1a29edcbbf8a1499 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import sys
import csv
import requests
import datetime
from pprint import pprint
from internetarchive import upload
x = datetime.datetime.now()
theme_zip_files = {}
if sys.argv[1].endswith(".zip"):
theme_zip_files[os.path.splitext(sys.argv[1].split("/")[-1])[0]] = sys.argv[1]
else:
for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
if file.endswith(".zip"):
theme_zip_files[os.path.splitext(file)[0]] = root +"/"+ file
preview_files = {}
for root, dirs, files in os.walk(sys.argv[2]):
for file in files:
if file.endswith(".png") or file.endswith(".txt"):
theme_name = file.split("_")[0]
item = os.path.splitext(file[file.find("_")+1:])[0]
if theme_name not in preview_files:
preview_files[theme_name] = []
preview_files[theme_name].append(root+"/"+file)
themes = {}
with open("../themes.csv") as infile:
reader = csv.reader(infile, delimiter=';')
for row in reader:
themes[row[0]] = {'date': row[2], 'title': row[3]}
for theme in theme_zip_files:
md = {
"collection" : "open_source_software",
"mediatype": "software",
"subject": "Desktop Theme",
"creator" : "themeworld",
"date" : "",
"description": '',
"title": ""
}
if theme in themes:
md['title'] = themes[theme]['title']
md['description'] = "Windows 95/98/ME/XP theme: {}".format(themes[theme]['title'])
md['date'] = themes[theme]['date']
if theme in preview_files:
upload_dict = {theme+".zip" : theme_zip_files[theme]}
for file in preview_files[theme]:
#print(file)
if ".txt" in file:
if "author" in file:
author = ''
with open(file, 'r') as infile:
for line in infile:
author += line.strip().strip(":").strip().strip("*").strip("-")
if "simsanet theme-o-matic" not in author:
no_emails = author.split(" ")
author = ''
for word in no_emails:
if "@" not in word and "/" not in word and "-" not in word:
author += word + " "
else:
author = ''
if author == '':
author = "themeworld"
md['creator'] = author
elif "text" in file:
text = "<div>Windows 95/98/ME/XP theme Read Me file:</div><div><br></div><pre>"
with open(file, 'r') as infile:
text += infile.read()
text +="</pre>"
md['description'] = text
continue
if 'double' in file and theme+".1.png" not in upload_dict:
filename = theme+".1.png"
else:
filename = file.split("/")[-1]
upload_dict[filename] = file
with open("log", "a") as logfile:
try:
print("Uploading {}: {}".format(theme, md['title']))
logfile.write("Uploading {}: {}\n".format(theme, md['title']))
r = upload(theme, files=upload_dict, metadata=md, verbose=True, retries=5, retries_sleep=5)
except requests.exceptions.HTTPError:
print("{} already exists, trying with {}_{}{:02}".format(theme, theme,x.year,x.month))
logfile.write("{} already exists, trying with {}_{}{:02}\n".format(theme, theme,x.year,x.month))
r = upload("{}_{}{:02}".format(theme,x.year,x.month), files=upload_dict, metadata=md, verbose=True, retries=5, retries_sleep=5)
print("{} response code: {}".format(theme_zip_files[theme], r[0].status_code))
logfile.write("{} response code: {}\n".format(theme_zip_files[theme], r[0].status_code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment