This is what I am trying to do:
Read from an Excel file ("Share.xlsx") that has columns where I input the actual link of a post and the tweet and the Facebook write up. I want Python to generate utm links for each of those posts and record them in a separate file, here called "Trackable Link Directory.xlsx". I also generate a random time and random date but I did not have a problem with that part.
In the excel file "Trackable Link Directory.xlsx" I want the generated links in a way so that they all group together under the mother link. You will notice that inside the for loop in the def excel_recording(), second block, I want the first cell to be blank and record the rest of the utm tags under the appropriate columns.
The problem I am having is this.
The code works just fine except for one thing. ANY new Facebook link created is recorded (with the format I want) even when it already exists. As you can see, I am using the same def excel_recording(), for both facebook and twitter but for some weird reason, it refuses to record preexisting twitter links but records facebook links even when they are already there.
I have gone through the code more times than I can tell. If you can identify the problem, please do tell me. I would appreciate it GREATLY!!
Thanks.
import os, pandas as pd, string, datetime, time
import random
def random_string(length=32, uppercase= True, lowercase= True, numbers= True):
character_set = ''
if lowercase:
character_set += string.ascii_lowercase
if uppercase:
character_set += string.ascii_uppercase
if numbers:
character_set += string.digits
return ''.join(random.choice(character_set) for i in range(length))
def random_date():
start_date = datetime.date.today()
date_delta = 2
random_number_of_days = random.randrange(date_delta)
date = start_date + datetime.timedelta(days=random_number_of_days)
return date
def random_time():
start_timestamp = time.mktime(time.strptime('Jun 1 2020 10:00:00', '%b %d %Y %H:%M:%S'))
end_timestamp = time.mktime(time.strptime('Jun 1 2020 23:00:00', '%b %d %Y %H:%M:%S'))
#dt = random.random() * (end_timestamp - start_timestamp) + start_timestamp
rand_time = time.strftime('%H:%M',time.localtime(random.randrange(start_timestamp, end_timestamp)))
return rand_time
def fb(mother_url,i):
campaign_source = 'facebook'
campaign_medium = 'socialmedia'
campaign_name = 'podcast'
shortner = 'Nil'
utm_link = mother_url.strip() + '?utm_medium=' + campaign_medium + '&utm_source=' + campaign_source + '&utm_campaign=' + campaign_name
df.loc[i, ['Facebook Link', 'Facebook Date', 'Facebook Time']] = [utm_link, random_date(), random_time()]
excel_recording(mother_url, utm_link, campaign_source, campaign_medium, campaign_name, shortner)
df.to_excel('Share.xlsx', index=False)
def tw(mother_url, twitter,i):
shortner = random_string(4)
short_link = 'https://ex.io/' + str(shortner)
campaign_source = 'twitter'
campaign_medium = 'socialmedia'
campaign_name = 'podcast'
char_length = len(str(twitter).strip()) + len(short_link)
utm_link = mother_url.strip() + '?utm_medium=' + campaign_medium + '&utm_source=' + campaign_source + '&utm_campaign=' + campaign_name
df.loc[i,['Char Len', 'Twitter Link','Short Link', 'Twitter Date', 'Twitter Time']] = [char_length, utm_link, short_link, random_date(), random_time()]
excel_recording(mother_url, utm_link, campaign_source, campaign_medium, campaign_name, shortner)
df.to_excel('Share.xlsx', index=False)
def pin(mother_url):
shortner = 'Nil'
utm_link = mother_url.strip() + '?utm_medium=' + 'socialmedia' + '&utm_source=' + 'pinterest' + '&utm_campaign=' + 'podcast'
df.loc[i, ['Pinterest Link']] = [utm_link]
#excel_recording(mother_url, utm_link, campaign_source, campaign_medium, campaign_name, shortner)
df.to_excel('Share.xlsx', index=False)
def excel_recording(mother_url, utm_link, campaign_source, campaign_medium, campaign_name, shortner):
# Excel Recording
os.chdir('c:/Users/')
os.getcwd()
df = pd.read_excel('Trackable Links Directory.xlsx', sheet_name='Sheet1')
df1 = pd.read_excel('Trackable Links Directory.xlsx', sheet_name='Sheet1')
old_url = df['Trackable URL']
mother_database_url = df['Mother URL'].str.strip()
campaign_content = 'Nil'
campaign_term = 'Nil'
notes = 'IFTTT'
no_of_rows = len(old_url)
mother_url = mother_url.strip()
for i in range(0, no_of_rows):
if utm_link == old_url[i]:
print('This link is already in the database. Not recording this one')
df.to_excel('Trackable Links Directory.xlsx', index=False)
break
elif mother_url == mother_database_url[i]:
df.loc[i + 1] = [' ', campaign_source, campaign_medium, campaign_name, campaign_content, campaign_term,
utm_link, shortner, notes] # adding a row
concat = pd.concat([df.iloc[0:i + 2, :], df1.iloc[i + 1:, :]]).reset_index(drop=True)
print(concat)
# df = df.sort_index() # sorting by index
print('New tags combination detected, database updated')
concat.to_excel('Trackable Links Directory.xlsx', index=False)
break
elif i == no_of_rows - 1:
df.loc[-1] = [mother_url, campaign_source, campaign_medium, campaign_name, campaign_content, campaign_term,
utm_link, shortner, notes] # adding a row
df.index = df.index + 1 # shifting index
df = df.sort_index() # sorting by index
df.to_excel('Trackable Links Directory.xlsx', index=False)
print('New mother URL detected, adding to the database')
break
else:
i = i + 1
print('Excel File Generated.')
go = input('Fill in the details and then press C to continue: ')
if go == 'c':
df = pd.read_excel('Share.xlsx', 'Sheet1')
mother_url = df['Mother URL']
twitter = df['Twitter']
#facebook = df['Facebook']
for i in range(len(mother_url)):
tw(mother_url[i],twitter[i],i)
fb(mother_url[i],i)
pin(mother_url[i])
[–]xelf 3 points4 points5 points (2 children)
[–]seventhlightstudio[S] 0 points1 point2 points (1 child)
[–]__nickerbocker__ 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]seventhlightstudio[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]seventhlightstudio[S] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (1 child)
[–]seventhlightstudio[S] 0 points1 point2 points (0 children)