Advertisement
Guest User

CPM-Project0.1

a guest
Aug 18th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 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 ApiCom:
  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.        
  54.     def headerShort(self):
  55.         auth="Basic " + base64.encodestring(self.user+":" + self.password)
  56.         auth=auth.replace('\n', '')
  57.         header={
  58.             'x-authorization': self.partnerID,
  59.             'cache-control': "no-cache",
  60.                 }
  61.         self.headerShort = header    
  62.                
  63.     def register(self):
  64.         url=urlbasis+"user/register"
  65.         querystring={"username": self.user, "emailAddress": self.mail,"password": self.password}
  66.         requests.request("POST", url, headers=self.headerShort, params=querystring)
  67.         time.sleep(2)
  68.         self.quitChat()
  69.        
  70.     def newCompany(self):
  71.          url=urlbasis+"api/companies/"
  72.          compName=self.user+" Limited"
  73.          querystring={"name": compName, "cashDeposit": "50000"}
  74.          requests.request("POST", url, headers=self.header, params=querystring)
  75.    
  76.     def getChatstring(self):
  77.         url=urlbasis+"api/chats"
  78.         response=requests.request("GET", url, headers=self.header)
  79.         parsed_json=json.loads(response.text)
  80.         chatstring=parsed_json[0]['id']
  81.         self.chatstring=chatstring
  82.    
  83.     def quitChat(self):
  84.         self.getChatstring()
  85.         url=urlbasis+"api/chats/quitchat/"+self.chatstring
  86.         requests.put(url, headers=self.header)
  87.    
  88. class Company(ApiCom):
  89.    
  90.     def __init__(self, compName):
  91.         self.compName=compName
  92.    
  93.     def masterUser(self):
  94.         UserName="Malte"
  95.         password="dvQ-bYV-pbW-s6D"
  96.         auth="Basic " + base64.encodestring(UserName+":" + password)
  97.         auth=auth.replace('\n', '')
  98.         header={
  99.             'authorization': auth,
  100.             'x-authorization': "986ffb5a-430e-4cf6-82c3-044f30052548",
  101.             'cache-control': "no-cache",
  102.                 }
  103.         self.masterUser=header
  104.        
  105.     def newCompany(CompName, user):
  106.         url=urlbasis+"api/companies/"
  107.         querystring={"name": CompName, "cashDeposit": "50000"}
  108.         header=user.header
  109.         requests.request("POST", url, headers=header, params=querystring)
  110.  
  111.     def account(self):
  112.         url=urlbasis+"api/search/companies/"+self.compName
  113.         response=requests.request("GET", url, headers=self.masterUser)
  114.         parsed_json=json.loads(response.text)
  115.         self.securitiesAccountID=parsed_json[0]['securitiesAccountId']
  116.         self.user=parsed_json[0]['ceo']['username']
  117.         self.userID=parsed_json[0]['ceo']['id']
  118.         self.compID=parsed_json[0]['id']
  119.        
  120.     def header(self):
  121.         pass      
  122.  
  123.     def order(self, securityIdentifier, action, type2, price, numberOfShares):
  124.         querystring={"owner": self.securitiesAccountID, "securityIdentifier": securityIdentifier, "action": action, "type": type2, "price": price, "numberOfShares":numberOfShares}
  125.         url=urlbasis+"api/securityorders/?"
  126.         response=requests.request("POST", url, headers=self.header, params=querystring)
  127.         print(response)
  128.        
  129.     def newBond(self, numberOfBonds, faceValue, interestRate, maturityDate):
  130.         url=urlbasis+"api/bonds/"
  131.         maturityDate=dateTime(maturityDate)
  132.         querystring={"companyId": self.compID, "numberOfBonds": numberOfBonds, "faceValue": faceValue, "interestRate": interestRate, "maturityDate": maturityDate}
  133.         response=requests.request("POST", url, headers=self.header, params=querystring)  
  134.         if str(response.status_code) == "201":
  135.             print("Bond erstellt")
  136.         else:
  137.             print("Bond nicht erstellt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement