def user(request): if request.user.is_authenticated(): flag = 0 if request.method == "POST": form = search_form(request.POST) search_keyword = request.POST.get('search_query') request.session['search_keyword'] = search_keyword #twitter_data=twitter_scraper(search_keyword) twitter_data = dict() reddit_data = reddit_scraper(search_keyword) return render( request, 'user/user.html', { 'search_form': search_form, 'twitter_data': twitter_data, 'search_keyword': search_keyword, 'reddit_data': reddit_data, }) else: form = search_form() return render(request, 'user/user.html', { 'search_form': form, 'flag': flag }) else: return redirect('signin')
def reddit_detailed_analysis(request): if request.user.is_authenticated(): flag = 0 if request.session.get('search_keyword'): flag = 1 search_keyword = request.session['search_keyword'] form = search_form() with open('reddit.json', 'r') as file: data = json.load(file) reddit = [] it = 0 temp_image = 'https://i.imgur.com/wesPbMx.jpg' for post in data['data']['children']: reddit.append({ 'title': post['data']['title'], 'permalink': post['data']['permalink'], 'author': post['data']['author'], 'url': post['data']['url'], 'thumbnail': post['data']['thumbnail'], 'num_comments': post['data']['num_comments'], 'subreddit': post['data']['subreddit_name_prefixed'], 'thumbnail_height': post['data']['thumbnail_height'], 'thumbnail_width': post['data']['thumbnail_width'] }) if 'https://www.reddit.com' in reddit[it]['url']: reddit[it]['url'] = "" if reddit[it]['thumbnail'] == 'self': reddit[it]['thumbnail'] = temp_image it = it + 1 return render( request, 'user/reddit.html', { 'search_keyword': search_keyword, 'search_form': form, 'reddit': reddit, }) else: if not flag: error = "please enter the keyword first" return redirect('user') else: return render(request, 'user/twitter.html', { 'search_keyword': search_keyword, 'search_form': form }) else: return redirect('signin')
def twitter_detailed_analysis(request): if request.user.is_authenticated(): flag = 0 if request.session.get('search_keyword'): flag = 1 search_keyword = request.session['search_keyword'] form = search_form() with open('twitter_result.json', 'r') as file: data = json.load(file) polarity = sentiment_analysis(data) total = polarity['positive'] + polarity['negative'] + polarity[ 'neutral'] whenCreated = {} dayofWeek = {} for tweets in data: print(tweets['statuses']) for tweet in tweets['statuses']: d = parse(tweet['created_at']).isoformat()[:10] if not d in whenCreated: whenCreated[d] = 1 else: whenCreated[d] += 1 weekDay = parse(tweet['created_at']).strftime("%A") if not weekDay in dayofWeek: dayofWeek[weekDay] = 1 else: dayofWeek[weekDay] += 1 return render( request, 'user/twitter.html', { 'search_keyword': search_keyword, 'search_form': form, 'tweets': data, 'whenCreated': whenCreated, 'dayOfWeek': dayofWeek, 'polarity': polarity, 'total': total }) else: if not flag: error = "please enter the keyword first" return redirect('user') else: return render(request, 'user/twitter.html', { 'search_keyword': search_keyword, 'search_form': form }) else: return redirect('signin')
def twitter_detailed_analysis(request): if request.user.is_authenticated(): flag=0 if request.session.get('search_keyword'): flag=1 search_keyword=request.session['search_keyword'] form=search_form() with open('twitter_result.json','r') as file: data=json.load(file) polarity=sentiment_analysis(data) return render(request,'user/twitter.html',{'search_keyword':search_keyword, 'search_form':form, 'tweets':data, 'polarity':polarity }) else: if not flag: error="please enter the keyword first" return redirect('user') else: return render(request,'user/twitter.html',{'search_keyword':search_keyword,'search_form':form}) else: return redirect('signin')