forked from jesswhyte/floppycapture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdcapture.py~
228 lines (183 loc) · 5.98 KB
/
cdcapture.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
### IN PROGRESS
### environment-specific Python3 script to walk through floppy disk capture workflow
### Jess, Jan 2018
import sys
import argparse
import os
import subprocess
import datetime
import json
import urllib
from urllib.request import urlopen
from collections import OrderedDict
#######################
###### ARGUMENTS ######
#######################
parser = argparse.ArgumentParser(
description ="Script to walk through floppy disk capture workflow, Jan 2018")
parser.add_argument(
'-l', '--lib', type=str,
help='Library, for a list of library IDs, visit ufot.me/libs ',
required=True,
choices=['ARCH','ART','ASTRO','CHEM','CRIM',
'DENT','OPIRG','EARTH','EAL','ECSL','FCML',
'FNH','GERSTEIN','INFORUM','INNIS','KNOX',
'LAW','MDL','MATH','MC','PONTIF','MUSIC',
'NEWCOLLEGE','NEWMAN','OISE','PJRC','PHYSICS',
'REGIS','RCL','UTL','ROM','MPI','STMIKES',
'TFRBL','TRIN','UC','UTARMS','UTM','UTSC','VIC'])
parser.add_argument(
'-d','--dir', type=str,
help='Start directory, e.g. /home/jess/CAPTURED', required=True)
parser.add_argument(
'-i','--i4',action='store_true',
help='use flag to default to i4/MFM')
parser.add_argument(
'-m', '--mediatype', type=str,
help='Use \"3.5\" or \"5.25\"',required=True,
choices=['3.5','5.25'])
parser.add_argument(
'-c', '--call', type=str,
help='Call or Collection Number', required=False)
parser.add_argument(
'-k', '--key', type=str,
help='Catkey', required=False)
parser.add_argument(
'-t', '--transcript', type=str,
help='Transcript of label', required=False)
parser.add_argument(
'-n','--note', type=str,
help='catalog note', required=False)
## Array for all args passed to script
args = parser.parse_args()
### Variables
drive = "d0"
date = datetime.datetime.today().strftime('%Y-%m-%d')
lib = args.lib
mediaType = args.mediatype
callNum = args.call
callDum=callNum.replace('.','-')
catKey = args.key
label = args.transcript
dir = args.dir
catUrl = "https://search.library.utoronto.ca/details?"+catKey+"&format=json"
note=args.note
#######################
###### FUNCTIONS ######
#######################
### TODO: rewrite kfStream as subprocess, temp)
def kfStream():
os.system(
"dtc -"+drive+" -fstreams/"+callDum+"/"
+callDum+"_stream -i0 -t2 -p | tee "
+outputPath+callDum+"_capture.log")
### TODO: Rewrite kfImage based on stdout of kfStream, e.g. MFM = OK
### TODO: Rewrite as subprocess
#takes existing stream, attemps to make image based on given fileSystem [not in use]
def kfImage(fileSystem):
os.system(
"dtc -fstreams/"+callDum+"/"
+callDum+"_stream00.0.raw -i0 -f"+outputPath+callDum+"_disk.img -"
+fileSystem+" -m1")
#Takes preservation stream + attempts to create i4 or MFM disk image
def kfi4():
os.system(
"dtc -"+drive+" -fstreams/"+callDum+"/"
+callDum+"_stream -i0 -f"+outputPath+callDum+
"_disk.img -i4 -t1 -l8 -p | tee "+outputPath+callDum+"_capture.log")
#get some json from an URL
def get_json_data(url):
response = urlopen(url)
data = response.read().decode()
return json.loads((data), object_pairs_hook=OrderedDict)
########################
##### THE GOODS ######
########################
### Change working directory
if not os.path.exists(dir):
os.makedirs(dir)
os.chdir(dir)
### Create directory for output if it doesn't exist
outputPath = lib+"/"+callDum+"/"
if not os.path.exists(outputPath):
os.makedirs(outputPath)
if os.path.exists(outputPath):
print("FC UPDATE: "+outputPath+" is created")
### GET THE TITLE AND OTHER METADATA
## make a dictionary out of the response from catUrl
## extract the title value from title key from that dictionary
## will write later in json dump
cat_dic = (get_json_data(catUrl))
title= cat_dic ["record"]["title"]
### check Media, set drive
if mediaType == "3.5":
drive = "d0"
elif mediaType == "5.25":
drive = "d1"
### TAKE A PICTURE
## Note: fswebcam defaults to /dev/video0, if device not found...
## use ls -ltrh /dev/video* to list devices and use
## flag -d to set device
## use cheese or VTL42 test utility to set focus, if needed
picName = callDum + ".jpg"
picParameters = " --jpeg 95 -r 800x600 --no-banner -S 5 "+outputPath+picName
### TODO: write as subprocess)
os.system("fswebcam"+ picParameters)
### MORE JSON AND METADATA STUFF
## Create dictionary of capture data
capture_dic = {
'disk':{
'CaptureDate': date,
'media': mediaType+"\" floppy disk",
'label': label,
'library': lib,
'diskpic': picName}
}
## delete holdings info (e.g. checkout info) from cat_dic
del cat_dic["record"]["holdings"]
## write to TEMPmetadata.json for now
with open('TEMPmetadata.json','w+') as metadata:
cat_dic.update(capture_dic)
json.dump(cat_dic, metadata)
### PRINT THE TITLE
##TODO: Consider requiring user to confirm TITLE IS: +title
## x1b stuff is just to make it show up a different color so it's noticeable
print("\x1b[6;30;42m" + "FC UPDATE: title is: " +title + "\x1b[0m")
### KRYOFLUX - GET A PRESERVATION STREAM
## Pause and give user time to put disk in
go = input("Please insert disk and hit Enter")
## take the stream only if it doesn't already exist
if not os.path.exists("streams/"+callDum+"/"+callDum+"_stream00.0.raw"):
if args.i4:
kfi4()
else:
kfStream()
fileSystem = input("Which filesytem? ")
if not os.path.exists(outputPath+callDum+"_disk.img"):
kfImage(fileSystem)
### TODO: write filesystem metadata and verify disk image - Think about if this should be separate...I think it should...
####################
#### END MATTER ####
####################
metadata.close()
### Rename our metadata.txt file
newMetadata = callDum + '.json'
os.rename('TEMPmetadata.json', outputPath+newMetadata)
### Update master log
## TODO: this should really use csv library, I was lazy
log = open('projectlog.csv','a+')
log.write(
"\n"+lib+","+callNum+","+catKey+","+mediaType+
",\""+str(title)+"\","+"\""+label+"\",\""+note+"\"")
if os.path.exists(
outputPath+picName):
log.write(",pic=OK")
else:
log.write(",pic=NO")
if os.path.exists(
outputPath+callDum+"_disk.img"):
log.write(",img=OK")
else:
log.write(",img=NO")
### Close master log
log.close()