Created
May 10, 2012 12:20
-
-
Save AyumuKasuga/2652730 to your computer and use it in GitHub Desktop.
miralinks.ru finance parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
from grab import Grab | |
from BeautifulSoup import BeautifulSoup | |
import re | |
import datetime as dt | |
class miralinksapi(): | |
def __init__(self): | |
access = {'username': 'login', | |
'password': 'pwd'} | |
self.g = Grab() | |
#предварительно сходим на главную | |
self.g.go('http://www.miralinks.ru/') | |
#задаем параметры post запроса | |
self.g.setup(post={'_method': 'POST', | |
'data[User][login]': access['username'], | |
'data[User][password]': access['password'], | |
'data[User][remember]': '0'}) | |
#идем логиниться | |
self.g.go('http://www.miralinks.ru/users/login') | |
#здесь хорошо бы проверить успешность аутентификации | |
def get_project_money(self, idcampaign, fromdate, todate): | |
''' | |
получаем сумму потраченных денег по проекту idcampaign | |
за период fromdate, todate | |
''' | |
#урл в котором и лежит все что нам нужно | |
url = 'http://www.miralinks.ru/articles/index/payed/project:%s/cmview:articles' % (idcampaign) | |
self.g.go(url) | |
#здесь тоже нужно проверить успешность запроса | |
#print self.g.response.body | |
#создаем объект soup | |
soup = BeautifulSoup(self.g.response.body) | |
#итерируем строчки таблички | |
articles = {} | |
for i in soup.html.body('tr', {'id': re.compile('actions\d')}): | |
srcdate = i.find('span', {'class': 'graytext help'}).string | |
tdate = srcdate.split(' ')[0].split('.') | |
tdate.reverse() | |
#вот она дата! | |
dat = dt.date(*map(lambda x: int(x), tdate)) | |
#id этой статьи | |
artid = i.attrMap['artid'] | |
#а вот так (немного корявенько) находим стоимость этой статьи | |
money = float(soup.html.body.first('tr', {'id': 'suppl' + artid}).findAll('td')[1].find('strong').string) | |
try: | |
articles[dat] += money | |
except KeyError: | |
articles[dat] = money | |
#self.articles = articles | |
# и теперь выбираем только нужный диапазон и суммируем все | |
ressum = float() | |
for d, m in articles.items(): | |
if d >= fromdate and d < todate: | |
ressum += m | |
return ressum | |
def __del__(self): | |
#выходим | |
self.g.go('http://www.miralinks.ru/users/logout') | |
if __name__ == '__main__': | |
m = miralinksapi() | |
print m.get_project_money(1, dt.date(2012, 4, 27), dt.date(2012, 4, 30)) | |
print m.get_project_money(2, dt.date(2012, 4, 1), dt.date(2012, 4, 27)) | |
print m.get_project_money(3, dt.date(2012, 4, 1), dt.date(2012, 4, 30)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment