-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtidal_amt.py
168 lines (140 loc) · 4.5 KB
/
tidal_amt.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
#!/usr/local/bin/python2.7
import crowdlib as cl
import crowdlib_settings
import time
import uuid
import tidal_settings as ts
import sqlite3
import os
import threading
import time
class pay_bot(threading.Thread):
def __init__(self, hitId):
threading.Thread.__init__(self)
self.hitId = hitId
def run(self):
while(True):
try:
a = cl.get_hit(self.hitId).assignments[0]
break
except:
time.sleep(5)
if(a is not None and a.is_paid is False and a.assignment_status == "Submitted"):
try:
a.approve()
print "worker pay approved"
except:
pass
class bonus_bot(threading.Thread):
def __init__(self, hitId, amount):
threading.Thread.__init__(self)
self.hitId = hitId
self.amount = amount
def run(self):
while(True):
try:
a = cl.get_hit(self.hitId).assignments[0]
break
except:
time.sleep(5)
if(a is not None):
try:
a.grant_bonus(self.amount,"payment for Tidal task (#"+self.hitId+")")
print "worker bonus approved"
except:
pass
BASE_REWARD = 0.05
TIME_LIMIT = 3600
KEYWORDS = ["tidal"]
AUTOPAY_DELAY = 3600*.5
PUB_URL = "https://crowd.ecn.purdue.edu"+ts.URL_PREFIX+"/wlogin"
MAX_ASSIGNMENTS = 1
LIFETIME = 3600*10
hit_type = cl.create_hit_type(title = "Join the rising tide!",
description = "Participate in an experimental \
crowd-working platform similar to AMT!",
reward = BASE_REWARD,
time_limit = TIME_LIMIT,
keywords = KEYWORDS,
autopay_delay = AUTOPAY_DELAY)
def init_amt_hit_db():
db_missing = not os.path.exists(ts.AMT_HIT_DB)
if(db_missing):
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
with open(ts.AMT_HIT_SCHEMA,'rt') as f:
schema = f.read()
c.executescript(schema)
conn.close()
def hit_exists(hitId):
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
c.execute('SELECT * FROM amt_hit where hitId = ?',
[hitId])
query_result = c.fetchone()
conn.close()
if(query_result is None):
return False
else:
return True
def get_hit(hitId):
if(hit_exists):
return cl.get_hit(hitId)
else:
return None
def get_assignment(assignmentId):
return cl.get_assignment(assignmentId)
def store_amt_hit(hit):
print "hitid",hit.id
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
c.execute('INSERT INTO amt_hit (hitId) values (?)',[hit.id])
conn.commit()
conn.close()
def delete_amt_hit(hitId):
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
c.execute('DELETE FROM amt_hit where hitId = ?',[hitId])
conn.commit()
conn.close()
def grant_bonus(hitId, amount):
p = bonus_bot(hitId,amount)
p.start()
def pay_worker(hitId):
p = pay_bot(hitId)
p.start()
def num_idle_amt_hits():
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
c.execute('SELECT COUNT(*) from amt_hit',[])
count = c.fetchone()[0]
conn.commit()
conn.close()
return count
def post_hit(n_tasks):
# Create a HIT type, with the title and description for this group of HITs.
# Post a HIT.
for i in range(0,n_tasks):
hit = hit_type.create_hit(url = PUB_URL,
height = 500,
max_assignments = MAX_ASSIGNMENTS,
lifetime = LIFETIME,
)
print "hit posted to URL: " , PUB_URL
store_amt_hit(hit)
# #some test code:
# print "num idle",num_idle_amt_hits()
# print "exists (true): ",hit_exists(hit.id)
# print "exists (false): ",hit_exists("@#RWEF")
# delete_amt_hit(hit.id)
# print "exists (false): ",hit_exists("@#RWEF")
def cancel_hits():
cl.set_all_hits_unavailable()
conn = sqlite3.connect(ts.AMT_HIT_DB)
c = conn.cursor()
c.execute('DELETE FROM amt_hit',[])
conn.commit()
conn.close()
print "hits cancelled!"
def report():
print "oops"