yahoo-finance étant HS, récup des cours par scrapping
This commit is contained in:
@@ -19,7 +19,6 @@ from pyramid_mailer import get_mailer
|
||||
from pyramid_mailer.message import Message, Attachment
|
||||
from datetime import *
|
||||
import hashlib
|
||||
from yahoo_finance import Share, Currency
|
||||
|
||||
from sqlalchemy.exc import DBAPIError
|
||||
from ..security import groupfinder
|
||||
@@ -37,7 +36,39 @@ from ..views.default import (
|
||||
|
||||
import json
|
||||
import time
|
||||
import urllib2
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
def getYahooQuote(ticker):
|
||||
# specify the url
|
||||
quote_page = 'https://finance.yahoo.com/quote/%s/history/' % ticker
|
||||
|
||||
# query & parse the html using beautiful soap and store in variable `soup`
|
||||
soup = BeautifulSoup(urllib2.urlopen(quote_page), 'html.parser')
|
||||
data = []
|
||||
# get the quote price
|
||||
rows = soup.findAll('table')[0].tbody.findAll('tr')
|
||||
for each_row in rows:
|
||||
divs = each_row.findAll('td')
|
||||
if divs[1].span.text != 'Dividend': #Ignore this row in the table
|
||||
#I'm only interested in 'Close' price;
|
||||
data.append({'Date': divs[0].span.text, 'Close': float(divs[5].span.text.replace(',',''))})
|
||||
# retourne la prière ligne
|
||||
return data[0]
|
||||
|
||||
def getCurrencyRate(currency):
|
||||
# specify the url
|
||||
quote_page = 'http://www.finances.net/devises/courseuro'
|
||||
|
||||
# query & parse the html using beautiful soap and store in variable `soup`
|
||||
soup = BeautifulSoup(urllib2.urlopen(quote_page), 'html.parser')
|
||||
# get the history table
|
||||
rows = soup.find('table', attrs={'class': 'news_table'}).tbody.findAll('tr')
|
||||
divs = rows[1].findAll('td')
|
||||
rate = divs[1].span.text
|
||||
return float(rate.replace(',','.'))
|
||||
|
||||
|
||||
@view_config(route_name='actifs_list', renderer='../templates/actifs/actifs_list.pt', permission='view')
|
||||
def actifs_list(request):
|
||||
url = request.route_url('actifs_list')
|
||||
@@ -49,17 +80,17 @@ def actifs_list(request):
|
||||
items = get_actifs(request, '0')
|
||||
|
||||
# MAJ du prtefeuille
|
||||
if 'form.submitted' in request.params:
|
||||
if 'form.submitted' in request.params:
|
||||
# maj des parités des devises d'après Yahoo finance
|
||||
usdeur = Currency('USDEUR')
|
||||
update_actif_devise(request, 'USD', usdeur.get_rate())
|
||||
update_actif_devise(request, 'USD', getCurrencyRate('USD'))
|
||||
|
||||
for item in items:
|
||||
if item.type == 'ACTION':
|
||||
sym = Share(item.symbole)
|
||||
# import pdb;pdb.set_trace()
|
||||
update_actif_valeur(request, item.symbole, sym.get_price())
|
||||
time.sleep(2) # attendre 2 secondes
|
||||
# get yahoo price
|
||||
quote_price = getYahooQuote(item.symbole)
|
||||
if quote_price:
|
||||
update_actif_valeur(request, item.symbole, quote_price['Close'])
|
||||
time.sleep(2) # attendre 2 secondes
|
||||
|
||||
# update du portefeuille
|
||||
update_portefeuille(request, logged_in)
|
||||
@@ -123,13 +154,10 @@ def actif_edit(request):
|
||||
symbole = request.params['symbole']
|
||||
else:
|
||||
symbole = actif.symbole
|
||||
sym = Share(symbole)
|
||||
|
||||
if sym.get_name() <> None:
|
||||
# import pdb;pdb.set_trace()
|
||||
new_values['libelle'] = sym.get_name()
|
||||
new_values['cours'] = sym.get_price()
|
||||
new_values['devise'] = sym.get_currency()
|
||||
|
||||
quote_price = getYahooQuote(symbole)
|
||||
if quote_price:
|
||||
new_values['cours'] = quote_price['Close']
|
||||
|
||||
update_actif(request, no_id, new_values)
|
||||
request.session.flash(u"La fiche a été mise à jour avec succès.", 'success')
|
||||
|
||||
@@ -20,7 +20,6 @@ from pyramid_mailer.message import Message, Attachment
|
||||
from datetime import *
|
||||
import hashlib
|
||||
from docutils.core import publish_parts
|
||||
from yahoo_finance import Share, Currency
|
||||
|
||||
from sqlalchemy.exc import DBAPIError
|
||||
from ..security import groupfinder
|
||||
|
||||
Reference in New Issue
Block a user