Hide
Need a quick chart or graph for your blog? Try our reStructured Text renderer. Join Siafoo Now or Learn More

Throwback Thursday inspired script to search image files Atom Feed 0

In Brief First off, all I wanted was a way to find images for Throwback Thursday and I have a very messy folder called Pictures and in there my photos go back to 2003, November.... more
# 's
  1# use os.path.getctime to get creation date/time.
2# use date.weekday() to get 0 = Monday to 6 = Sunday.
3# then use present Thursdays as tbt files :)
4
5# ctime - the last time the file's inode was changed (e.g. permissions changed, file renamed, etc..)
6# mtime - last time the file's CONTENTS were changed
7# atime - last time the file was accessed.
8# author: Michael E. Case, mec at dcn dot davis dot ca dot us
9
10import sys
11import os
12from datetime import *
13import time
14from sys import argv
15#from subprocess import Popen
16import argparse
17import random
18
19# 2015-12-14:
20# Given a path, search through it and all it's subdirectories for "throwback thursday" files.
21
22# info to identify program and current revision, and some globals.
23global pgm, rev, date
24pgm = (sys.argv[0]).replace('./','')
25rev = '$Revision: 1.0 $'.replace('$','')[9:].strip()
26date = '$Date: 2015-12-14 14:15:28 $'.replace('$','')[5:].strip()
27
28def isint( s ):
29 try:
30 int(s)
31 except ValueError:
32 return False
33 else:
34 return True
35
36def tbt_main():
37
38 #read command arguments
39 global pgm, rev, date
40
41 ap = argparse.ArgumentParser(prog=pgm, epilog="(Revision "+rev+" of "+date+")", description="""Program for \"Throwback Thursdays\"
42allows users to specify day of week to find, defaults to Thursday, and provides a list of jpg and png files within the directory
43you specify that occurred on that day of week. However, precedence goes mw, m then l. In other words, ONLY ONE of
44those flags will be supported, if you specify more than one, the precedence is enforced.""")
45 ap.add_argument("directory", help="Required. Directory from which to read files.")
46 ap.add_argument("-d", help="Optional. Day of week. Defaults to Thursday. Can not do multiple days.", default="3")
47 ap.add_argument("-l", help="Optional. Reply with list of files that can be passed to something like xargs.", action="store_true", default=False)
48 ap.add_argument("-m", help="Optional. Mac OS open Preview App with resulting list of files. If the list of files gets too long, the command may fail.", action="store_true", default=False)
49 ap.add_argument("-mw", help="Optional. Mac OS open Preview App with one image per window. Can be torture to your screen, be careful.", action="store_true", default=False)
50 ap.add_argument("-n", help="Optional. Number of files to *randomly* retreive from list.", default=-1, type=int)
51 newargs = ap.parse_args()
52 #print(newargs)
53 #if ()
54 listfiles = newargs.l
55 indir = newargs.directory
56 nf = newargs.n
57 macprev = newargs.m
58 macprevwin = newargs.mw
59 dow = 3
60 if isint(newargs.d) == False:
61 lcd = newargs.d.lower()
62 if "monday" in lcd:
63 dow = 0
64 elif "tuesday" in lcd:
65 dow = 1
66 elif "wednesday" in lcd:
67 dow = 2
68 elif "thursday" in lcd:
69 dow = 3
70 elif "friday" in lcd:
71 dow = 4
72 elif "saturday" in lcd:
73 dow = 5
74 elif "sunday" in lcd:
75 dow = 6
76 else:
77 print(newargs.d+" not found in day of week. Day of week will default to Thursday (3).")
78 dow = 3
79 else:
80 if dow > 6:
81 print(newargs.d+" is greater than 6. Day of week will default to Thursday (3).")
82 dow = 3
83 else:
84 dow = int(newargs.d)
85 # print ("newargs.d = "+newargs.d+" and dow = "+str(dow))
86
87 if macprev == True and macprevwin == True and listfiles == True:
88 macprev = False
89 listfiles = False
90 print("Found -mw, -w and -l, precedence dictates using mw.")
91 if macprev == True and listfiles == True:
92 listfiles = False
93 print("Found -w and -l, precedence ditates using -m.")
94 if macprevwin == True and listfiles == True:
95 listfiles = False
96 print("Found -mw and -l, precedence dictates using -mw.")
97
98 # print(macprev, str(nf), indir, listfiles)
99
100 # print(indir)
101 fs = ""
102 fsofar = 0
103 randomfs = False
104 if nf != -1:
105 randomfs = True
106 nfilestot = 0
107 # probtofind = 0
108 rindstosave = []
109
110 # can't figure out how to do this using "probability of this picture being one of the n you requested."
111 # or rather, that solution did not always give me n. So the new solution is come up with n integers between
112 # one and nfilestot. Those will be the ones we get. No repeats, of course.
113 if randomfs == True:
114 for root, dirs, files in os.walk(indir, topdown=True):
115 #print("in first for")
116 for name in files:
117 tm = os.path.getmtime(os.path.join(root,name))
118 dt = datetime.fromtimestamp(tm)
119 if dt.weekday() == dow:
120 if len(name.split(".")) > 1:
121 spl = name.split(".")
122 last = spl[len(spl)-1]
123 if last.lower() == "jpg" or last.lower() == "png":
124 nfilestot +=1
125 # print("total number of all jpg and png = "+str(nfilestot))
126 # exit()
127 if nfilestot <= nf:
128 rindstosave = range(1,nfilestot+1)
129 elif nfilestot > 1:
130 nsel = 0
131 while nsel < nf and nf > 0:
132 rn = random.randint(1, nfilestot) # [1, nfilestot]
133 if rn not in rindstosave:
134 rindstosave.append(rn)
135 nsel += 1
136 # print ("nsel = "+str(nsel))
137 # print (rindstosave)
138 rindstosave.sort()
139 # print (rindstosave)
140 # probtofind = nf / nfilestot
141 elif nfilestot == 1:
142 rindstosave.append(1)
143 else:
144 print ("nfilestot NOT > 0!")
145 exit()
146 fc = 0
147 if randomfs and len(rindstosave) == 0:
148 print("Can not proceed. No random requested for "+str(nf)+" and there were no files found to display")
149 exit()
150
151 for root, dirs, files in os.walk(indir, topdown=True):
152 #print("in first for")
153 for name in files:
154 #print("in second for")
155 tm = os.path.getmtime(os.path.join(root,name))
156 dt = datetime.fromtimestamp(tm)
157 if dt.weekday() == dow:
158 if len(name.split(".")) > 1:
159 spl = name.split(".")
160 last = spl[len(spl)-1]
161 if last.lower() == "jpg" or last.lower() == "png":
162 fc += 1
163 #print("found jpg")
164 #os.system
165 #Popen
166 if macprev == True:
167 #print (fs)
168 if randomfs == False:
169 fs += "\""+os.path.join(root,name)+"\" "
170 else:
171 if fc in rindstosave:
172 fs += "\""+os.path.join(root,name)+"\" "
173 rindstosave = rindstosave[1:]
174 if macprevwin == True:
175 if randomfs == False:
176 os.system("open -a /Applications/Preview.app "+"\""+os.path.join(root,name)+"\"")
177 else:
178 if fc in rindstosave:
179 os.system("open -a /Applications/Preview.app "+"\""+os.path.join(root,name)+"\"")
180 rindstosave = rindstosave[1:]
181 if listfiles == True:
182 if randomfs == False:
183 print("\""+os.path.join(root,name)+"\"")
184 else:
185 if fc in rindstosave:
186 print("\""+os.path.join(root,name)+"\"")
187 rindstosave = rindstosave[1:]
188
189 # call("open -f -a /Applications/Preview.app", os.path.join(root,name))
190 if macprev == True:
191 #print ("open -a /Applications/Preview.app "+fs)
192 os.system("open -a /Applications/Preview.app "+fs)
193
194if __name__ == "__main__":
195 tbt_main()

First off, all I wanted was a way to find images for Throwback Thursday and I have a very messy folder called Pictures and in there my photos go back to 2003, November.

Some are older, but their modification/creation dates on my computer will not reflect their true date/time so this tool will be useless for those. Unless I modify it later, for example, I can think of an improvement. . . if date modified content does not match date in name of file (parse the name of the file) then use the name as the date.