Esempio n. 1
0
def add_comment_to_company(company_id, data):
    #user = UserTokenRepo().get_auth_user(request.headers.get('Authorization'))
    user = auth.user()
    company = CompanyRepo().get_company(company_id)

    msg, attachment_obj, upload_data = data.get('comment', None), data.get(
        'attachment', None), None
    if attachment_obj is not None and attachment_obj.filename != '':
        data = uploader.upload(attachment_obj)
        data['uploaderId'] = data['id']
        del data['id']
        upload_data = UploadRepo().create(data)
    elif msg is None or msg == '':
        raise SException("Please provide some comment",
                         redirect_url='/companies/' + company.code + '/' +
                         company.slug)

    comment = CommentRepo().create({
        'user': user,
        'company': company,
        'message': msg,
        'type': data.get('type'),
        'attachment': upload_data
    })
    return company
    return transformers.transform_comment(comment)
Esempio n. 2
0
class Command(BaseCommand):
    def __init__(self):
        self.company_details_repo = CompanyDetailsRepo()
        self.company_repo = CompanyRepo()
        self.nse = Nse()
        self.stockapi = NSE()

    def run(self):
        nse_list = self.nse.get_stock_codes()

        for code, title in nse_list.iteritems():
            company = self.company_repo.filter(stockExchangeCode='NSE',
                                               code=code).first()
            if company:
                if company.weight < 1:
                    print "saving: ", code, title
                    company.weight += 1
                    company.save()
            else:
                print "creating: ", code, title
                try:
                    details = self.company_details_repo.create(**{})
                    c = self.company_repo.create(details=details,
                                                 **self._extract_company_data(
                                                     title, code))
                    c.details = details
                    c.save()
                    c = self.stockapi.latest(c)
                except:
                    print "error for " + title

    def _extract_company_data(self, name, code):
        return {
            "name": name,
            "code": code,
            #"quandlCode": data['dataset_code'],
            "description": name,
            "stockExchangeCode": 'NSE',
            "type": 'Time Series',
            "frequency": 'daily',
            "refreshedAt": helper.now(),
            #"oldestAvailableDate": helper.now(),
            "newAvailableDate": helper.now(),
            "history": [],
            "historyCount": 0,
            "weight": 1
        }
Esempio n. 3
0
def reply_to_comment(company_id, comment_id, message, reply_to_id=None):
    user = auth.user()
    company = CompanyRepo().get_company(company_id)
    comment = CommentRepo().get(id=comment_id)
    reply = ReplyCommentRepo().create({"user": user, "message": message})
    comment.replies.append(reply)
    comment.save()
    return company
Esempio n. 4
0
def reply_to_comment_old(company_id, comment_id, message, reply_to_id=None):
    user = UserTokenRepo().get_auth_user(request.headers.get('Authorization'))
    company = CompanyRepo().get_company(company_id)
    comment = CommentRepo().get(id=comment_id)
    reply = ReplyCommentRepo().create({"user": user, "message": message})
    comment.replies.append(reply)
    comment.save()
    return transformers.transform_reply(reply)
Esempio n. 5
0
def list_of_watchlist(per_page=5, page=1):
    user = UserRepo().objects.no_dereference().filter(
        id=auth.user().id).first()
    company_ids = [u.id for u in user.favourites]
    #return CompanyRepo().set_transformer(transformers.company)\
    return CompanyRepo().skip_list('slice__history', 0, 1)\
            .set_excludes([])\
            .filter_self(id__in=company_ids)\
            .paginate_self(per_page=per_page, page=page)
Esempio n. 6
0
def filter(data):
    if data is None:
        return []
        raise SException("You have to provide something.", 400)
    result = CompanyRepo().set_transformer(transformers.company)\
            .filter_self(Q(name__icontains=data) | Q(code__icontains=data))\
            .paginate(10)
    #.filter_self(Q(name__icontains=data) | Q(code__icontains=data)).paginate()
    return result.items
Esempio n. 7
0
class Command(BaseCommand):
    def __init__(self):
        self.company_repo = CompanyRepo()

    def run(self):
        companies = self.company_repo.all()
        for company in companies:
            #try:
            print "Updating stock for : " + company.code
            c = get_current_stock_of_company(company.code, company=company)
Esempio n. 8
0
def get_current_stock_of_company(company_code, company=None):
    if company is None:
        company = CompanyRepo().get(code=company_code)
        if not company:
            raise SException("Error in company code", 400)
    if company.stockExchangeCode == 'NSE':
        company = nse.latest(company)
    else:
        company = bse.latest(company)

    return company
Esempio n. 9
0
class SlugMaker(BaseCommand):
    def __init__(self):
        self.company_repo = CompanyRepo()

    def run(self):
        from slugify import slugify
        companies = self.company_repo.set_excludes(['history']).all()
        for company in companies:
            company.slug = slugify(company.name)
            company.save()
            print company.name, company.slug
        print "Done"
Esempio n. 10
0
class Command(BaseCommand):
    def __init__(self):
        self.company_repo = CompanyRepo()
        self.stock_repo = StockRepo()
        self.company_details_repo = CompanyDetailsRepo()

    def run(self):
        paginator = self.company_repo.set_excludes(
            []).filter_self(stockExchangeCode='NSE').paginate(1)
        company = paginator.items[0]
        print "running for " + company.name
        #start_new_thread(self.add_history_to_stock, (company,))
        self.add_history_to_stock(company)
        print "-" * 40

        while paginator.has_next:
            paginator = paginator.next()
            company = paginator.items[0]
            print "running for " + company.name
            #start_new_thread(self.add_history_to_stock, (company,))
            self.add_history_to_stock(company)
            print "-" * 40
            #time.sleep(1)

        #time.sleep(100000)
        print "Done"

    def add_history_to_stock(self, company):
        counter = len(company.history)
        if company.details is None:
            print "creating details for " + company.name
            company.details = self.company_details_repo.create(**{})
            company.details.save()
        for stock in company.history:
            try:
                stock['company_id'] = company.id
                try:
                    s = self.stock_repo.create(**stock)
                except:
                    continue
                company.stocks.append(s)
                company.save()
                counter -= 1
            except Exception as e:
                print e, company.name
        #if len(company.history) == len(company.stocks):
        print "finished : " + company.name
        if counter == 0:
            company.history = []
            company.save()
        else:
            print "Not deleting company history for - " + company.name
Esempio n. 11
0
class Command(BaseCommand):
    def __init__(self):
        self.trending_company_repo = TrendingCompanyRepo()
        self.company_repo = CompanyRepo()
        self.nse = Nse()

    def run(self):
        gainers = self.nse.get_top_gainers()[:5]
        losers = self.nse.get_top_losers()[:5]
        print gainers, losers
        return
        g_comapnies = [
            self.company_repo.get_or_create(code=g['symbol']) for g in gainers
        ]
        l_comapnies = [
            self.company_repo.get_or_create(code=l['symbol']) for l in losers
        ]
        trendings = g_comapnies.extends(l_comapnies)
        date = helper.today()
        try:
            self.trending_company_repo.create(date=date, trendings=trendings)
        except Exception as e:
            print "error", e
        print "Done"
Esempio n. 12
0
def get_only_company(company_code):
    #comp = CompanyRepo().set_excludes(['history']).filter(code=company_code).first()
    comp = CompanyRepo().get(code=company_code)
    if not comp:
        raise SException("Invalid company code", 400)
    return comp
Esempio n. 13
0
 def __init__(self):
     self.trending_company_repo = TrendingCompanyRepo()
     self.company_repo = CompanyRepo()
     self.nse = Nse()
Esempio n. 14
0
 def __init__(self):
     self.company_repo = CompanyRepo()
     self.stock_repo = StockRepo()
     self.count = 0
     self.lock = allocate_lock()
     self.sleep_time = 1  # in seconds
Esempio n. 15
0
def unlike_a_comment(company_id, comment_id):
    user = auth.user()
    company = CompanyRepo().get_company(company_id)
    comment = CommentRepo().get(id=comment_id)
    CommentRepo().unlike(comment, user)
    return company
Esempio n. 16
0
def remove_from_watchlist(company_id):
    company = CompanyRepo().get_company(company_id)
    user = UserRepo().objects.no_dereference()\
            .filter(id=auth.user().id)\
            .update(pull__favourites=company)
    return "Removed successfully"
Esempio n. 17
0
def add_to_watchlist(company_id):
    company = CompanyRepo().get_company(company_id)
    user = UserRepo().objects.no_dereference()\
            .filter(id=auth.user().id)\
            .update(add_to_set__favourites=company)
    return "Added successfully"
Esempio n. 18
0
def get_company_details(company_id):
    return CompanyRepo().filter(id=company_id).first()
Esempio n. 19
0
 def __init__(self):
     self.company_details_repo = CompanyDetailsRepo()
     self.company_repo = CompanyRepo()
     self.nse = Nse()
     self.stockapi = NSE()
Esempio n. 20
0
def list_comments_of_company(company_id):
    company = CompanyRepo().get_company(company_id)
    return CommentRepo().set_transformer(transformers.transform_comment)\
            .filter_self(company=company)\
            .set_order_by('-createdAt')\
            .paginate()
Esempio n. 21
0
#import quandl
from flask import request, session
from mongoengine.queryset.visitor import Q
from nsetools import Nse

from app.mod_repository.stocktwist import CompanyRepo, UserRepo,\
                                        UserTokenRepo, CommentRepo, \
                                        ReplyCommentRepo, UploadRepo, \
                                        TrendingCompanyRepo
import transformers
from app.mod_library.exception import SException
from app.mod_library import uploader, auth, stockapis

nse = stockapis.NSE()
bse = stockapis.BSE()
company_repo = CompanyRepo()
user_repo = UserRepo()
yesterday_date = date.today() - timedelta(1)
yesterday_date = yesterday_date.strftime("%Y-%m-%d")
today_date = date.today().strftime("%Y-%m-%d")


def list_all_company(per_page=10):
    #r = company_repo.set_transformer(transformers.company).all()
    #r = company_repo.set_transformer(transformers.company)\
    #r = company_repo.skip_list('slice__history', 0, 1)\
    #        .set_excludes([])\
    #        .orderBy('-historyCount').paginate(per_page=per_page)
    r = company_repo.filter_self(stock__ne=None).paginate(per_page=per_page)
    return r
Esempio n. 22
0
 def __init__(self):
     self.company_repo = CompanyRepo()
Esempio n. 23
0
class StockGetter(BaseCommand):
    """
    To Update stock of the company to latest
    """
    STOCK_API = "https://www.quandl.com/api/v3/datasets/{}/{}.json?api_key=xMH7BiBu6s24LHCizug3"

    def __init__(self):
        self.company_repo = CompanyRepo()
        self.stock_repo = StockRepo()
        self.count = 0
        self.lock = allocate_lock()
        self.sleep_time = 1  # in seconds

    def run(self):
        companies = self.company_repo.all()
        complete = companies.count()
        for company in companies:
            start_new_thread(self.read_stock, (company, ))
            time.sleep(self.sleep_time)
        while self.count != complete:
            time.sleep(4)
            continue
        print "Companies synced successfully"

    def _extract_history(self, data):
        history = []
        history_data = data['data']
        if data['database_code'] == 'BSE':
            for d in history_data:
                history.append({
                    "date": d[0],
                    "open": d[1],
                    "high": d[2],
                    "low": d[3],
                    "close": d[4],
                    "last": None,
                    "totalTradeQuantity": None,
                    "turnover": None,
                    "wap": d[5],
                    "noOfShares": d[6],
                    "noOfTrades": d[7],
                    "totalTurnover": d[8],
                    "deliverableQuantity": d[9],
                    "percentageDeliveryQtytoTradedQty": d[10],
                    "spreadHtoL": d[11],
                    "spreadCtoO": d[12],
                })
        elif data['database_code'] == 'NSE':
            for d in history_data:
                history.append({
                    "date": d[0],
                    "open": d[1],
                    "high": d[2],
                    "low": d[3],
                    "last": d[4],
                    "close": d[5],
                    "totalTradeQuantity": d[6],
                    "turnover": d[7],  # in Lacs
                    "wap": None,
                    "noOfShares": None,
                    "noOfTrades": None,
                    "totalTurnover": None,
                    "deliverableQuantity": None,
                    "percentageDeliveryQtytoTradedQty": None,
                    "spreadHtoL": None,
                    "spreadCtoO": None,
                })
        return history

    def read_stock(self, company):
        try:
            url = self.STOCK_API.format(company.stockExchangeCode,
                                        company.code)
            response = requests.get(url)
            if 'dataset' not in response.json():
                print response.json()
                self.sleep_time = 2
                time.sleep(2)
                self.read_stock(company)
            data = response.json()['dataset']
            if (company.refreshedAt -
                    helper.str_to_datetime(data['refreshed_at']).replace(
                        tzinfo=None)) == timedelta(0):
                print company.stockExchangeCode, company.code, company.name, "Continuing"
            else:
                print company.stockExchangeCode, company.code, company.name, "Updating"
                company.refreshedAt = helper.str_to_datetime(
                    data['refreshed_at'])
                company.oldestAvailableDate = helper.str_to_datetime(
                    data['oldest_available_date'])
                company.newAvailableDate = helper.str_to_datetime(
                    data['newest_available_date'])
                company.history = helper.str_to_datetime(data)
                company.save()
        except Exception as e:
            print "Error (" + company.code + "): " + e.__str__()
            pass
        self.lock.acquire()
        self.count += 1
        self.lock.release()
Esempio n. 24
0
 def __init__(self):
     self.company_repo = CompanyRepo()
     self.stock_repo = StockRepo()
Esempio n. 25
0
def list_companies():
    return CompanyRepo().set_order_by('name').paginate()
Esempio n. 26
0
def list_replies_of_comments(company_id, comment_id):
    company = CompanyRepo().get_company(company_id)
    comment = CommentRepo().get(id=comment_id)
    return ReplyCommentRepo().set_transformer(
        transformers.transform_reply).c_paginate(comment.replies)
Esempio n. 27
0
 def __init__(self):
     self.company_repo = CompanyRepo()
     self.stock_repo = StockRepo()
     self.company_details_repo = CompanyDetailsRepo()
Esempio n. 28
0
class FirstTimeDataSeeder(BaseCommand):
    json_path = APP_DIR + '/mod_utils/stockdetails/'
    NSE_PATH = APP_DIR + '/mod_utils/resources/NSE.csv'
    BSE_PATH = APP_DIR + '/mod_utils/resources/BSE.csv'
    STOCK_API = "https://www.quandl.com/api/v3/datasets/{}.json?api_key=xMH7BiBu6s24LHCizug3"
    completed = 0
    lock = allocate_lock()

    def __init__(self):
        self.company_repo = CompanyRepo()
        self.stock_repo = StockRepo()

    def run(self):
        start_new_thread(self.read_stock, (self.NSE_PATH, ))
        start_new_thread(self.read_stock, (self.BSE_PATH, ))
        while self.completed != 2:
            time.sleep(2)
        """
        self.create_from_json_files()
        """
        return

    def create_from_json_files(self):
        json_files = fileop.list_all_files(self.json_path,
                                           only_files=True,
                                           extension='json',
                                           with_path=True)
        for json_file in json_files:
            try:
                print json_file
                json_data = json.load(fileop.open_file(json_file))
                company_data = self._extract_company_data(json_data)
                self.company_repo.create(company_data)
            except Exception as e:
                fileop.append(BASE_DIR + '/error_company.txt',
                              json_file + " " + e.__str__() + "\n")

    def read_stock(self, csv_file_path):
        count = 1
        with fileop.open_file(csv_file_path, 'rb') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                market_n_company_code = row['market_n_company_code']
                company_name = row['company_name']
                print count, market_n_company_code, company_name
                if self.company_code_exists(
                        market_n_company_code.split('/')[-1]):
                    continue
                try:
                    count += 1
                    url = self.STOCK_API.format(market_n_company_code)
                    result = requests.get(url)
                    if result.status_code == 200:
                        self.create_company(result.json()['dataset'])
                    else:
                        fileop.append(BASE_DIR + '/quandl_error_company.txt', market_n_company_code + " " + csv_file_path\
                                + " " + company_name + " " + str(result.status_code) + "\n")
                except Exception as e:
                    fileop.append(
                        BASE_DIR + '/quandl_error_company.txt',
                        "Error in " + csv_file_path + " " + e.__str__() + "\n")

        fileop.append(BASE_DIR + '/quandl_results.txt',
                      csv_file_path + " = " + str(count) + "\n")
        self.lock.acquire()
        self.completed += 1
        self.lock.release()
        return

    def company_code_exists(self, company_code):
        return self.company_repo.filter(code=company_code).count()

    def create_company(self, json_data):
        company_data = self._extract_company_data(json_data)
        return self.company_repo.create(company_data)

    def _extract_company_data(self, data):
        history = self._extract_history(data)
        return {
            "name":
            data['name'],
            "code":
            data['dataset_code'],
            #"quandlCode": data['dataset_code'],
            "description":
            data['description'],
            "stockExchangeCode":
            data['database_code'],
            "type":
            data['type'],
            "frequency":
            data['frequency'],
            "refreshedAt":
            helper.str_to_datetime(data['refreshed_at']),
            "oldestAvailableDate":
            helper.str_to_datetime(data['oldest_available_date']),
            "newAvailableDate":
            helper.str_to_datetime(data['newest_available_date']),
            "history":
            history,
            "historyCount":
            len(history),
        }

    def _extract_history(self, data):
        history = []
        history_data = data['data']
        if data['database_code'] == 'BSE':
            for d in history_data:
                history.append({
                    "date": d[0],
                    "open": d[1],
                    "high": d[2],
                    "low": d[3],
                    "close": d[4],
                    "last": None,
                    "totalTradeQuantity": None,
                    "turnover": None,
                    "wap": d[5],
                    "noOfShares": d[6],
                    "noOfTrades": d[7],
                    "totalTurnover": d[8],
                    "deliverableQuantity": d[9],
                    "percentageDeliveryQtytoTradedQty": d[10],
                    "spreadHtoL": d[11],
                    "spreadCtoO": d[12],
                })
        elif data['database_code'] == 'NSE':
            for d in history_data:
                history.append({
                    "date": d[0],
                    "open": d[1],
                    "high": d[2],
                    "low": d[3],
                    "last": d[4],
                    "close": d[5],
                    "totalTradeQuantity": d[6],
                    "turnover": d[7],  # in Lacs
                    "wap": None,
                    "noOfShares": None,
                    "noOfTrades": None,
                    "totalTurnover": None,
                    "deliverableQuantity": None,
                    "percentageDeliveryQtytoTradedQty": None,
                    "spreadHtoL": None,
                    "spreadCtoO": None,
                })
        return history

    def _stock_details(self, data):
        return {}