Explorar o código

Updated dataLoad to handle some sensible commandline parameters.

bugz %!s(int64=5) %!d(string=hai) anos
pai
achega
cddd1da04a
Modificáronse 1 ficheiros con 37 adicións e 17 borrados
  1. 37 17
      dataLoad.py

+ 37 - 17
dataLoad.py

@@ -3,13 +3,22 @@
 from PIL import Image
 from pprint import pprint
 
-import sys, time, os, requests, random, json
+import sys, time, os, requests, random, json, argparse
+
+parser = argparse.ArgumentParser(description='Unknown program that breaks in strange ways.')
+parser.add_argument("--download", help="Download Images", action="store_true")
+parser.add_argument("JSON", type=str, nargs="?", help="Filename to save results", default="test.js")
+args = parser.parse_args()
+pprint(args)
 
 # Should we spend the time to download image, and process it? (True = Yes, False = No)
-DOWNLOAD = False
+# DOWNLOAD = False
+DOWNLOAD = args.download
 
 # Should we add the JSON in a file? (True is filename, False = do not do)
-JSONME = 'test.js'
+# JSONME = 'test.js'
+JSONME = args.JSON
+
 # NOTE: To begin the insert of the JSONIFIED image and word its
 # // T
 # A JS comment with a uppercase T
@@ -18,9 +27,9 @@ JSONME = 'test.js'
 # A JS comment with a lowercase t
 
 # httpbin.org/headers
-S = requests.Session()
+sess = requests.Session()
 head = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
-S.headers.update(head)
+sess.headers.update(head)
 
 ON = 'X'
 OFF = '.'
@@ -33,13 +42,24 @@ pix = [0, 0]
 size = [0, 0]
 pal = ''
 
-def download(o, i):
-    global S
-    r = S.get(f'http://s0urce.io/client/img/word/{o}/{i}')
+if not os.path.exists('in'):
+    os.mkdir('in')
+
+
+def download(howhard, index):
+    global sess
+    """
+    Download an image based upon how hard it is.
+
+    Note:  This sucks, because it overwrites images.  
+    Why not save the images and how hard?
+    Quit running your script on floppy disks!
+    """
+    r = sess.get(f'http://s0urce.io/client/img/word/{howhard}/{index}')
     if(r.status_code == 200):
-        with open(f'in/{i}.png', 'wb') as f:
+        with open(f'in/{index}.png', 'wb') as f:
             f.write(r.content)
-        return f'{i}.png ' + str(r.status_code)
+        return f'{index}.png ' + str(r.status_code)
     return r.status_code
 
 def img_point(x, y):
@@ -153,36 +173,36 @@ def run(VAL):
         os.remove(f'{fname}')
         print(f"File '{fname}' automatically removed")
 
-if(DOWNLOAD == True):
+if (DOWNLOAD == True):
     print("Downloading s0urce.io Words")
     print("EASY")
     time.sleep(5)
     for e in range(0, 62):
         print(download('e', e))
-        time.sleep(random.randint(10, 15))
+        # time.sleep(random.randint(10, 15))
 
     run(0)
 
     print("MEDIUM")
-    time.sleep(5)
+    # time.sleep(5)
     for m in range(0, 66):
         print(download('m', m))
-        time.sleep(random.randint(10, 15))
+        # time.sleep(random.randint(10, 15))
 
     run(62)
 
     print("HARD")
-    time.sleep(5)
+    # time.sleep(5)
     for h in range(0, 55):
         print(download('h', h))
-        time.sleep(random.randint(10, 15))
+        # time.sleep(random.randint(10, 15))
 
     run(128)
 
 # ----------------------------------------------------------------------------------------
 # All below was in a seperate dataJS.py file... but now I have fixed it so it's 1 script!
 
-if(JSONME.lower() != 'false'):
+if (JSONME.lower() != 'false'):
     print("Now exporting to JSON")
     print(f"Targeting file: '{JSONME}'")
     time.sleep(5)