Exemplo n.º 1
0
    def delete(self):
        current_username = get_jwt_identity()['username']
        current_user = User.find_by_username(current_username)

        data = subscriptions_likes_parser.parse_args()
        res_id = data.get('research_id')
        res = Research.find_by_id(res_id)

        if current_user is None:
            return {
                'response': False,
                'message': 'User {} does not exist'.format(current_username)
            }, 400

        if res is None:
            return {
                'response': False,
                'message': 'Research {} does not exist'.format(res.topic)
            }, 400

        if current_user.unlike(res):
            from app import db
            db.session.commit()

        return {
            'response':
            True,
            'message':
            '{} removed like froom {}'.format(current_username, res.topic)
        }
Exemplo n.º 2
0
    def put(self):
        from flask import request
        from sqlalchemy import and_
        from requests import get
        from json import loads

        res_id = int(request.args.get('id'))
        current_username = get_jwt_identity()['username']
        current_user = User.find_by_username(current_username)

        permissions = db.session.query(UserResearchPermission).filter(
            and_(UserResearchPermission.userId == current_user.id,
                 UserResearchPermission.researchId == res_id)).first()

        if permissions is None:
            return {
                "message": "You don't have permission to edit this research"
            }, 400

        response = req(
            'http://localhost:5000/ml/api/v1.0/update?res_id={}'.format(
                new_research.id)).json()

        if response['done'] == False:
            return {"message": "Internal server error"}, 500

        current_res = Research.find_by_id(res_id)
        itters = ConductedResearch.query.filter_by(researchId=res_id).all()
        print(itters[-1].id)

        current_res.conducted.append(itters[-1])
        db.session.add(itters[-1])
        db.session.commit()

        return {"message": "updated"}
Exemplo n.º 3
0
    def get(self):
        from flask import request
        res = Research.find_by_id(request.args.get('research_id'))

        return {

        }
Exemplo n.º 4
0
    def post(self):
        current_username = get_jwt_identity()
        current_user = User.find_by_username(current_username)

        data = subscriptions_likes_parser.parse_args()
        res_id = data.get('research_id')
        res = Research.find_by_id(res_id)

        if current_user is None:
            return {
                'response': False,
                'message': 'User {} does not exist'.format(current_username)
            }, 400
        
        if res is None:
            return {
                'response': False,
                'message': 'Research {} does not exist'.format(res.topic)
            }, 400
        
        if current_user.subscribe(res):
            from app import db
            db.session.commit()

        return {
            'response': True,
            'message': '{} is subscribed to {} from now.'.format(current_username, res.topic)
        }
Exemplo n.º 5
0
def init_db():
    db.drop_all()
    db.create_all()

    # Init tag
    db.session.add(Tag(id=1, name="渣三维"))
    db.session.add(Tag(id=2, name="转专业"))
    db.session.add(Tag(id=3, name="高GT"))
    db.session.add(Tag(id=4, name="高GPA"))

    # Init project
    db.session.add(Project(id=1, name="无相关实习经历,有个人项目", value=2))
    db.session.add(Project(id=2, name="国内小公司实习", value=2))
    db.session.add(Project(id=3, name="国内大公司实习", value=3))
    db.session.add(Project(id=4, name="BAT实习", value=4))
    db.session.add(Project(id=5, name="外企实习", value=5))

    # Init Recommendation
    db.session.add(Recommendation(id=1, name="无推荐信", value=1))
    db.session.add(Recommendation(id=2, name="国内普通推", value=2))
    db.session.add(Recommendation(id=3, name="海外普通推", value=3))
    db.session.add(Recommendation(id=4, name="国内牛推", value=4))
    db.session.add(Recommendation(id=5, name="海外牛推", value=5))

    # Init Research
    db.session.add(Research(id=1, name="无科研经历", value=1))
    db.session.add(Research(id=2, name="初步的科研经历", value=2))
    db.session.add(Research(id=3, name="大学实验室做过较深入的研究", value=3))
    db.session.add(Research(id=4, name="1~3个月的海外研究经历", value=4))
    db.session.add(Research(id=5, name="大于3个月的海外研究经历", value=5))

    # Init Country
    db.session.add(Country(id=1, name="美国"))
    db.session.add(Country(id=2, name="英国"))
    db.session.add(Country(id=3, name="加拿大"))
    db.session.add(Country(id=4, name="澳大利亚"))
    db.session.add(Country(id=5, name="德国"))
    db.session.add(Country(id=6, name="法国"))
    db.session.add(Country(id=7, name="香港"))
    db.session.add(Country(id=8, name="日本"))
    db.session.add(Country(id=9, name="新加坡"))

    db.session.commit()


