#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 11:59:16 2017

@author: apogee_tpaschke
"""

import re
import pprint
import itertools
import copy
import csv
from collections import Counter
from itertools import islice
import pandas as pd 

file = open('/Users/apogee_tpaschke/Downloads/searchterms.csv')
csv_f = csv.reader(file)

# make a raw list that extracts all the raw urls from the csv
raw_list =[]
url_list = []
clean_list = []
search_list = []
act_list =[]
adv_list = []

csv_f = islice(csv_f,19,None, None)

for row in csv_f:
        if row[1] != "":
            raw_list.append(row[0:2])
        else:
            None

file.close()

df = pd.DataFrame(raw_list)
df.columns = ['Activity','Referrer']

adv = df.Activity.unique()

for i in adv:
    print(i)


#==============================================================================
# adv = adv.reset_index 
# adv = adv[0].tolist()
# print(adv)
#==============================================================================






#==============================================================================
# 
# for row in raw_list:
#     act_list.append(row[0])
#     
# for row in act_list:
#     adv_list + row
#     
#     
# 
# activity = set(adv_list)
# 
# print(activity)
# 
#==============================================================================


# make a raw list that extracts all the raw urls from the csv
#==============================================================================
# for row in raw_list:
#     url_list.append(row)
# 
# # make a new url list that only accepts urls with more than 3 parsed elements to ensure they have search terms 
# for url in url_list:
#     parse = re.split(r'(=+|&+)', url)
#     if len(parse) < 3:
#         del(url)
#     else:
#         clean_list.append(parse)
# 
# #this is the for loop that parses and returns the search terms for each referral.
# # a & b are different variables that help identify if the search was performed directly through bing or through a form on bing
# 
# for url in clean_list:
#         a = str(url[2].lower())
#         b = str(url[0])
#         if "bing" not in b:
#                 search = "n/a"
#         elif "FORM" in b:
#                 c = str(url[10].lower())
#                 search = c.split("+")
#         elif "%20" in a:
#                 search = a.split("%20")
#         elif "www." in a:
#                 search = a.split("www.")
#         else:
#                 search = a.split("+")
#         output = search_list.append(search)
# 
# 
# 
# #copy the final iteration of the list to memory so it can be references outside the for loop
#         copy.deepcopy(search_list)
#         
# #combine all the individual search query lists into one master list 
# final_list = list(itertools.chain.from_iterable(search_list))
# 
# #count each unique word and return totals, exlcude any words that show up < 5 times 
# counts = Counter(final_list)
# top_terms=counts.most_common(20)
# pprint.pprint(top_terms)
#==============================================================================
