Permalink
Browse files

First step to converting to evepaste. Lots of refactoring for the bet…

…ter.
  • Loading branch information...
1 parent acce3d3 commit cf43399d9801eabba99a54d2009bb6b467710a40 @sudorandom sudorandom committed Jan 2, 2014
View
Oops, something went wrong.
@@ -23,7 +23,6 @@
'30002510': 'Rens',
'30002053': 'Hek',
}
-app.config['TYPES'] = json.loads(open('data/types.json').read())
app.config['USER_AGENT'] = 'Evepraisal/1.0 +http://evepraisal.com/'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../data/scans.db'
app.config['CACHE_TYPE'] = 'memcached'
@@ -51,7 +50,7 @@
def ignore_errors(f, *args, **kwargs):
try:
f(*args, **kwargs)
- except:
+ except Exception:
pass
@@ -62,11 +61,6 @@ def before_first_request():
except Exception as e:
app.logger.error(str(e))
- try:
- db.engine.execute('ALTER TABLE "Scans" ADD COLUMN "UserId" integer')
- except Exception, e:
- app.logger.error(str(e))
-
@app.before_request
def before_request():
View
@@ -3,7 +3,7 @@
import xml.etree.ElementTree as ET
from . import app, cache
-from models import EveType
+from models import get_type_by_id
def memcache_type_key(typeId, options=None):
@@ -16,10 +16,10 @@ def get_cached_values(eve_types, options=None):
"Get Cached values given the eve_types"
found = {}
for eve_type in eve_types:
- key = memcache_type_key(eve_type.type_id, options=options)
+ key = memcache_type_key(eve_type, options=options)
obj = cache.get(key)
if obj:
- found[eve_type.type_id] = obj
+ found[eve_type] = obj
return found
@@ -61,7 +61,7 @@ def get_market_values(eve_types, options=None):
solarsystem_id = options.get('solarsystem_id', -1)
for types in [eve_types[i:i + 100] for i in range(0, len(eve_types), 100)]:
query = []
- query += ['typeid=%s' % str(x.type_id) for x in types]
+ query += ['typeid=%s' % str(type_id) for type_id in types]
all_price_metric = 'percentile'
if solarsystem_id == '-1':
buy_price_metric = 'percentile'
@@ -143,7 +143,8 @@ def get_market_values_2(eve_types, options=None):
market_prices = {}
solarsystem_id = options.get('solarsystem_id', '-1')
for types in [eve_types[i:i + 200] for i in range(0, len(eve_types), 200)]:
- typeIds_str = 'type_ids=%s' % ','.join(str(x.type_id) for x in types)
+ typeIds_str = 'type_ids=%s' % ','.join(str(type_id)
+ for type_id in types)
query = ['typeid=%s' % typeIds_str]
if solarsystem_id != '-1':
@@ -168,27 +169,21 @@ def get_market_values_2(eve_types, options=None):
market_prices[k] = {}
if row['buysell'] == 's':
price = float(row['price'])
- market_prices[k]['sell'] = {
- 'avg': price,
- 'min': price,
- 'max': price,
- }
+ market_prices[k]['sell'] = {'avg': price,
+ 'min': price,
+ 'max': price}
elif row['buysell'] == 'b':
price = float(row['price'])
- market_prices[k]['buy'] = {
- 'avg': price,
- 'min': price,
- 'max': price,
- }
+ market_prices[k]['buy'] = {'avg': price,
+ 'min': price,
+ 'max': price}
for typeId, prices in market_prices.iteritems():
avg = (prices['sell']['avg'] + prices['buy']['avg']) / 2
- market_prices[typeId]['all'] = {
- 'avg': avg,
- 'min': avg,
- 'max': avg,
- 'price': avg,
- }
+ market_prices[typeId]['all'] = {'avg': avg,
+ 'min': avg,
+ 'max': avg,
+ 'price': avg}
market_prices[typeId]['buy']['price'] = \
market_prices[typeId]['buy']['max']
market_prices[typeId]['sell']['price'] = \
@@ -206,78 +201,71 @@ def get_market_values_2(eve_types, options=None):
def get_invalid_values(eve_types, options=None):
invalid_items = {}
for eve_type in eve_types:
- if eve_type.props.get('market') is False:
+ type_details = get_type_by_id(eve_type)
+ if type_details and type_details.get('market') is False:
zeroed_price = {'avg': 0, 'min': 0, 'max': 0, 'price': 0}
price_info = {
'buy': zeroed_price.copy(),
'sell': zeroed_price.copy(),
'all': zeroed_price.copy(),
}
- invalid_items[eve_type.type_id] = price_info
+ invalid_items[eve_type] = price_info
return invalid_items
def get_componentized_values(eve_types, options=None):
componentized_items = {}
for eve_type in eve_types:
- if 'components' in eve_type.props:
- component_types = [
- EveType(c['materialTypeID'], count=c['quantity'])
- for c in eve_type.props['components']]
+ type_details = get_type_by_id(eve_type)
+ if type_details and 'components' in type_details:
+ component_types = dict((c['materialTypeID'], c['quantity'])
+ for c in type_details['components'])
- populate_market_values(
- component_types,
- methods=[
- get_cached_values, get_market_values, get_market_values_2],
+ component_prices = get_market_prices(
+ component_types.keys(),
+ methods=[get_cached_values,
+ get_market_values,
+ get_market_values_2],
options=options)
+ price_map = dict(component_prices)
zeroed_price = {'avg': 0, 'min': 0, 'max': 0, 'price': 0}
complete_price_data = {
'buy': zeroed_price.copy(),
'sell': zeroed_price.copy(),
'all': zeroed_price.copy(),
}
- for component in component_types:
+ for component, quantity in component_types.items():
for market_type in ['buy', 'sell', 'all']:
for stat in ['avg', 'min', 'max', 'price']:
- complete_price_data[market_type][stat] += \
- component.pricing_info[market_type][stat] * \
- component.count
- componentized_items[eve_type.type_id] = complete_price_data
+ _price = price_map.get(component)
+ if _price:
+ complete_price_data[market_type][stat] += (
+ _price[market_type][stat] * quantity)
+ componentized_items[eve_type] = complete_price_data
# Cache for up to 10 hours
cache.set(
- memcache_type_key(eve_type.type_id, options=options),
+ memcache_type_key(eve_type, options=options),
complete_price_data,
timeout=10 * 60 * 60)
return componentized_items
-def populate_market_values(eve_types, methods=None, options=None):
- unpopulated_types = list(eve_types)
- if methods is None:
- methods = [
- get_invalid_values, get_cached_values, get_componentized_values,
- get_market_values, get_market_values_2]
- for pricing_method in methods:
- if len(unpopulated_types) == 0:
+def get_market_prices(modules, methods=None, options=None):
+ unpriced_modules = modules[:]
+ prices = {}
+ for pricing_method in methods or [get_invalid_values,
+ get_cached_values,
+ get_componentized_values,
+ get_market_values,
+ get_market_values_2]:
+ if len(modules) == len(prices):
break
- # returns a dict with {type_id: pricing_info}
- prices = pricing_method(unpopulated_types, options=options)
- app.logger.debug(
- "Found %s/%s items using method: %s", len(prices),
- len(unpopulated_types), pricing_method)
- new_unpopulated_types = []
- for eve_type in unpopulated_types:
- if eve_type.type_id in prices:
- pdata = prices[eve_type.type_id]
- pdata['totals'] = {
- 'volume': eve_type.props.get('volume', 0) * eve_type.count
- }
- for total_key in ['sell', 'buy', 'all']:
- _total = pdata[total_key]['price'] * eve_type.count
- pdata['totals'][total_key] = _total
- eve_type.pricing_info = pdata
- else:
- new_unpopulated_types.append(eve_type)
- unpopulated_types = new_unpopulated_types
- return eve_types
+ # each pricing_method returns a dict with {type_id: pricing_info}
+ _prices = pricing_method(unpriced_modules, options=options)
+ app.logger.debug("Found %s/%s items using method: %s",
+ len(_prices), len(modules), pricing_method)
+ for type_id, pricing_info in _prices.items():
+ prices[type_id] = pricing_info
+ unpriced_modules.remove(type_id)
+ return prices.items()
View
@@ -2,27 +2,50 @@
import math
from datetime import timedelta
from babel.dates import format_timedelta
+from flask import request
from . import app
+from models import get_type_by_name
+
+
+def get_market_name(market_id):
+ return app.config['VALID_SOLAR_SYSTEMS'].get(market_id, 'UNKNOWN'),
+
+
+@app.context_processor
+def utility_processor():
+ def is_from_igb():
+ return 'EVE-IGB' in request.headers.get('User-Agent', '')
+ return dict(is_from_igb=is_from_igb,
+ get_market_name=get_market_name,
+ get_type_by_name=get_type_by_name)
# Adopted from http://stackoverflow.com/a/3155023/74375
-def millify(n, format="{:,.2f}"):
+def millify(n, fmt="{:,.2f}"):
millnames = ['', 'Thousand', 'Million', 'Billion', 'Trillion',
'Quadrillion', 'Quintillion', 'Sextillion', 'Septillion',
'Octillion', 'Nonillion', 'Decillion']
millidx = max(0, min(len(millnames) - 1,
int(math.floor(math.log10(abs(n)) / 3.0))))
num = n / 10 ** (3 * millidx)
- return (format + ' {}').format(num,
- millnames[millidx])
+ return (fmt + ' {}').format(num,
+ millnames[millidx])
+
+
+@app.template_filter('comma_separated_int')
+def comma_separated_int(value):
+ try:
+ return "{:,}".format(value)
+ except Exception:
+ return ""
@app.template_filter('format_isk')
def format_isk(value):
try:
return "{:,.2f}".format(value)
- except:
+ except Exception:
return ""
@@ -46,7 +69,7 @@ def format_volume(value):
if value < 1:
return "%.2fm<sup>3</sup>" % value
return "{:,.2f}m<sup>3</sup>".format(value)
- except:
+ except Exception:
return "unknown m<sup>3</sup>"
@@ -62,7 +85,7 @@ def relative_time(past):
try:
delta = timedelta(seconds=delta_seconds)
return format_timedelta(delta, locale='en_US') + postfix
- except:
+ except Exception:
return ''
View
@@ -1,12 +1,7 @@
-import time
-import json
from functools import wraps
from flask import g, redirect, url_for, request, flash
-from models import Scans
-from . import db, cache
-
def login_required(func):
@wraps(func)
@@ -16,54 +11,3 @@ def decorated_function(*args, **kwargs):
return redirect(url_for('login', next=request.url))
return func(*args, **kwargs)
return decorated_function
-
-
-def save_result(result, public=True):
- data = json.dumps(result, indent=2)
- user_id = None
- if g.user:
- user_id = g.user.Id
- scan = Scans(
- Data=data,
- Created=int(time.time()),
- BuyValue=result['totals']['buy'],
- SellValue=result['totals']['sell'],
- Public=public,
- UserId=user_id)
- db.session.add(scan)
- db.session.commit()
- return scan.Id
-
-
-def load_result(result_id):
- try:
- result_id = int(result_id)
- except:
- return
-
- data = cache.get("results:%s" % result_id)
- if data:
- return data
-
- q = Scans.query.filter(Scans.Id == result_id)
- if g.user:
- q = q.filter(
- (Scans.UserId == g.user.Id) | (Scans.Public == True)) # noqa
- else:
- q = q.filter(Scans.Public == True) # noqa
-
- row = q.first()
-
- if row:
- data = json.loads(row.Data)
- if 'raw_scan' in data:
- data['raw_paste'] = data['raw_scan']
- del data['raw_scan']
-
- if row.Public is True:
- cache.set("results:%s" % result_id, data, timeout=600)
- return data
-
-
-def is_from_igb():
- return request.headers.get('User-Agent', '').find("EVE-IGB") != -1
Oops, something went wrong.

0 comments on commit cf43399

Please sign in to comment.