-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmazonListingGenerator.py
213 lines (159 loc) · 6.85 KB
/
AmazonListingGenerator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# Developed by Alex Israelov
# Alex Enterprise
#
# Dear Craigslist:
# Your site has too much email integration, and needs an API
# even though you strongly disallow bots like this one.
# also thanks for providing a free affiliate marketing platform.
import os
import time
import Listing
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from pyvirtualdisplay import Display
import spintax
import requests
from amazonify import amazonify
class AmazonListingGenerator:
#region Helper methods
#region WaitFor methods
def waitForName(self, name):
for i in range(0, 30):
#self.debug("waiting for id \"" + name + "\"...")
if len(self.client.find_elements_by_name(name)) != 0:
break
time.sleep(2)
def waitForId(self, idName):
for i in range(0, 30):
#self.debug("waiting for id \"" + idName + "\"...")
if len(self.client.find_elements_by_id(idName)) != 0:
break
time.sleep(2)
def waitForCss(self, css):
for i in range(0, 30):
#self.debug("waiting for css selector \"" + css + "\"...")
if len(self.client.find_elements_by_css_selector(css)) != 0:
break
time.sleep(2)
def waitForClass(self, className):
for i in range(0, 30):
#self.debug("waiting for class \"" + className + "\"...")
if len(self.client.find_elements_by_class_name(className)) != 0:
break
time.sleep(2)
#endregion
@staticmethod
def debug(inString):
print(" [AMAZON] - %s" % inString.encode('utf-8').strip())
#endregion
def __init__(self, amazonLinksFile):
self.display = ""
if not os.name == 'nt':
self.display = Display(visible=0, size=(1248, 1000)) # 800x600
self.display.start()
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.offline.enable", False)
profile.set_preference("network.http.use-cache", False)
self.client = webdriver.Firefox(firefox_profile=profile)
self.amazonLinksFile = amazonLinksFile
self.affiliateTag = 'goodtastyfrui-20'
def __del__(self):
if not os.name == 'nt':
self.display.stop()
self.client.quit()
return 0
# formats an Amazon link with your affiliate tag
def formatAmazonLink(self, link):
formattedLink = amazonify(link, self.affiliateTag)
# self.debug("formattedLink: " + formattedLink)
return formattedLink
# returns a list of Listing objects
def generateListingList(self):
i = 1
listings = []
f = open("links.txt", "r") # open listings file
f1 = f.readlines()
for x in f1: # for each link
if("###" in x):
break
try:
self.debug("Getting listing " + str(i) + "/" + str(len(f1) - 1))
newListing = self.generateListing(x) # create a new listing from that amazon link
listings.append(newListing) # add the listing to the list
except:
self.debug("Couldn't process link: " + x)
i += 1
return listings # return the list
# returns a Listing object constructed from an Amazon link
def generateListing(self, amazonLink):
title = ""
desc = ""
formattedLink = ""
# self.debug("Go to Amazon link")
self.client.get(amazonLink)
# region Get title and description
# self.debug("Grabbing title")
self.waitForId("productTitle")
title = self.client.find_element_by_id("productTitle").text
# self.debug("Grabbing description")
desc = self.client.find_element_by_id("productDescription").text
# endregion
# region Get images
self.waitForId("imageBlock")
imagesBlock = self.client.find_element_by_id("imageBlock")
time.sleep(4)
imageElementList = imagesBlock.find_elements_by_css_selector('img')
imagePaths = [] # save all image paths here
i = 0
for image in imageElementList:
if i > 8: # don't allow more than 8 images
break
# self.debug("Downloading image")
titleString = title.encode("utf-8") # convert the title to a string
imagePath = 'images/' + titleString[0:14] + " " + str(i) + ".png" # specify image path
imageSrc = imageElementList[i].get_attribute('src') # get the image source
#self.debug("imageSrc: " + imageSrc)
#self.debug("imagePath: " + imagePath)
# write the image source to the image path
with open(imagePath, 'wb') as handle:
response = requests.get(imageSrc, stream=True)
if not response.ok:
print(response)
for block in response.iter_content(1024):
if not block:
break
handle.write(block)
i += 1
imagePaths.append(imagePath)
# endregion
# region Format amazon link
# self.debug("Formatting Amazon link: " + amazonLink)
formattedLink = self.formatAmazonLink(amazonLink)
# We're using amzn.pw. This site is dogshit so plan to migrate to another in the future
# self.debug("Shortenening link, loading amzn.pw")
self.client.get("https://amzn.pw?url=" + amazonLink)
# Reload the hompage by clicking main logo
self.waitForClass("mlogo")
self.client.find_element_by_class_name("mlogo").click()
time.sleep(2)
# Wait for the new field and then put the link in
self.waitForId("urlin")
self.client.find_element_by_id("urlin").send_keys(formattedLink)
self.client.find_element_by_id("shortenurl").click()
# Like I said, this website is dogshit and will sometimes give us cached links.
# Idk why running it again works... but it works. I guess.
self.client.get("http://example.com")
self.client.get("https://amzn.pw?url=" + amazonLink)
self.client.find_element_by_id("urlin").send_keys(formattedLink)
self.client.find_element_by_id("shortenurl").click()
# page displays a QR code when complete
self.waitForClass("qr")
linkDiv = self.client.find_element_by_class_name("short-url")
formattedLink = linkDiv.find_element_by_css_selector("a").get_attribute('href')
# self.debug("newLink: " + formattedLink)
# endregion
newListing = Listing.Listing(title, desc, formattedLink, imagePaths)
return newListing