/gdata/finance/service.py

http://radioappz.googlecode.com/ · Python · 243 lines · 190 code · 12 blank · 41 comment · 4 complexity · 7e29d74c6eec402be194ab0b0fc5a99a MD5 · raw file

  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2009 Tan Swee Heng
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Classes to interact with the Google Finance server."""
  17. __author__ = 'thesweeheng@gmail.com'
  18. import gdata.service
  19. import gdata.finance
  20. import atom
  21. class PortfolioQuery(gdata.service.Query):
  22. """A query object for the list of a user's portfolios."""
  23. def returns(self):
  24. return self.get('returns', False)
  25. def set_returns(self, value):
  26. if value is 'true' or value is True:
  27. self['returns'] = 'true'
  28. returns = property(returns, set_returns, doc="The returns query parameter")
  29. def positions(self):
  30. return self.get('positions', False)
  31. def set_positions(self, value):
  32. if value is 'true' or value is True:
  33. self['positions'] = 'true'
  34. positions = property(positions, set_positions,
  35. doc="The positions query parameter")
  36. class PositionQuery(gdata.service.Query):
  37. """A query object for the list of a user's positions in a portfolio."""
  38. def returns(self):
  39. return self.get('returns', False)
  40. def set_returns(self, value):
  41. if value is 'true' or value is True:
  42. self['returns'] = 'true'
  43. returns = property(returns, set_returns,
  44. doc="The returns query parameter")
  45. def transactions(self):
  46. return self.get('transactions', False)
  47. def set_transactions(self, value):
  48. if value is 'true' or value is True:
  49. self['transactions'] = 'true'
  50. transactions = property(transactions, set_transactions,
  51. doc="The transactions query parameter")
  52. class FinanceService(gdata.service.GDataService):
  53. def __init__(self, email=None, password=None, source=None,
  54. server='finance.google.com', **kwargs):
  55. """Creates a client for the Finance service.
  56. Args:
  57. email: string (optional) The user's email address, used for
  58. authentication.
  59. password: string (optional) The user's password.
  60. source: string (optional) The name of the user's application.
  61. server: string (optional) The name of the server to which a connection
  62. will be opened. Default value: 'finance.google.com'.
  63. **kwargs: The other parameters to pass to gdata.service.GDataService
  64. constructor.
  65. """
  66. gdata.service.GDataService.__init__(self,
  67. email=email, password=password, service='finance', server=server,
  68. **kwargs)
  69. def GetPortfolioFeed(self, query=None):
  70. uri = '/finance/feeds/default/portfolios'
  71. if query:
  72. uri = PortfolioQuery(feed=uri, params=query).ToUri()
  73. return self.Get(uri, converter=gdata.finance.PortfolioFeedFromString)
  74. def GetPositionFeed(self, portfolio_entry=None, portfolio_id=None,
  75. query=None):
  76. """
  77. Args:
  78. portfolio_entry: PortfolioEntry (optional; see Notes)
  79. portfolio_id: string (optional; see Notes) This may be obtained
  80. from a PortfolioEntry's portfolio_id attribute.
  81. query: PortfolioQuery (optional)
  82. Notes:
  83. Either a PortfolioEntry OR a portfolio ID must be provided.
  84. """
  85. if portfolio_entry:
  86. uri = portfolio_entry.GetSelfLink().href + '/positions'
  87. elif portfolio_id:
  88. uri = '/finance/feeds/default/portfolios/%s/positions' % portfolio_id
  89. if query:
  90. uri = PositionQuery(feed=uri, params=query).ToUri()
  91. return self.Get(uri, converter=gdata.finance.PositionFeedFromString)
  92. def GetTransactionFeed(self, position_entry=None,
  93. portfolio_id=None, ticker_id=None):
  94. """
  95. Args:
  96. position_entry: PositionEntry (optional; see Notes)
  97. portfolio_id: string (optional; see Notes) This may be obtained
  98. from a PortfolioEntry's portfolio_id attribute.
  99. ticker_id: string (optional; see Notes) This may be obtained from
  100. a PositionEntry's ticker_id attribute. Alternatively it can
  101. be constructed using the security's exchange and symbol,
  102. e.g. 'NASDAQ:GOOG'
  103. Notes:
  104. Either a PositionEntry OR (a portfolio ID AND ticker ID) must
  105. be provided.
  106. """
  107. if position_entry:
  108. uri = position_entry.GetSelfLink().href + '/transactions'
  109. elif portfolio_id and ticker_id:
  110. uri = '/finance/feeds/default/portfolios/%s/positions/%s/transactions' \
  111. % (portfolio_id, ticker_id)
  112. return self.Get(uri, converter=gdata.finance.TransactionFeedFromString)
  113. def GetPortfolio(self, portfolio_id=None, query=None):
  114. uri = '/finance/feeds/default/portfolios/%s' % portfolio_id
  115. if query:
  116. uri = PortfolioQuery(feed=uri, params=query).ToUri()
  117. return self.Get(uri, converter=gdata.finance.PortfolioEntryFromString)
  118. def AddPortfolio(self, portfolio_entry=None):
  119. uri = '/finance/feeds/default/portfolios'
  120. return self.Post(portfolio_entry, uri,
  121. converter=gdata.finance.PortfolioEntryFromString)
  122. def UpdatePortfolio(self, portfolio_entry=None):
  123. uri = portfolio_entry.GetEditLink().href
  124. return self.Put(portfolio_entry, uri,
  125. converter=gdata.finance.PortfolioEntryFromString)
  126. def DeletePortfolio(self, portfolio_entry=None):
  127. uri = portfolio_entry.GetEditLink().href
  128. return self.Delete(uri)
  129. def GetPosition(self, portfolio_id=None, ticker_id=None, query=None):
  130. uri = '/finance/feeds/default/portfolios/%s/positions/%s' \
  131. % (portfolio_id, ticker_id)
  132. if query:
  133. uri = PositionQuery(feed=uri, params=query).ToUri()
  134. return self.Get(uri, converter=gdata.finance.PositionEntryFromString)
  135. def DeletePosition(self, position_entry=None,
  136. portfolio_id=None, ticker_id=None, transaction_feed=None):
  137. """A position is deleted by deleting all its transactions.
  138. Args:
  139. position_entry: PositionEntry (optional; see Notes)
  140. portfolio_id: string (optional; see Notes) This may be obtained
  141. from a PortfolioEntry's portfolio_id attribute.
  142. ticker_id: string (optional; see Notes) This may be obtained from
  143. a PositionEntry's ticker_id attribute. Alternatively it can
  144. be constructed using the security's exchange and symbol,
  145. e.g. 'NASDAQ:GOOG'
  146. transaction_feed: TransactionFeed (optional; see Notes)
  147. Notes:
  148. Either a PositionEntry OR (a portfolio ID AND ticker ID) OR
  149. a TransactionFeed must be provided.
  150. """
  151. if transaction_feed:
  152. feed = transaction_feed
  153. else:
  154. if position_entry:
  155. feed = self.GetTransactionFeed(position_entry=position_entry)
  156. elif portfolio_id and ticker_id:
  157. feed = self.GetTransactionFeed(
  158. portfolio_id=portfolio_id, ticker_id=ticker_id)
  159. for txn in feed.entry:
  160. self.DeleteTransaction(txn)
  161. return True
  162. def GetTransaction(self, portfolio_id=None, ticker_id=None,
  163. transaction_id=None):
  164. uri = '/finance/feeds/default/portfolios/%s/positions/%s/transactions/%s' \
  165. % (portfolio_id, ticker_id, transaction_id)
  166. return self.Get(uri, converter=gdata.finance.TransactionEntryFromString)
  167. def AddTransaction(self, transaction_entry=None, transaction_feed = None,
  168. position_entry=None, portfolio_id=None, ticker_id=None):
  169. """
  170. Args:
  171. transaction_entry: TransactionEntry (required)
  172. transaction_feed: TransactionFeed (optional; see Notes)
  173. position_entry: PositionEntry (optional; see Notes)
  174. portfolio_id: string (optional; see Notes) This may be obtained
  175. from a PortfolioEntry's portfolio_id attribute.
  176. ticker_id: string (optional; see Notes) This may be obtained from
  177. a PositionEntry's ticker_id attribute. Alternatively it can
  178. be constructed using the security's exchange and symbol,
  179. e.g. 'NASDAQ:GOOG'
  180. Notes:
  181. Either a TransactionFeed OR a PositionEntry OR (a portfolio ID AND
  182. ticker ID) must be provided.
  183. """
  184. if transaction_feed:
  185. uri = transaction_feed.GetPostLink().href
  186. elif position_entry:
  187. uri = position_entry.GetSelfLink().href + '/transactions'
  188. elif portfolio_id and ticker_id:
  189. uri = '/finance/feeds/default/portfolios/%s/positions/%s/transactions' \
  190. % (portfolio_id, ticker_id)
  191. return self.Post(transaction_entry, uri,
  192. converter=gdata.finance.TransactionEntryFromString)
  193. def UpdateTransaction(self, transaction_entry=None):
  194. uri = transaction_entry.GetEditLink().href
  195. return self.Put(transaction_entry, uri,
  196. converter=gdata.finance.TransactionEntryFromString)
  197. def DeleteTransaction(self, transaction_entry=None):
  198. uri = transaction_entry.GetEditLink().href
  199. return self.Delete(uri)