# https://api.github.com/search/repositories?q=tetris+language:assembly&sort=stars&order=desc
Exemplo n.º 6
0
    def get(self):
        from flask import request
        try:
            res = Research.find_by_id(request.args.get('res_id'))
            itter = res.conducted[-1].news[0]

            articles = itter.news_list

            sentiment = {
                'positive_percent': (itter.pos_count_general / 10) * 100,
                'negative_percent': (itter.neg_count_general / 10) * 100,
                'neutral_percent':
                ((10 - itter.pos_count_general - itter.neg_count_general) / 10)
                * 100
            }

            news_art = [{
                'source': a.source.split('||')[1],
                'source_url': a.source.split('||')[0],
                'title': a.title,
                'url': a.link
            } for a in articles]

            import nltk
            nltk.download("stopwords")

            from nltk.corpus import stopwords
            from string import punctuation

            stop_words = set(stopwords.words('english')).union(
                set(stopwords.words("russian")))
            text = ' '.join([t.text.lower() for t in articles])

            wordcount = {}

            for word in text.split():
                if word not in stop_words or word not in [
                        'the', 'a', 'an', 'and'
                ]:
                    if word not in wordcount:
                        wordcount[word] = 1
                    else:
                        wordcount[word] += 1

            from collections import Counter

            word_counter = [w[0] for w in Counter(wordcount).most_common(10)]

            return {
                'news': news_art,
                'words': word_counter,
                'sentiment': sentiment
            }

        except Exception as e:
            print(e)
            return {"message": "No news or internal error"}
Exemplo n.º 7
0
    def get(self):
        from app import db
        from flask import request
        from sqlalchemy import desc

        research = Research.find_by_id(int(request.args.get('res_id')))
        owner = User.find_by_id(research.ownerId)
        last = None

        try:
            last = db.session.query(ConductedResearch.date).filter(
                ConductedResearch.researchId == research.id).order_by(
                    desc(ConductedResearch.date)).first()[0].strftime(
                        '%d.%m.%Y')
        except:
            last = research.creationDate.strftime('%d.%m.%Y')

        return {
            'id':
            research.id,
            'topic':
            research.topic,
            'description':
            research.description,
            'creation':
            str(research.creationDate.strftime('%d.%m.%Y')),
            'last_update':
            str(last),
            'views':
            research.views,
            'owner': {
                'id': owner.id,
                'username': owner.username,
                'fullname': owner.fullname
            },
            'keywords': [
                x[0] for x in db.session.query(ResearchKeyword.keyword).filter(
                    ResearchKeyword.researchId == research.id).all()
            ],
            'active_modules': [
                x[0] for x in db.session.query(ResearchModule.module).filter(
                    ResearchModule.researchId == research.id).all()
            ],
            'likes':
            len(
                db.session.query(likes).filter(
                    likes.c.research_id == research.id).all()),
            'subscriptions':
            len(
                db.session.query(subscriptions).filter(
                    subscriptions.c.research_id == research.id).all())
        }
Exemplo n.º 8
0
    def get(self):
        from flask import request
        res_id = request.args.get('research_id')
        res = Research.find_by_id(res_id)

        if res is None:
            return {
                'response': False,
                'message': 'Research {} does not exist'.format(res.topic)
            }, 400

        return {
            'response': True,
            'message': 'Research {} got {} views'.format(res.topic, res.views),
            'views': res.views
        }
Exemplo n.º 9
0
    def get(self):
        from flask import request
        res = Research.find_by_id(int(request.args.get('res_id')))

        def to_json(x):
            return {
                "rate": x.rate,
                "text": x.text,
                "sentiment": x.sentimentScore
            }

        try:
            from app.models import PlayStoreResearch, PlayStoreReview
            pl_res = PlayStoreResearch.query.filter_by(id=res.id).first()
            pl_reviews = PlayStoreReview.query.filter_by(
                playStoreResearchId=res.id).all()

            return {
                "hist": {
                    "one": pl_res.rateOneCount,
                    "two": pl_res.rateTwoCount,
                    "three": pl_res.rateThreeCount,
                    "four": pl_res.rateFourCount,
                    "five": pl_res.rateFiveCount
                },
                "app_info": {
                    "name":
                    pl_res.name,
                    "rate":
                    pl_res.averageRating,
                    "downloads":
                    pl_res.downloads,
                    "reviews":
                    pl_res.maxReviews,
                    "not_clear_reviews":
                    pl_res.maxReviews - int(0.25 - pl_res.maxReviews)
                },
                "top_reviews": list(map(lambda x: to_json(x),
                                        list(pl_reviews)))
            }

        except:
            return {"message": "internal server error"}, 500
