Skip to content

Instantly share code, notes, and snippets.

@lilactown
Created April 24, 2013 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilactown/5454111 to your computer and use it in GitHub Desktop.
Save lilactown/5454111 to your computer and use it in GitHub Desktop.
Python script which downloads a file from a URL to a given file name, then checks to see if a certain phrase is contained in it.
#! /usr/bin/env python
# Description:
# Downloads a file from a URL to a given file name, then checks to see if a certain phrase is
# contained in it.
#
#Instructions:
# Running the script is easy: 'python puppet.py <URL> <FILE>' will download data from <URL> over
# HTTP and save it locally to <FILE>. It will then search for the default phrase 'position = support-intern',
# and if it does not find it, append the phrase to the end of the file and then check again.
#
# For additional commands and ways to test the script, use 'python puppet.py -h'
#
# By Will Acton
import sys
from random import randint
import getopt
import urllib2
import urlparse
search_phrase = "position = support-intern"
def check_phrase(file, phrase): # Search file for a particular phrase; if it isn't there, write it at the end
print "Searching file '"+file.name+"' for '"+phrase+"'..."
for line in file: # NOTE: 'file' is a file object passed to the function. Usually in read-only to avoid creating a new file
if phrase in line:
#print line
print "Found."
return 1
print "Not found."
try: append = open(file.name, 'a')
except: print "Error opening file!"
else:
append.write("\n"+phrase) # append the search phrase to the file
append.close()
print "Line appended to file.\n"
return 1
return 0
def download_puppet(url_str, file_str, check=1, phrase=search_phrase): # Download a file if check = 0, check if the search phrase exists
url_obj = urlparse.urlsplit(url_str)
if url_obj.scheme == "http":
try: puppet = urllib2.urlopen(url_obj.geturl())
except urllib2.HTTPError, err:
print err
return 0
print "Downloading '"+url_obj.geturl()+"'..."
web_file = open(file_str, 'wb')
web_file.write(puppet.read())
web_file.close()
if check:
local_file = open(file_str, 'r')
k = check_phrase(local_file, phrase)
local_file.close()
return k
return 1
else: print "URL error."
return 0
def print_help():
print "Usage:\npuppet.py <url> <file> [<phrase>]: download <url> to <file> and check for '<phrase>' (default is 'position = support-intern')" \
"\npuppet.py -d <url>: download data from a url" \
"\npuppet.py -c <file>: check a file for the default phrase" \
"\npuppet.py -p <url> <file> <phrase>: download <url> to <file> and check for '<phrase>'" \
"\npuppet.py -g <file> [<true/false>]: generate a test file to be searched. If true, inserts 'position = support-intern' phrase. Default is false."
try: opts, args = getopt.getopt(sys.argv[1:],"dcpg")
except getopt.GetoptError: # If unknown argument appears
print_help()
sys.exit()
if not opts and len(args) == 2: # puppet.py <url> <file>
if download_puppet(args[0], args[1]): # search for default phrase
try: local_file = open(args[1], 'r')
except: "Error re-opening file!"
else:
check_phrase(local_file, search_phrase)
local_file.close()
print "Success! Exiting..."
else:
print "Failure. :( Exiting..."
elif args: # puppet.py -<h/d/c/p/g> <args[0]> <args[1]>
for opt in opts:
if opt[0] == '-d' and len(args) > 1: # puppet.py -d <url> <file>
if download_puppet(args[0], args[1], 0): print "Success!" # Try downloading it
else: print "Invalid URL."
if opt[0] == '-c': # puppet.py -c <file>
try: local_file = open(args[0], 'r')
except: print "Error opening file!"
else:
check_phrase(local_file, search_phrase)
local_file.close()
if opt[0] == '-p' and len(args) > 2: # puppet.py -p <url> <file> <phrase>
if search_puppet(args[0],args[1], 1, args[2]): print "Success! Exiting..."
else: print "Failure. :( Exiting..."
if opt[0] == '-g': # Generate a test file with 100 lines, inserting the search phrase
try: local_file = open(args[0], "w")
except: print "Error opening file. :("
else:
print "File created. Writing to file..."
ins = -1
if len(args) == 2 and args[1] == "true":
ins = randint(1,100) # Pick a line number, any line number to insert the phrase
for n in range(0,100):
if n == ins: # check if the line number matches to insertion point
local_file.write(search_phrase)
else:
local_file.write(str(n)+"\n")
local_file.close()
print "Done writing!"
else:
print_help()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment