-
Notifications
You must be signed in to change notification settings - Fork 0
/
renewer.py
executable file
·248 lines (203 loc) · 8.26 KB
/
renewer.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import ConfigParser
import cStringIO
import logging
import os
import pickle
import sys
import webbrowser
import zlib
try:
import requests
from bs4 import BeautifulSoup as Soup
except:
print "A required library was not found. Please see https://github.com/Sepero/CraigsRenewer#dependencies"
raise
# Todo
# Check for update
# Schedule loop
# Make sure only one instance is running
LOGGER = logging.getLogger(__name__)
class WebHandler(object):
def __init__(self, useragent=""):
self.session = requests.Session()
self.session.verify = True
self.session.headers['User-Agent'] = useragent
def open_url(self, url, method='get', data='', save_file=None):
LOGGER.debug("open_url() %s %s", method, url)
if data:
LOGGER.debug("URL data: %s", data)
try: # TODO: Move loop outside try/except
for loop in range(6): # Retry 6 up to times, 10 seconds each try.
if method.lower() == 'get':
pull = self.session.get
else:
pull = self.session.post
response = pull(url, data=data, timeout=10)
if response.status_code != 200:
logging.error("Page error: %s %d", url, response.status_code)
if save_file:
with open(save_file, 'w') as filehandle:
filehandle.write(response.text)
return response.text
except:
logging.error("Could not connect")
raise
def open_browser(self=None, url=''):
"""
Open a webpage in the desktop default browser like Firefox.
"""
webbrowser.open(url, new=1)
class Updater(object):
def check(self):
web = WebHandler()
#web.open_url("http://update url")
class Listing(object):
"""
This is a simple class to hold all the listings to renew.
It's primary function is to improve code readability.
"""
def __init__(self, title="", url="", data={}):
self.title = title
self.url = url
self.data = data
def __str__(self):
return '"%s" %s %s' % (self.title, self.url, self.data)
class RenewHandler(object):
config_files = [] # List of possible config file locations.
config_files.append(os.path.join(os.path.expanduser("~"), '.craigsrenewer'))
config_files.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '.craigsrenewer'))
def __init__(self):
for cfile in self.config_files:
try:
self.config = ConfigParser.SafeConfigParser()
self.config.readfp(open(cfile), cfile)
print "Using config file: %s" % cfile
break
except IOError:
LOGGER.debug("Error reading: %s", cfile, exc_info=True)
if not self.config.sections():
logging.error("ERROR: No configuration file found. %s", self.config_files)
raise IOError("No configuration file found.")
# This variable is a flag to let the GUI know if backend is currently renewing.
self.processing = False
def begin_renew_process(self):
""" Starts loop of renewing listings on all accounts. """
LOGGER.info("Beginning renew processes.")
# This variable is a flag to let the GUI know if backend is currently renewing.
self.processing = True
# Get login accounts from config.
accounts = self.get_login_accounts()
print "Beginning renewal processes for %d accounts." % len(accounts)
for account in accounts:
# Create web browser session.
if self.config.has_option("useragent", "name"):
self.web = WebHandler(self.config.get("useragent", "name"))
else:
self.web = WebHandler("")
# Log into website.
print
print "Logging in as account '%s'." % account['user']
page = self.log_into_site(account)
# Is page requesting captcha.
if self.check_for_captcha(page):
print "Captcha was found. Please login to your account manually."
print "account '%s'" % account['user']
continue
# Verify that login was successful.
if not self.is_login_success(page):
print "Account '%s' did not appear to login correctly." % account['user']
# Find items to renew.
listings = self.get_renewable_listings(page)
# Renew each item.
if not listings:
print "No listings to renew."
else:
self.renew_listings(listings)
# This variable is a flag to let the GUI know if backend is currently renewing.
self.processing = False
def get_login_accounts(self):
"""
Extracts accounts and info from config and generates a list
of dictionaries.
"""
config = self.config
accounts = []
for section in config.sections():
if section.lower().startswith("account"):
d = {"user": config.get(section, "user"),
"pass": config.get(section, "pass")}
accounts.append(d)
return accounts
def log_into_site(self, account):
"""
*account* is a Python dictionary containing login information
for a single account. It will login using the information.
Returns the page after attempting login.
"""
url = "https://accounts.craigslist.org/login"
page = self.web.open_url(url)
data = {}
for e in Soup(page).find('form', attrs={'name': 'login'}).findAll("input"):
try:
data[e['name']] = e['value']
except(KeyError, TypeError):
pass
data.update({'inputEmailHandle': account['user'],
'inputPassword': account['pass']})
page = self.web.open_url(url, data=data, method='post', save_file='listing.html')
return page
def check_for_captcha(self, page):
result = Soup(page).find('div', id="recaptcha_table")
LOGGER.info("Captcha result: %s" % result)
return result
def is_login_success(self, page):
return Soup(page).find('p', text='Showing all postings')
def get_renewable_listings(self, page):
"""
Scrape page for renewable listings.
Return a list of Listing objects.
"""
listings = []
for item in Soup(page).findAll('input', attrs={
'name': 'go', 'value': 'renew', 'type': 'submit', 'class': 'managebtn'}):
url = item.parent['action']
data = {}
# for subitem in Soup(item).findAll('input'):
while item:
# Get name/value pairs for every nested input inside a form.
data[item['name']] = item['value']
logging.debug("item.previous_sibling %s" % item.previous_sibling)
#item = item.parent
item = item.previous_sibling
logging.debug("data %s" % data)
title = Soup(page).find('a', href=url).text
tmplisting = Listing(title, url, data)
logging.debug(tmplisting)
listings.append(tmplisting)
logging.info("Listings to renew %d", len(listings))
return listings
def renew_listings(self, listings):
for tmplisting in listings:
try:
logging.debug("tmplisting.data %s" % tmplisting.data)
self.web.open_url(tmplisting.url, 'post', tmplisting.data)
print "Update success: %s" % tmplisting.title
logging.info("Update success: %s" % tmplisting.title)
except:
print "Update failed: %s" % tmplisting.title
logging.warning("Update failed: %s" % tmplisting.title)
raise
def main():
renewhand = RenewHandler()
if '--debug' in sys.argv:
logging.basicConfig(level=logging.DEBUG)
renewhand.begin_renew_process()
Updater().check()
exit()
if __name__ == '__main__':
import signal
# This allows the program to exit quickly when pressing ctrl+c.
signal.signal(signal.SIGINT, signal.SIG_DFL)
main()