Exemplo n.º 10
0
    def get(self):
        from datetime import datetime, timedelta
        from flask import request
        search = Research.find_by_id(request.args.get('res_id')).search[0]
        start = request.args.get('start', None)
        end = request.args.get('end', None)

        print(start)
        print(end)
        try:
            result = {'popularity': [], 'countries': [], 'related': []}

            for d in search.days:
                result['popularity'].append({
                    'date': d.date.strftime('%d.%m.%Y'),
                    'rate': d.interest
                })

            if start != None:
                start = datetime.strptime(start, '%d.%m.%Y')
                result['popularity'] = [
                    x for x in result['popularity'] if x.creationDate >= start
                ]

            if end != None:
                end = datetime.strptime(end, '%d.%m.%Y') + timedelta(days=1)
                result['popularity'] = [
                    x for x in result['popularity'] if x.creationDate <= start
                ]

            for c in search.countries:
                result['countries'].append({
                    'country': c.country,
                    'rate': c.interest
                })

            for r in search.related:
                result['related'].append(r.topic)
        except Exception as e:
            print('\n\n\n', e, '\n\n\n')
            result = {"message": "Internal error or no instances in database"}

        return result
Exemplo n.º 11
0
    def post(self):
        data = subscriptions_likes_parser.parse_args()
        res_id = data.get('research_id')
        res = Research.find_by_id(res_id)

        if res is None:
            return {
                'response': False,
                'message': 'Research {} does not exist'.format(res.topic)
            }, 400

        res.views += 1
        from app import db
        db.session.commit()

        return {
            'response': True,
            'message': 'Research {} got {} views'.format(res.topic, res.views),
            'views': res.views
        }
Exemplo n.º 12
0
    def get(self):
        try:
            from flask import request
            import pandas as pd
            res = Research.find_by_id(request.args.get('res_id'))
            itter = res.conducted[-1].twitter[0]

            tweets = itter.tweets

            tweets_dates = [t.timestamp.strftime("%d.%m.%Y") for t in tweets]
            timeline = pd.to_datetime(pd.Series(tweets_dates),
                                      format='%d.%m.%Y')
            timeline.index = timeline.dt.to_period('m')
            timeline = timeline.groupby(level=0).size()
            timeline = dict(zip(timeline.index.format(), timeline))

            sentiment = {
                'positive_percent': (itter.pos_count / 201) * 100,
                'negative_percent': (itter.neg_count / 201) * 100,
                'neutral_percent':
                ((201 - itter.pos_count - itter.neg_count) / 201) * 100
            }

            import nltk
            nltk.download("stopwords")

            from nltk.corpus import stopwords
            from string import punctuation

            stop_words = set(stopwords.words('english')).union(
                set(stopwords.words("russian")))
            text = ' '.join([t.text.lower() for t in tweets])

            wordcount = {}

            for word in text.split():
                if word not in stop_words or word not in [
                        'the', 'a', 'an', 'and'
                ]:
                    if word not in wordcount:
                        wordcount[word] = 1
                    else:
                        wordcount[word] += 1

            from collections import Counter

            word_counter = Counter(wordcount).most_common(10)

            freq_words = []

            for w in word_counter:
                freq_words.append({w[0]: w[1]})

            return {
                'popularity_rate':
                timeline,
                'sentiment':
                sentiment,
                'frequent_words':
                freq_words,
                'tweets': [{
                    'url': t.source,
                    'sentiment': t.sentimentScore
                } for t in tweets]
            }

        except Exception as e:
            print(e)
            return {"message": "Internal server error or no research"}
