-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch
executable file
·175 lines (135 loc) · 5.82 KB
/
search
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
#!/usr/bin/python
import logging
import datetime
import requests
import sys
import smtplib
from bs4 import BeautifulSoup
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import urllib2
import imageInfo
class EcaytradeSearch:
def __init__(self):
self.from_email = "[email protected]"
self.from_name = "Ecaytrade Search <" + self.from_email + ">"
gmail_user = self.from_email
with open ("password", "r") as password:
gmail_pwd = password.read().replace('\n', '')
s = smtplib.SMTP("smtp.gmail.com",587)
s.ehlo()
s.starttls()
s.ehlo
s.login(gmail_user, gmail_pwd)
self.smtp = s
def run(self, to_email, category, search_terms, all):
logging.debug('search terms: {}'.format(search_terms))
payload = {
'do_search' : 'Search',
'searchword' : ' '.join(search_terms),
'catid_search' : category
}
days = 31 if all else 1
res = requests.post("http://www.ecaytrade.com/search.php", params=payload)
if res.status_code is not 200:
print result
sys.exit(1)
soup = BeautifulSoup(res.content, "lxml")
search_results = soup.find_all("td", { "class": "pointer" })
logging.debug('{} results'.format(len(search_results)))
for search_result in search_results:
parent_node = search_result.parent
date_added = parent_node.find_all("td")[2].text
if datetime.datetime.strptime(date_added, '%Y.%m.%d') < datetime.datetime.today() - datetime.timedelta(days=days):
continue
title = search_result.text.strip().encode('utf-8').strip()
url = search_result.find("a")['href']
link = "<a href='http://www.ecaytrade.com/" + url + "'>View ad on web</a>"
logging.debug('-----------------------------------------------------------------')
logging.debug('title : {}'.format(title))
logging.debug('link : {}'.format(link))
logging.debug('date_added: {}'.format(date_added))
item_data = requests.get("http://www.ecaytrade.com/" + url)
item_soup = BeautifulSoup(item_data.content, "lxml")
item_container = item_soup.find("table", {"id": "container"})
item_tables = item_container.find_all("table")
item_results = item_tables[5]
item_trs = item_results.find_all("tr")
item_tr = item_trs[2]
item_details = item_tr.find("table")
item_details_trs = item_details.find_all("tr")
item_details_text = item_details_trs[1].text.strip()
item_details_text = item_details_text.encode('utf-8').strip()
logging.debug('details : {}' + item_details_text)
# convert newlines into breaks
item_details_text = item_details_text.replace('\n', '<br>\n')
seller_email = item_details_trs[6]
seller_email_url = seller_email.find('a')['href']
seller_email_link = "<a href='http://www.ecaytrade.com/" + seller_email_url + "'>Contact seller</a>"
logging.debug('contact : {}'.format(seller_email_link))
images = ''
thumbs = item_soup.find_all("a", {"class": "thumb"})
for i in thumbs:
img = i.find("img")
src = img['src']
url = src.replace('_tmb1', '')
href = 'http://www.ecaytrade.com/' + url
try:
imgdata = urllib2.urlopen(href)
image_type,width,height = imageInfo.getImageInfo(imgdata)
img = '<img src="' + href + '" width="' + str(width) + '" height="' + str(height) + '">'
images += img + '<br>\n'
except:
pass
html = """\
<html>
<head></head>
<body>
<p>
<h1>{}</h1>
<br>
{}
</p>
<p>
{}
<br>
Date added: {}
<br>
</p>
<p>
{}
</p>
<p>
{}
</p>
</body>
</html>
""".format(title, item_details_text, link, date_added, seller_email_link, images)
logging.debug(html)
msg = MIMEMultipart('alternative')
msg['Subject'] = '"{}" search result: {}'.format(' '.join(search_terms), title)
msg['From'] = self.from_name
msg['To'] = to_email
msg.attach(MIMEText(html, 'html'))
self.smtp.sendmail(self.from_name, to_email, msg.as_string())
def __del__(self):
if self.smtp:
self.smtp.quit()
def parse_args():
import argparse
parser = argparse.ArgumentParser(description='ecayTrade search')
parser.add_argument('--email', dest='email', help='email address to send to')
parser.add_argument('--category', dest='category', help='category id', default=0)
parser.add_argument('--search', dest='search_terms', nargs = '+', help='search terms')
parser.add_argument('--all', dest='all', action='store_true', help='send all results')
parser.add_argument('--debug', dest='debug', action='store_true', help='debug mode logging')
args = parser.parse_args()
logging.getLogger('').setLevel(logging.DEBUG if args.debug else logging.WARNING)
return args
def main():
args = parse_args()
search = EcaytradeSearch()
search.run(args.email, args.category, args.search_terms, args.all)
return 0
if __name__ == '__main__':
sys.exit(main())