This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathodesk_jobs.py
181 lines (152 loc) · 6.83 KB
/
odesk_jobs.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
import odesk
from datetime import datetime, timedelta
import mechanize,cookielib
import re
import sys
import json
class odesk_jobs(NebriOS):
schedule = "0 0 * * *" # daily
app_key = '' # your oDesk application key
app_secret = '' # oDesk application secret key
#to get oDesk api tokens you need:
username = '' # your oDesk login
password = '' # your oDesk password
def login_to_odesk(self, client, br):
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages?
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# User-Agent
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
# Open odesk site
r = br.open('https://www.odesk.com/login.php')
form = br.forms().next() # actually login form name is 'login' but there is only one form
print 'Logging in to oDesk using URL', form.action
form['username'] = self.username
form['password'] = self.password
br.form = form
r = br.submit()
html = r.read()
m = re.match('[^]*(Invalid username, email or password)[^]*', html)
if m.groups():
raise RuntimeError, 'Cannot login to oDesk: Invalid username, email or password'
else:
print 'Login successful'
def get_verifier(self, client, br):
verifier = ''
if client and br:
url = client.auth.get_authorize_url()
print 'Getting verifier using URL', url
r = br.open(url)
html = r.read()
# authorize our application (if needed)
for form in br.forms():
if form.attrs['id'] == 'authorize':
print "Sending authorize"
br.form = form
r = br.submit()
html = r.read()
break
# check that our verifier is on form and return it
m = re.search('Your oauth_verifier=([a-zA-Z0-9]+)<\/div>', html)
if m:
verifier = m.groups()[0]
print 'Verifier received:', verifier
else:
print "Something is wrong in initialisation: client:", client, "browser:", br
return verifier
def authorize(self):
# for details go to https://developers.odesk.com/?lang=python#authentication_oauth-10
client = odesk.Client(public_key=self.app_key, secret_key=self.app_secret)
# 1. Get request token
request_token, request_token_secret = client.auth.get_request_token()
print 'Received request_token:', request_token, ', request_token_secret:', request_token_secret
# 2. Authorize and get verifier
# Browser
br = mechanize.Browser()
self.login_to_odesk(client, br)
verifier = self.get_verifier(client, br)
# 3. Get access token
access_token, access_token_secret = client.auth.get_access_token(verifier)
# 4. update and save new tokens
if access_token and access_token_secret:
client = odesk.Client(public_key=self.app_key, secret_key=self.app_secret,
oauth_access_token=access_token, oauth_access_token_secret=access_token_secret,
fmt='json')
self.access_token = access_token
self.access_token_secret = access_token_secret
return client
return
def search_jobs(self, client):
if not client:
print('Client must be initialized before calling job_search')
raise RuntimeError, 'Client is not initialized'
request_params = {'q':'python',
'job_status': 'open',
'days_posted': 5,
'budget': '1000-',
'client_hires': '15-',
'skills':['python'] }
response = client.provider_v2.search_jobs(data=request_params)
if response:
jobs_found = response
return jobs_found
return
def filter_date_created(self, jobs_found, days_ago=7):
if not jobs_found:
return
jobs_filtered = []
#get current datetime in utc because server gives us response in utc
week_ago = datetime.utcnow() - timedelta(days=days_ago)
for job in jobs_found:
date_created = datetime.strptime(job[u'date_created'], "%Y-%m-%dT%H:%M:%S+%f")
if (date_created.isoformat() > week_ago.isoformat() ):
jobs_filtered.append(job)
print 'Total jobs found: ', len(jobs_found)
print 'Filtered jobs: ', len(jobs_filtered)
self.jobs_filtered = jobs_filtered
return jobs_filtered
def check(self):
# 3 atempts to get acess tokens and create odesk clint
for a in range(3):
# if we have all tokens - just create oDesk Client
if self.access_token and self.access_token_secret:
client = odesk.Client(public_key=self.app_key, secret_key=self.app_secret,
oauth_access_token=self.access_token, oauth_access_token_secret=self.access_token_secret,
fmt='json')
else: #else we have to login and get access tokens
client = self.authorize()
if not client: continue
# get jobs from oDesk:
try:
jobs = self.search_jobs(client)
except RuntimeError, err:
print 'Error while searching jobs on oDesk:', err
continue
if not jobs: continue
jobs_filtered = self.filter_date_created(jobs)
if not self.jobs_filtered: continue
else: return True
#if during 3 attempts we didn't return True, nothing is found or error occured.
return False
def action(self):
if not shared.compatible_odesk_jobs:
shared.compatible_odesk_jobs = []
if not self.jobs_filtered:
return
for job in self.jobs_filtered:
shared.number_of_elements_in_list_at_action = len(self.jobs_filtered)
shared.compatible_odesk_jobs.append( {'job' : job['title'],
'url' : job['url'],
'date_created': job['date_created']})