Skip to content

Commit

Permalink
added changes requested by mikey
Browse files Browse the repository at this point in the history
Added the connec.py file. Remove println statement from application controller. Reduced line length in hit_approve py files
  • Loading branch information
maddalihanumateja committed Sep 2, 2017
1 parent 3fb68e9 commit 738e8f1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
1 change: 0 additions & 1 deletion app/controllers/ApplicationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class ApplicationController @Inject() (implicit val env: Environment[User, Sessi
val timestamp: Timestamp = new Timestamp(now.getMillis)
val ipAddress: String = request.remoteAddress
val qString = request.queryString.map { case (k, v) => k.mkString -> v.mkString }
println(qString)

var referrer: Option[String] = qString.get("referrer") match{
case Some(r) =>
Expand Down
66 changes: 66 additions & 0 deletions connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
from os.path import expanduser
import boto3
import psycopg2
import psycopg2.extras
from sqlalchemy import create_engine


def connect_to_mturk():
# Get key from an external file
secret_key = {}

with open("rootkey.csv") as myfile:
for line in myfile:
name, var = line.partition("=")[::2]
secret_key[name.strip()] = str(var.strip())

# Setup mTurk parameters
#host = 'https://mturk-requester-sandbox.us-east-1.amazonaws.com/'
host = 'https://mturk-requester.us-east-1.amazonaws.com'
region_name = 'us-east-1'
aws_access_key_id = secret_key["AWSAccessKeyId"]
aws_secret_access_key = secret_key["AWSSecretKey"]

mturk = boto3.client('mturk',
endpoint_url=host,
region_name=region_name,
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
)

# Sample line of code to get account balance [$10,000.00]
print mturk.get_account_balance()['AvailableBalance']

return mturk

def connect_to_db():

home = expanduser("~")
file_path = home + "/.pgpass"
with open(file_path) as filename:

arr = []
for line in filename:
l = line.strip()
if l[0] != '#':
arr = l.split(":")

dbhost = arr[0]
dbport = arr[1]
dbname = arr[2]
dbuser = arr[3]
dbpass = arr[4]

# Format of the connection string: dialect+driver://username:password@host:port/database
connection_str = ('postgresql://' + dbuser + ':' + dbpass +
'@' + dbhost + ':' + dbport + '/' + dbname)
print connection_str
engine = create_engine(connection_str)
conn = psycopg2.connect("dbname=" + dbname +
" user=" + dbuser +
" host=" + dbhost +
" port=" + dbport +
" password=" + dbpass + "")

return conn, engine
5 changes: 3 additions & 2 deletions hit_approve_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@

'''
list_reviewable_hits() gets all HITs that are ready to be reviewed from Amazon, stored in a dict.
Each HIT is assigned to multiple (5) turkers, so we call list_assignments_for_hit() to get those
assignments using the HIT ID. We can then approve/reject each assignment using it's ID. When we
We call list_assignments_for_hit() to get those assignments using the HIT ID.
We can then approve/reject each assignment depending on whether it is present in the amt_assignment table
and if the user has submitted the correct confirmation code. When we
call approve_assignment(AsmtID), the turker is automatically paid and MTurk fees are debited.
See the following for API reference and python bindings, respectively.
Expand Down
13 changes: 9 additions & 4 deletions hit_approve_bonus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

# Get the list of all missions completed by turkers excluding onboarding and their first 500ft mission
# Filter out the missions that were already rewarded with a bonus in a previous run of this program. (These can be stored as an additional column in the mission_user_table)
# Filter out the missions that were already rewarded with a bonus in a previous run of this program.
# (These can be stored as an additional column in the mission_user_table)
# Calculate the distance associated with the completed unpaid missions
# Calculate bonus amount based on distance audited
# Use the boto function award a bonus to that user. (Find the same user in the amt_assignment table and get the hit id and assignment id for this)
# Use the boto function award a bonus to that user.
#(Find the same user in the amt_assignment table and get the hit id and assignment id for this)

cur.execute("""SELECT username,mission.mission_id,mission_user.mission_user_id,mission.label,region_id,distance,distance_ft,distance_mi, hit_id, assignment_id, paid
cur.execute("""SELECT username,mission.mission_id,mission_user.mission_user_id,mission.label,
region_id,distance,distance_ft,distance_mi, hit_id, assignment_id, paid
from mission_user
join mission on(mission.mission_id = mission_user.mission_id)
join user_role on(user_role.user_id=mission_user.user_id and user_role.role_id=4)
Expand Down Expand Up @@ -63,7 +66,9 @@
reason = "Bonus of $" + str(bonus) + " paid for completing a "+str(row['mission_distance'])+" mile long mission on project sidewalk"

if(send_bonuses):
response = mturk.send_bonus(WorkerId=row['username'],BonusAmount=str(bonus),AssignmentId=row['assignment_id'],Reason=reason,UniqueRequestToken=row['username']+row['assignment_id']+row['mission_user_id'])
response = mturk.send_bonus(WorkerId=row['username'],BonusAmount=str(bonus),
AssignmentId=row['assignment_id'],Reason=reason,
UniqueRequestToken=row['username']+row['assignment_id']+row['mission_user_id'])

print reason
print response
Expand Down

0 comments on commit 738e8f1

Please sign in to comment.