-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_creatives.py
82 lines (65 loc) · 1.91 KB
/
save_creatives.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pandas as pd
import requests
import sys
import os
def verify_input():
if len(sys.argv) < 2:
files = os.listdir()
for file in files:
print(file)
if file.endswith(".xlsx"):
file = file
print(f"using {file}")
return file
else:
file = sys.argv[1]
if not file.endswith(".xlsx"):
sys.exit("Input file must be an excel file (.xlsx)")
return file
sys.exit("No excel file")
def make_entry(row):
entry = {}
entry["link"] = row[0]
entry["headline"] = row[1]
entry["campaign"] = row[2]
entry["creatives"] = [row[3], row[4], row[5]]
return entry
def save_creatives(entry):
# download images to folder
for image in entry["creatives"]:
r = requests.get(image)
# save the image to directory
with open(image.split("/")[-1], "wb") as f:
f.write(r.content)
# make a text file named info.txt
with open("info.txt", "w") as f:
f.write(f"Headline: {entry['headline']}\n")
f.write(f"Link: {entry['link']}\n")
f.write(f"Campaigns: {entry['campaign']}")
def main():
file = verify_input()
df = pd.read_excel(file, header=None)
registry = []
for index, row in df.iterrows():
entry = make_entry(row)
registry.append(entry)
counter = 0
for entry in registry:
counter += 1
dir = entry["campaign"]
# create folder for campaign
try:
os.mkdir(dir)
os.chdir(dir)
save_creatives(entry)
# go back to parent directory
os.chdir("..")
print(f"{counter}/{len(registry)}")
# clear the screen
print("")
print("\033c", end="")
except FileExistsError:
print(f"{dir} already exists")
continue
if __name__ == "__main__":
main()