Exemplo n.º 13
0
    def post(self):
        data = create_research.parse_args()

        current_username = get_jwt_identity()['username']
        current_user = User.find_by_username(current_username)
        new_research = Research()
        print(new_research.id)
        try:
            new_research.topic = data.get('topic')
            new_research.description = data.get('description')

            for word in data.get('keywords'):
                new_keyword = ResearchKeyword(keyword=word,
                                              researchId=new_research.id)
                new_research.keywords.append(new_keyword)
                db.session.add(new_keyword)

            for module in data.get('modules'):
                new_module = ResearchModule(module=module,
                                            researchId=new_research.id)
                new_research.modules.append(new_module)
                db.session.add(new_module)

            new_research.updateInterval = data.get('update_interval')
            new_research.type_of_research = data.get('isPublic')
            new_research.analysers = data.get('analysers')
            new_research.appId = data.get('app_id')
            new_research.appName = data.get('app_name')
            new_research.appDev = data.get('app_dev')

            if 'search' in data.get('modules'):
                new_search = SearchTrends(id=new_research.id)
                new_search.query = ' '.join(data.get('keywords')[:5])
                new_research.search.append(new_search)
                db.session.add(new_search)

            user_res = UserResearchPermission(userId=current_user.id,
                                              researchId=new_research.id,
                                              permission=True)

            user_res.researches.append(new_research)
            current_user.my_researches.append(user_res)
            current_user.owners.append(new_research)

            db.session.add(new_research)
            db.session.commit()

        except Exception as e:
            print(e)
            return {'response': False, 'message': 'Internal server error'}, 500

        from requests import get as req

        try:
            print(new_research.id)
            response = req(
                'http://localhost:5000/ml/api/v1.0/update?res_id={}'.format(
                    new_research.id)).json()
            if response['done'] == False:
                return {"message": "Internal server error"}, 500

            itters = ConductedResearch.query.filter_by(
                researchId=new_research.id).all()

            new_research.conducted.append(itters[-1])
            db.session.add(itters[-1])
            db.session.commit()
            print('commit')

        except Exception as e:
            return {'response': False, 'message': str(e)}, 404

        return {
            'response': True,
            'id': new_research.id,
            'message': 'Research {} was created'.format(new_research.topic)
        }
Exemplo n.º 14
0
    def post(self):
        data = create_research.parse_args()
        
        current_username = get_jwt_identity()
        current_user = User.find_by_username(current_username)

        try:
            new_research = Research()
            new_research.topic = data.get('topic')
            new_research.description = data.get('description')

            for word in data.get('keywords'):
                new_keyword = ResearchKeyword(
                    keyword=word, 
                    researchId=new_research.id
                )
                new_research.keywords.append(new_keyword)
                db.session.add(new_keyword)
            
            print('\n\n\n',data.get('modules'),'\n\n\n')
            for module in data.get('modules'):
                new_module = ResearchModule(
                    module=module,
                    researchId=new_research.id
                )
                new_research.modules.append(new_module)
                db.session.add(new_module)
            
            new_research.updateInterval = data.get('update_interval')
            new_research.type_of_research = data.get('isPublic')
            new_research.analysers = data.get('analysers')
            new_research.appId = data.get('app_id')
            new_research.appName = data.get('app_name')
            new_research.appDev = data.get('app_dev')

            if 'search' in data.get('modules'):
                new_search = SearchTrends(id=new_research.id)
                new_search.query = ' '.join(data.get('keywords')[:5])
                new_research.search.append(new_search)
                db.session.add(new_search)

            user_res = UserResearchPermission(
                userId=current_user.id,
                researchId=new_research.id,
                permission=True
            )
            
            user_res.researches.append(new_research)
            current_user.my_researches.append(user_res)
            current_user.owners.append(new_research)
            
            db.session.add(new_research)
            db.session.commit()
        
        except Exception as e:
            return {
                'response': False,
                'message': 'Internal server error'
            }, 500

        return {
            'response': True,
            'id': new_research.id,
            'message': 'Research {} was created'.format(new_research.topic)
        }
# Init project
db.session.add(Project(id=1, name="无相关实习经历,有个人项目", value=2))
db.session.add(Project(id=2, name="国内小公司实习", value=2))
db.session.add(Project(id=3, name="国内大公司实习", value=3))
db.session.add(Project(id=4, name="BAT实习", value=4))
db.session.add(Project(id=5, name="外企实习", value=5))

# Init Recommendation
db.session.add(Recommendation(id=1, name="无推荐信", value=1))
db.session.add(Recommendation(id=2, name="国内普通推", value=2))
db.session.add(Recommendation(id=3, name="海外普通推", value=3))
db.session.add(Recommendation(id=4, name="国内牛推", value=4))
db.session.add(Recommendation(id=5, name="海外牛推", value=5))

# Init Research
db.session.add(Research(id=1, name="无科研经历", value=1))
db.session.add(Research(id=2, name="初步的科研经历", value=2))
db.session.add(Research(id=3, name="大学实验室做过较深入的研究", value=3))
db.session.add(Research(id=4, name="1~3个月的海外研究经历", value=4))
db.session.add(Research(id=5, name="大于3个月的海外研究经历", value=5))

# Init Country
db.session.add(Country(id=1, name="美国"))
db.session.add(Country(id=2, name="英国"))
db.session.add(Country(id=3, name="加拿大"))
db.session.add(Country(id=4, name="澳大利亚"))
db.session.add(Country(id=5, name="德国"))
db.session.add(Country(id=6, name="法国"))
db.session.add(Country(id=7, name="香港"))
db.session.add(Country(id=8, name="日本"))
db.session.add(Country(id=9, name="新加坡"))