codekasten/adstreme/keyword_generator.py

117 lines
3 KiB
Python
Raw Normal View History

2006-04-05 23:27:14 +02:00
#!/usr/bin/env python
#TODO: funktion, die aus parametern schoene listen macht.
import string,sys
def add_prefix_to_strings(prefix,stringlist):
"""gets a string and a list of strings
returns a list of strings each prefixed with given string."""
ret = []
for item in stringlist:
ret.append(prefix+item)
return ret
def quotes_and_braces(keywords):
"""gets a string with keywords, returns
given string
"given string"
[given string]
"""
ret = '%s\n' % keywords
ret += '"%s"\n' % keywords
ret += '[%s]\n' % keywords
return ret
def arg_string_to_list(s):
"""gets a string.
removes all occurences of " and splits with , as separator."""
s = s.replace('"','')
ret = s.split(',')
return ret
def get_arguments():
"""
returns command line arguments as 3 lists."""
ret1 = []
ret2 = []
ret3 = []
try:
ret1 = arg_string_to_list(sys.argv[1])
except:
pass
try:
ret2 = arg_string_to_list(sys.argv[2])
except:
pass
try:
ret3 = arg_string_to_list(sys.argv[3])
except:
pass
return (ret1,ret2,ret3)
def generate_keywords(list1,list2,list3):
"""
gets 3 lists of keyword parts.
returns a string containing every possible combination of above
keywords and its generated variants from quotes_and_braces."
"""
ret = ""
for list1item in list1:
tmplist1 = []
tmplist1 = add_prefix_to_strings(list1item+" ",list2)
for tmplist1item in tmplist1:
tmplist2 = []
tmplist2 = add_prefix_to_strings(tmplist1item+" ",list3)
for tmplist2item in tmplist2:
ret += quotes_and_braces(tmplist2item)
ret += "\n\n"
return ret[:-3] #cut the tailing '\n\n\n'
def test():
#sample data
#test quotes_and_brace
keywords = "fair clothing"
expected_result = """fair clothing\n"fair clothing"\n[fair clothing]\n"""
if quotes_and_braces(keywords) != expected_result:
raise "test of quotes_and_braces() failed!"
#test add_prefix_to_strings
pref = "fair "
strlist = ["clothing","clothings"]
expected_result = ["fair clothing","fair clothings"]
if add_prefix_to_strings(pref,strlist) != expected_result:
raise "test of add_prefix_to_strings() failed!"
#test generate_keywords
list1 = ["fair"]
list2 = ["clothing","clothings"]
list3 = ["buy"]
expected_result = """fair clothing buy\n"fair clothing buy"\n[fair clothing buy]\n\n\nfair clothings buy\n"fair clothings buy"\n[fair clothings buy]\n\n\n"""
if generate_keywords(list1,list2,list3) != expected_result:
raise "test of generate_keywords() failed!"
#test arg_string_to_list(s)
s = '"clothing,clothings"'
expected_result = ['clothing', 'clothings']
if arg_string_to_list(s) != expected_result:
raise "test of arg_string_to_list() failed!"
#test get_arguments()
(list1,list2,list3) = get_arguments()
if __name__ == "__main__":
#test()
2006-04-05 23:27:14 +02:00
if len(sys.argv) !=4 :
print ' %s is a script for generating keyword lists suitable for google adwords.' % sys.argv[0]
print ' usage: %s "part1,alterative part1" "part2, another part2" ",verb"' % sys.argv[0]
else:
(list1,list2,list3) = get_arguments()
2006-04-05 23:27:14 +02:00
print generate_keywords(list1,list2,list3)