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) }
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"}
def get(self): from flask import request res = Research.find_by_id(request.args.get('research_id')) return { }
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) }
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"}
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()) }
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 }
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
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
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 }
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"}