Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
oczkers committed Jan 10, 2017
2 parents cca9d12 + aa0000d commit 99c37d3
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Patches and Suggestions
- rjansen
- ricklhp7
- hunterjm
- fifa2017player
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changelog
---------


0.2.6 (2017-01-10)
++++++++++++++++++
* add (minimum request) delay param (#233)
* add fast param to bid method
* use Unauthorized expcetion (fix #232)

0.2.5 (2016-12-28)
++++++++++++++++++
* add timeout (#226)
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Optional parameters:
Bid
```

Optional parameters:

- FAST: [boolean] True for skipping trade status & credits check.

.. code-block:: python
>>> fut.bid(items[0]['trade_id'], 600)
Expand Down
2 changes: 1 addition & 1 deletion fut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""

__title__ = 'fut'
__version__ = '0.2.5'
__version__ = '0.2.6'
__author__ = 'Piotr Staroszczyk'
__author_email__ = 'piotr.staroszczyk@get24.org'
__license__ = 'GNU GPL v3'
Expand Down
3 changes: 2 additions & 1 deletion fut/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# chrome 53 @ win10
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
Expand Down Expand Up @@ -40,3 +40,4 @@

cookies_file = 'cookies.txt'
timeout = 15 # defaulf global timeout
delay = (0, 5) # default mininum delay between requests (random range)
34 changes: 23 additions & 11 deletions fut/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import requests
import re
from time import time
from time import time, sleep
try:
from cookielib import LWPCookieJar
except ImportError:
Expand All @@ -20,13 +20,14 @@
except ImportError:
import json

from .config import headers, headers_and, headers_ios, flash_agent, cookies_file, timeout
from .config import headers, headers_and, headers_ios, flash_agent, cookies_file, timeout, delay
from .log import logger
from .urls import urls
from .exceptions import (FutError, ExpiredSession, InternalServerError,
UnknownError, PermissionDenied, Captcha,
Conflict, MaxSessions, MultipleSession,
FeatureDisabled, doLoginFail, NoUltimateTeam)
Unauthorized, FeatureDisabled, doLoginFail,
NoUltimateTeam)
from .EAHashingAlgorithm import EAHashingAlgorithm


Expand Down Expand Up @@ -155,10 +156,12 @@ def teams(year=2017, timeout=timeout):


class Core(object):
def __init__(self, email, passwd, secret_answer, platform='pc', code=None, emulate=None, debug=False, cookies=cookies_file, timeout=timeout):
def __init__(self, email, passwd, secret_answer, platform='pc', code=None, emulate=None, debug=False, cookies=cookies_file, timeout=timeout, delay=delay):
self.credits = 0
self.cookies_file = cookies # TODO: map self.cookies to requests.Session.cookies?
self.timeout = timeout
self.delay = delay
self.request_time = 0
if debug: # save full log to file (fut.log)
self.logger = logger(save=True)
else: # NullHandler
Expand Down Expand Up @@ -307,13 +310,15 @@ def __login__(self, email, passwd, secret_answer, platform='pc', code=None, emul
personas = rc.json()['userAccountInfo']['personas']
for p in personas:
# self.clubs = [i for i in p['userClubList']]
# sort clubs by lastAccessTime (latest first)
# sort clubs by lastAccessTime (latest first but looks like ea is doing this for us(?))
# self.clubs.sort(key=lambda i: i['lastAccessTime'], reverse=True)
for c in p['userClubList']:
if c['skuAccessList'] and game_sku in c['skuAccessList']:
self.persona_id = p['personaId']
self.persona_name = p['personaName']
break
if not hasattr(self, 'persona_id') or not hasattr(self, 'persona_name'):
raise FutError(reason='Error during login process (no persona found).')

# authorization
self.r.headers.update({ # prepare headers
Expand Down Expand Up @@ -408,6 +413,8 @@ def __request__(self, method, url, *args, **kwargs):
# TODO: update credtis?
self.r.headers['X-HTTP-Method-Override'] = method.upper()
self.logger.debug("request: {0} args={1}; kwargs={2}".format(url, args, kwargs))
sleep(max(self.request_time - time() + random.randrange(self.delay[0], self.delay[1]+1), 0)) # respect minimum delay
self.request_time = time() # save request time for delay calculations
rc = self.r.post(url, timeout=self.timeout, *args, **kwargs)
self.logger.debug("response: {0}".format(rc.content))
if not rc.ok: # status != 200
Expand Down Expand Up @@ -436,6 +443,8 @@ def __request__(self, method, url, *args, **kwargs):
# img = self.r.get(self.urls['fut_captcha_img'], params={'_': int(time()*1000), 'token': captcha_token}, timeout=self.timeout).content # doesnt work - check headers
img = None
raise Captcha(err_code, err_reason, err_string, captcha_token, img)
elif err_code == '401' or err_string == 'Unauthorized':
raise Unauthorized(err_code, err_reason, err_string)
elif err_code == '409' or err_string == 'Conflict':
raise Conflict(err_code, err_reason, err_string)
else:
Expand Down Expand Up @@ -618,17 +627,20 @@ def searchAuctions(self, ctype, level=None, category=None, assetId=None, defId=N
rc = self.__get__(self.urls['fut']['SearchAuctions'], params=params)
return [itemParse(i) for i in rc['auctionInfo']]

def bid(self, trade_id, bid):
def bid(self, trade_id, bid, fast=False):
"""Make a bid.
:params trade_id: Trade id.
:params bid: Amount of credits You want to spend.
:params fast: True for fastest bidding (skips trade status & credits check).
"""
rc = self.tradeStatus(trade_id)[0]
if rc['currentBid'] < bid and self.credits >= bid:
data = {'bid': bid}
url = '{0}/{1}/bid'.format(self.urls['fut']['PostBid'], trade_id)
rc = self.__put__(url, data=json.dumps(data))['auctionInfo'][0]
if not fast:
rc = self.tradeStatus(trade_id)[0]
if rc['currentBid'] > bid or self.credits < bid:
return False # TODO: add exceptions
data = {'bid': bid}
url = '{0}/{1}/bid'.format(self.urls['fut']['PostBid'], trade_id)
rc = self.__put__(url, data=json.dumps(data))['auctionInfo'][0]
if rc['bidState'] == 'highest' or (rc['tradeState'] == 'closed' and rc['bidState'] == 'buyNow'): # checking 'tradeState' is required?
return True
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


__title__ = 'fut'
__version__ = '0.2.5'
__version__ = '0.2.6'
__author__ = 'Piotr Staroszczyk'
__author_email__ = 'piotr.staroszczyk@get24.org'
__license__ = 'GNU GPL v3'
Expand Down

0 comments on commit 99c37d3

Please sign in to comment.