Advertisement
Guest User

CPM-Project0.1

a guest
Aug 19th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Aug 15 19:24:17 2016
  4.  
  5. @author: Malte
  6. """
  7. import requests
  8. import json
  9. import base64
  10. import time
  11. from datetime import datetime
  12.  
  13. urlbasis="http://stable.alpha-trader.com/"
  14.  
  15. def dateTime(days):
  16.     # datetime helpfunction
  17.     now=datetime.now()
  18.     now=time.mktime(now.timetuple())
  19.     maturityDate=now+days*60*60*24
  20.     maturityDate=int(maturityDate)
  21.     maturityDate=str(maturityDate)+"000"
  22.     return maturityDate
  23.     print(maturityDate)
  24.  
  25. class Account(object):
  26.    
  27.     partnerID = "986ffb5a-430e-4cf6-82c3-044f30052548"
  28.    
  29.     def __init__(self, user, password):
  30.         self.user = user
  31.         if password == "password":
  32.             self.password = "pw!"+user
  33.         else:            
  34.             self.password = password
  35.         self.mail=self.user+"qwertzuiolfh@gmx.de"
  36.         self.header()
  37.         self.headerShort()
  38.                        
  39.     def __str__(self):
  40.         self.header= "No header yet"
  41.         self.headerShort = "No header yet"
  42.         self.chatstring = "no Account"
  43.        
  44.     def header(self):
  45.         auth="Basic " + base64.encodestring(self.user+":" + self.password)
  46.         auth=auth.replace('\n', '')
  47.         header={
  48.             'authorization': auth,
  49.             'x-authorization': self.partnerID,
  50.             'cache-control': "no-cache",
  51.                 }
  52.         self.header = header
  53.         return header
  54.        
  55.     def headerShort(self):
  56.         auth="Basic " + base64.encodestring(self.user+":" + self.password)
  57.         auth=auth.replace('\n', '')
  58.         header={
  59.             'x-authorization': self.partnerID,
  60.             'cache-control': "no-cache",
  61.                 }
  62.         self.headerShort = header    
  63.                
  64.     def register(self):
  65.         url=urlbasis+"user/register"
  66.         querystring={"username": self.user, "emailAddress": self.mail,"password": self.password}
  67.         requests.request("POST", url, headers=self.headerShort, params=querystring)
  68.         time.sleep(5)
  69.         self.quitChat()
  70.        
  71.     def newCompany(self):
  72.         url=urlbasis+"api/companies/"
  73.         compName=self.user+"Limited"
  74.         querystring={"name": compName, "cashDeposit": "50000"}
  75.         requests.request("POST", url, headers=self.header, params=querystring)
  76.    
  77.     def getChatstring(self):
  78.         url=urlbasis+"api/chats"
  79.         response=requests.request("GET", url, headers=self.header)
  80.         parsed_json=json.loads(response.text)
  81.         chatstring=parsed_json[0]['id']
  82.         self.chatstring=chatstring
  83.         print(response)
  84.    
  85.     def quitChat(self):
  86.         self.getChatstring()
  87.         url=urlbasis+"api/chats/quitchat/"+self.chatstring
  88.         requests.put(url, headers=self.header)
  89.    
  90. class Company(Account):
  91.    
  92.     def __init__(self, compName):
  93.         self.masterUser()
  94.         self.compName=compName
  95.         self.account()
  96.         Account.__init__(self, user=self.user, password="password")
  97.                
  98.     def newCompany(CompName, user):
  99.         url=urlbasis+"api/companies/"
  100.         querystring={"name": CompName, "cashDeposit": "50000"}
  101.         header=user.header
  102.         requests.request("POST", url, headers=header, params=querystring)
  103.    
  104.     def masterUser(self):
  105.         UserName="Malte"
  106.         password="XYZ"
  107.         auth="Basic " + base64.encodestring(UserName+":" + password)
  108.         auth=auth.replace('\n', '')
  109.         header={
  110.             'authorization': auth,
  111.             'x-authorization': "986ffb5a-430e-4cf6-82c3-044f30052548",
  112.             'cache-control': "no-cache",
  113.                 }
  114.         self.masterUser=header
  115.    
  116.     def account(self):
  117.         url=urlbasis+"api/search/companies/"+self.compName
  118.         response=requests.request("GET", url, headers=self.masterUser)
  119.         parsed_json=json.loads(response.text)
  120.         self.securitiesAccountID=parsed_json[0]['securitiesAccountId']
  121.         self.user=parsed_json[0]['ceo']['username']
  122.         self.userID=parsed_json[0]['ceo']['id']
  123.         self.compID=parsed_json[0]['id']
  124.  
  125.     def order(self, securityIdentifier, action, type2, price, numberOfShares):
  126.         querystring={"owner": self.securitiesAccountID, "securityIdentifier": securityIdentifier, "action": action, "type": type2, "price": price, "numberOfShares":numberOfShares}
  127.         url=urlbasis+"api/securityorders/?"
  128.         response=requests.request("POST", url, headers=self.header, params=querystring)
  129.         print(response)
  130.        
  131.     def newBond(self, numberOfBonds, faceValue, interestRate, maturityDate):
  132.         url=urlbasis+"api/bonds/"
  133.         maturityDate=dateTime(maturityDate)
  134.         querystring={"companyId": self.compID, "numberOfBonds": numberOfBonds, "faceValue": faceValue, "interestRate": interestRate, "maturityDate": maturityDate}
  135.         response=requests.request("POST", url, headers=self.header, params=querystring)  
  136.         if str(response.status_code) == "201":
  137.             print("Bond erstellt")
  138.         else:
  139.             print("Bond nicht erstellt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement