diff --git a/README.md b/README.md index 1409504..df25169 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CodeClan Collaborative Projekt - Tweet analysis app +# CodeClan Collaborative Project - Tweet analysis app We’ve been approached by an organisation that would like the ability to find out tweet frequency or sentiment by location. We will build a Minimum Viable Product that will retrieve data from Twitter and display it to the user, then explore ways to analyze and display meaningful data in an interesting way. diff --git a/client/public/css/main.css b/client/public/css/main.css index 634d2a9..24fe494 100644 --- a/client/public/css/main.css +++ b/client/public/css/main.css @@ -33,18 +33,64 @@ body { .content-middle { grid-area: content-top; - /* display: flex; */ - /* flex-direction: row; */ + display: flex; + flex-direction: row; + justify-content: space-evenly; +} + +#charts { + margin: 0px; + padding: 0px; + display: flex; + flex-direction: row; + justify-content: space-evenly; +} + +#grid-item-5{ + color: #c4114b; +} + +#grid-item-6{ + color: #15A5CC; +} + +#grid-item-7{ + color: #c4114b; +} + +#grid-item-8{ + color: #15A5CC; +} + +.chart { + margin: 0px; + height: 24rem; + width: 24rem; + /* //width: 49%; */ } /* ============================ DATA: */ -.grid-summary{ +.grid-summary-retweets{ display: grid; - width: 60%; - margin-left: 2rem; + width: 30%; + margin-left: 0rem; + margin-right: 2rem; + height: 6rem; + grid-template-columns: 1fr 1fr; + border-bottom: grey solid 2px; + text-align: center; + /* padding-right: 10rem; */ +} + +.grid-summary-favourites{ + display: grid; + width: 30%; + margin-left: 0rem; + margin-right: 2rem; height: 6rem; - grid-template-columns: 1fr 1fr 1fr 1fr; + grid-template-columns: 1fr 1fr; border-bottom: grey solid 2px; + text-align: center; /* padding-right: 10rem; */ } @@ -54,28 +100,15 @@ body { } .content-bottom { grid-area: content-top; - display: flex; - flex-direction: row; + margin-bottom: 2rem; } /* --------------------charts: */ -#charts{ +.showFlex{ margin: 4rem 0 4rem; display: flex; flex-direction: row; } -.chart { - - margin:auto; - min-height: 30rem; - width: 49%; -} - -.chart div{ - /* display: inline-block; */ -} -/* ============================ FOOTER: */ - .footer { grid-area: footer; height: 5rem; @@ -155,6 +188,11 @@ button#submit { box-shadow: 0px 0px 3px 0px rgba(0,0,2,0.22); } +button#submit:hover{ + background-color: #15A5CC; + color: white; +} + .search-history { margin-left: 2rem; margin-bottom: 0rem; @@ -170,35 +208,60 @@ button#submit { width: 66%; } -#result-container { +#loading-overlay { + position: absolute; margin-top: 2rem; margin-left: 2rem; + margin-bottom: 2rem; + margin-right: 2rem; + right: 0; + height: 35rem; + width: 62.2%; + z-index: 99; + background-color: #58595b; + justify-self: flex-end; + opacity: 0.7; } -h2 { - font-size: 18px; +.modal-content { + display: flex; + align-items: center; + justify-content: center; + width: 100%; + height: 100%; } -.h2 { - font-size: 28px; - font-weight: normal; +#loading-modal{ + background-color: white; + position: absolute; + margin-top: 2rem; + margin-left: 2rem; + margin-bottom: 2rem; + margin-right: 2rem; + top: 39%; + right: 21%; + height: 6rem; + width: 20%; + z-index: 999; + justify-self: flex-end; + opacity: 1; + color: black; } -.chart-1 { - display: flex; - flex-direction: column; - align-items: center; +#result-container { + margin-top: 2rem; + margin-left: 2rem; } -.chart-2 { - display: flex; - flex-direction: column; - align-items: center; +/* h2 { + font-size: 18px; +} */ + +.h2 { + font-size: 28px; + font-weight: normal; } -/* div.chart-1, div.chart-2 { - padding-left: 5%; -} */ .navbar h2 { margin-top:1.7rem; margin-right: 6rem; @@ -217,10 +280,17 @@ ul#search-results-list { color: black; background-color: white; background-clip: content-box; - padding-left: 100px; - padding-right: 250px; + list-style: none; + margin: 0; + padding: 0; } +ul#search-results-list li { + margin-bottom: 1rem; +} + + + .hidden { display: none; diff --git a/client/public/index.html b/client/public/index.html index 6456c5f..0f4178b 100644 --- a/client/public/index.html +++ b/client/public/index.html @@ -5,6 +5,22 @@ + @@ -44,11 +60,15 @@

CodeClan Analytica

Ever wondered where all those tweets come from?

-

When you enter a word or phrase above, we use it to gather all the tweets from the last 7 days that - mention that word or phrase.

Since the tweets come from all over the world, we can anaylse their - location data and plot on the map exactly where they originated from

... for research.

+

When you enter a word or phrase above, we use it to gather the last 3000 tweets in realtime that mention that + word or phrase.

Since the tweets come from all over the world, we can anaylse their location data and + plot on the map exactly where they originated from.

... for research purposes.

+ +
@@ -57,33 +77,32 @@

Ever wondered where all those tweets come from?

-
-
Tweets total
-
Retweets total
-
Followers total
-
Sentiment
+ - - + -
-

Search Results:

-
    -
+
diff --git a/client/src/app.js b/client/src/app.js index 6c78549..547a8d8 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -24,13 +24,14 @@ document.addEventListener('DOMContentLoaded', () => { const charts = document.querySelector('#charts') const chartView = new ChartView(charts); - const totals = document.querySelector('#summary'); - const totalsView = new TotalsView(totals); + const positiveTotals = document.querySelector('#summary1'); + const negativeTotals = document.querySelector('#summary2'); + const totalsView = new TotalsView(positiveTotals, negativeTotals); formView.bindEvents(); listView.bindEvents(); - happySearches.bindEvents('%20%3A%29'); //'%20%3A%29' - happy + happySearches.bindEvents('%20%3A%29', true); //'%20%3A%29' - happy sadSearches.bindEvents('%20%3A%28'); // '%20%3A%28' - sad mapView.bindEvents(); chartView.bindEvents(); diff --git a/client/src/models/searches.js b/client/src/models/searches.js index 33a6113..999fbba 100644 --- a/client/src/models/searches.js +++ b/client/src/models/searches.js @@ -7,9 +7,15 @@ const Searches = function (url) { this.APIrequest = new Request('http://localhost:3000/api/search-results'); } -Searches.prototype.bindEvents = function (sentimentQuery) { +Searches.prototype.bindEvents = function (sentimentQuery, primary) { PubSub.subscribe('FormView:search-term-submitted', (event) => { - this.postSearch(event.detail); + + // todo move the db stuff to another bit + if (primary) { + this.postSearch(event.detail); + this.renderLoadingSpinner(); + } + this.getSearchResults(event.detail.searchTerm, sentimentQuery); // console.log(event.detail.searchTerm); }) @@ -30,12 +36,14 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) { let query = null; let tweetChannel = null; let calculationChannel = null; + let coordinateChannel = null; // if there is no sentiment query // search without one and set the channel variable to be generic if (!sentimentQuery){ query = `?query=${searchTerm}`; tweetChannel = 'Searches:tweet-data-loaded'; calculationChannel = 'Searches:totals-calulated'; + coordinateChannel = 'Searches:tweet-coordinates-loaded'; // if there is a sentiment query // add the query to the search } else @@ -45,6 +53,7 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) { if (sentimentQuery === '%20%3A%29'){ tweetChannel = 'Searches:happy-tweet-data-loaded'; calculationChannel = 'Searches:happy-totals-calulated'; + coordinateChannel = 'Searches:happy-tweet-coordinates-loaded'; } // if the extra query is for sad sentiment @@ -52,7 +61,11 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) { else if (sentimentQuery === '%20%3A%28'){ tweetChannel = 'Searches:sad-tweet-data-loaded'; calculationChannel = 'Searches:sad-totals-calulated'; + coordinateChannel = 'Searches:sad-tweet-coordinates-loaded'; } + +console.log("searching for " + query); + this.APIrequest.get(query) .then((tweets) => { const totals = this.calculator(tweets); @@ -72,10 +85,7 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) { } }) PubSub.publish(tweetChannel, tweets); - - const stringifiedCoordinates = JSON.stringify(tweetsCoordinates); - // console.log('stringifiedtweetscoordinates: ', stringifiedCoordinates); - PubSub.publish('Searches:tweet-coordinates-loaded', tweetsCoordinates); + PubSub.publish(coordinateChannel, tweetsCoordinates); }) .catch(console.error); }; @@ -106,7 +116,14 @@ Searches.prototype.calculator = function (tweetData) { totals.retweets += tweet.retweets; totals.favourites += tweet.favourites; }) - console.log(totals) return totals; } + +Searches.prototype.renderLoadingSpinner = function() { + const loadingOverlay = document.querySelector('#loading-overlay'); + const loadingModal = document.querySelector('#loading-modal'); + loadingOverlay.classList.remove('hidden'); + loadingModal.classList.remove('hidden'); +}; + module.exports = Searches; diff --git a/client/src/views/chart_view.js b/client/src/views/chart_view.js index 3cf1199..db70213 100644 --- a/client/src/views/chart_view.js +++ b/client/src/views/chart_view.js @@ -1,7 +1,7 @@ var echarts = require('echarts'); const PubSub = require('../helpers/pub_sub.js'); -const ViewChart = function(chartHtmlElement){ +const ViewChart = function (chartHtmlElement) { this.myChart_1 = null; this.myChart_2 = null; this.chartHtmlElement = chartHtmlElement; @@ -10,29 +10,50 @@ const ViewChart = function(chartHtmlElement){ ViewChart.prototype.bindEvents = function () { - - this.myChart_1 = echarts.init(document.getElementById('chart_1')) - this.myChart_2 = echarts.init(document.getElementById('chart_2')) - this.retweetsValues = []; - this.favouritesValues = []; + this.retweetsValues = {}; + this.favouritesValues = {}; + + PubSub.subscribe('Searches:happy-totals-calulated', (event) => { - this.retweetsValues.push(event.detail.retweets); + const chart1 = document.querySelector('#chart_1'); + this.chartHtmlElement.classList.remove('hidden'); + chart1.classList.add('chart'); + this.myChart_1 = echarts.init(document.getElementById('chart_1')) + this.myChart_2 = echarts.init(document.getElementById('chart_2')) + this.retweetsValues.happyRetweets = event.detail.retweets; + this.favouritesValues.happyFavourites = event.detail.favourites; + // this.retweetsValues.push(event.detail.retweets); console.log('happy retweets from event: ', event.detail.retweets); - if (this.retweetsValues.length === 2){ - console.log('both values are ready from happy listener: ', this.retweetsValues); - this.renderChart(this.retweetsValues[0], this.retweetsValues[1], 'Positive vs Negative Retweets', this.myChart_1); - this.retweetsValues = []; + console.log('happy retweets from object: ', this.retweetsValues); + if (this.retweetsValues.happyRetweets && this.retweetsValues.sadRetweets) { + console.log('both values are ready from happy listener: ', this.retweetsValues); + this.renderChart(this.retweetsValues.happyRetweets, this.retweetsValues.sadRetweets, 'Positive vs Negative Retweets', this.myChart_1); + //this.retweetsValues = []; + } + if (this.favouritesValues.happyFavourites && this.favouritesValues.sadFavourites) { + console.log('both values are ready from happy listener: ', this.favouritesValues); + this.renderChart(this.favouritesValues.happyFavourites, this.favouritesValues.sadFavourites, 'Positive vs Negative Retweets', this.myChart_2); + //this.retweetsValues = []; } //this.renderChart(event.detail.favourites, event.detail.favourites, 'Positive vs Negative Likes', this.myChart_2); }) PubSub.subscribe('Searches:sad-totals-calulated', (event) => { - this.retweetsValues.push(event.detail.retweets); + const chart2 = document.querySelector('#chart_2'); + this.chartHtmlElement.classList.remove('hidden'); + chart2.classList.add('chart'); + this.retweetsValues.sadRetweets = event.detail.retweets; + this.favouritesValues.sadFavourites = event.detail.favourites; console.log('sad retweets from event: ', event.detail.retweets); - if (this.retweetsValues.length === 2){ + if (this.retweetsValues.length === 2) { console.log('both values are ready from sad listener: ', this.retweetsValues); - this.renderChart(this.retweetsValues[0], this.retweetsValues[1], 'Positive vs Negative Retweets', this.myChart_1); - this.retweetsValues = []; + this.renderChart(this.retweetsValues.happyRetweets, this.retweetsValues.sadRetweets, 'Positive vs Negative Retweets', this.myChart_1); + //this.retweetsValues = []; + } + if (this.favouritesValues.happyFavourites && this.favouritesValues.sadFavourites) { + console.log('both values are ready from happy listener: ', this.favouritesValues); + this.renderChart(this.favouritesValues.happyFavourites, this.favouritesValues.sadFavourites, 'Positive vs Negative Retweets', this.myChart_2); + //this.retweetsValues = []; } //this.renderChart(event.detail.favourites, event.detail.favourites, 'Positive vs Negative Likes', this.myChart_2); }) @@ -40,17 +61,34 @@ ViewChart.prototype.bindEvents = function () { ViewChart.prototype.renderChart = function (chartData_positive, chartData_negative, title, chart_x) { chart_x.setOption({ - title: { - text: title + // title: { + // text: title + // }, + color: ['#c4114b','#15A5CC'], + grid: { + left: 0, + top: 0, + right: 0, + bottom: 0 }, tooltip: {}, series: [{ - radius: '50%', + radius: '70%', type: 'pie', data: [ { value: chartData_positive, name: `positive: ${chartData_positive}` }, { value: chartData_negative, name: `negative: ${chartData_negative}` }, - ] + ], + itemStyle: { + normal: { + label: { + show: false + }, + labelLine: { + show: false + } + } + } }] }); } diff --git a/client/src/views/list_view.js b/client/src/views/list_view.js index 8791d3d..78bedc4 100644 --- a/client/src/views/list_view.js +++ b/client/src/views/list_view.js @@ -8,6 +8,14 @@ const ListView = function (listElement, searchResultsListElement) { } ListView.prototype.bindEvents = function () { + let allTweets = []; + let eventCounter = 0; + + PubSub.subscribe('FormView:search-term-submitted', (event) => { + eventCounter = 0; + allTweets = []; + }); + PubSub.subscribe('Searches:searches-data-loaded', (event) => { this.render(event.detail); }) @@ -16,12 +24,22 @@ ListView.prototype.bindEvents = function () { // this.renderSearchResultsList(event.detail); // }); PubSub.subscribe('Searches:happy-tweet-data-loaded', (event) => { + eventCounter += 1; + allTweets = allTweets.concat(event.detail, allTweets); + if (eventCounter === 2) { + this.renderTop5Tweets(allTweets); + } console.log('got happy tweets') - this.renderSearchResultsList(event.detail); + //this.renderSearchResultsList(event.detail); }); PubSub.subscribe('Searches:sad-tweet-data-loaded', (event) => { + eventCounter += 1; + allTweets = allTweets.concat(event.detail, allTweets); + if (eventCounter === 2) { + this.renderTop5Tweets(allTweets); + } console.log('got sad tweets') - this.renderSearchResultsList(event.detail); + //this.renderSearchResultsList(event.detail); }); }; @@ -35,7 +53,17 @@ ListView.prototype.renderSearchResultsList = function (searchResults) { ListView.prototype.renderOneSearchResult = function (searchResult) { const searchResultItem = document.createElement('li'); searchResultItem.textContent = searchResult.text; + searchResultItem.id = 'tweet' + searchResult.id; + //console.log(searchResult.retweets); this.searchResultsListElement.appendChild(searchResultItem); + twttr.widgets.createTweet( + ' ' + searchResult.id, + document.getElementById('tweet' + searchResult.id), + console.log(searchResult.id), + { + theme: 'light' + } + ); } ListView.prototype.render = function (searches) { @@ -61,14 +89,27 @@ ListView.prototype.createDeleteButton = function (listItemId) { PubSub.publish('ListView:delete-clicked', listItemId); }) - const historyButtons = document.querySelectorAll('#list button'); - const list = document.querySelector('#list'); - const buttonCount = historyButtons.length; - if (buttonCount >= 4) { - list.lastChild.remove(); - }; + // const historyButtons = document.querySelectorAll('#list button'); + // const list = document.querySelector('#list'); + // const buttonCount = historyButtons.length; + // if (buttonCount >= 6) { + // list.lastChild.remove(); + // }; return deleteButton } +ListView.prototype.renderTop5Tweets = function (tweets) { + tweets.sort((a, b) => { + return b.retweets - a.retweets; + }); + const top5Tweets = tweets.slice(0, 5); + const container = document.querySelector('#result-container'); + container.classList.remove('hidden'); + this.searchResultsListElement.innerHTML = ""; + top5Tweets.forEach((tweet) => { + this.renderOneSearchResult(tweet); + }); +}; + -module.exports = ListView; \ No newline at end of file +module.exports = ListView; diff --git a/client/src/views/map_view.js b/client/src/views/map_view.js index 7e0940f..7d6959b 100644 --- a/client/src/views/map_view.js +++ b/client/src/views/map_view.js @@ -7,10 +7,39 @@ const MapView = function(){ const geoJsonTest = new GeoJsonTest(); MapView.prototype.bindEvents = function () { + let eventCounter = 0; + let allCoordinates = []; + + // load empty map on start this.renderMap(); - PubSub.subscribe('Searches:tweet-coordinates-loaded', (event) => { - // console.log('event', event); - this.renderMap(event.detail); + + PubSub.subscribe('FormView:search-term-submitted', (event) => { + eventCounter = 0; + allCoordinates = []; + }); + + PubSub.subscribe('Searches:happy-tweet-coordinates-loaded', (event) => { + eventCounter += 1; + allCoordinates = allCoordinates.concat(event.detail, allCoordinates); + if (eventCounter === 2){ + console.log('all coords happy', allCoordinates); + this.renderMap(allCoordinates); + this.removeLoadingSpinner(); + } else { + console.log('all coords sad was resolved first', allCoordinates); + } + }); + + PubSub.subscribe('Searches:sad-tweet-coordinates-loaded', (event) => { + eventCounter += 1; + allCoordinates = allCoordinates.concat(event.detail, allCoordinates); + if (eventCounter === 2){ + console.log('all coords sad', allCoordinates); + this.renderMap(allCoordinates); + this.removeLoadingSpinner(); + } else { + console.log('all coords happy was resolved first', allCoordinates); + } }); }; @@ -35,7 +64,7 @@ const json = { var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/light-v9', - center: [-20, 20], + center: [0, 45], zoom: 0.65 }); @@ -167,4 +196,11 @@ const json = { } +MapView.prototype.removeLoadingSpinner = function(){ + const loadingOverlay = document.querySelector('#loading-overlay'); + const loadingModal = document.querySelector('#loading-modal'); + loadingOverlay.classList.add('hidden'); + loadingModal.classList.add('hidden'); +} + module.exports = MapView; diff --git a/client/src/views/totals_view.js b/client/src/views/totals_view.js index 946d8f1..f591514 100644 --- a/client/src/views/totals_view.js +++ b/client/src/views/totals_view.js @@ -1,31 +1,62 @@ const PubSub = require('../helpers/pub_sub.js'); -const TotalsView = function (formElement) { - this.formElement = formElement; +const TotalsView = function (positiveGrid, negativeGrid) { + this.positiveGrid = positiveGrid; + this.negativeGrid = negativeGrid; + this.happyTweets = 0; + this.sadTweets = 0; + this.sadRetweets = 0; + this.happyRetweets = 0; + this.happyFavourites = 0; + this.sadFavourites = 0; } TotalsView.prototype.bindEvents = function () { - PubSub.subscribe('Searches:totals-calulated', (event) => { - this.renderTotals(event.detail); + PubSub.subscribe('Searches:happy-totals-calulated', (event) => { + //this.happyTweets = event.detail.totalTweets; + this.happyRetweets = event.detail.retweets; + this.happyFavourites = event.detail.favourites; + + this.renderTotals() + this.positiveGrid.classList.remove('hidden'); + }) + PubSub.subscribe('Searches:sad-totals-calulated', (event) => { + //this.sadTweets = event.detail.totalTweets; + this.sadRetweets = event.detail.retweets; + this.sadFavourites = event.detail.favourites; + + this.renderTotals() + this.negativeGrid.classList.remove('hidden'); + }) } -TotalsView.prototype.renderTotals = function (totalData) { +TotalsView.prototype.renderTotals = function () { const firstContentElement = document.querySelector('#grid-item-5'); - firstContentElement.textContent = totalData.totalTweets; + firstContentElement.textContent = this.happyRetweets; const secondContentElement = document.querySelector('#grid-item-6'); - secondContentElement.textContent = totalData.favourites; + secondContentElement.textContent = this.sadRetweets; const thirdContentElement = document.querySelector('#grid-item-7'); - thirdContentElement.textContent = totalData.retweets; + thirdContentElement.textContent = this.happyFavourites; const fourthContentElement = document.querySelector('#grid-item-8'); - // fourthContentElement.textContent = totalData.XXXX; + fourthContentElement.textContent = this.sadFavourites; + //const sentiment = this.determineSentiment(); + //fourthContentElement.textContent = sentiment; +} +TotalsView.prototype.determineSentiment = function() { + if (this.sadRetweets + this.sadFavourites <= this.happyRetweets + this.happyFavourites){ + return ':)' + } + return ':(' } + + module.exports = TotalsView; \ No newline at end of file diff --git a/server/helpers/twitter.js b/server/helpers/twitter.js index 5b01eaf..7906ea3 100644 --- a/server/helpers/twitter.js +++ b/server/helpers/twitter.js @@ -24,7 +24,7 @@ Twitter.prototype.getAllSearchResultsFromLast7DaysForSearchTerm = function (sear return new Promise((resolve, reject) => { let nextResultsQuery = null; let currentPageNumber = 1; - const maxPages = 1; + const maxPages = 2; let allResults = []; this.getSinglePageOfResultsFromLast7Days(searchTerm, nextResultsQuery, currentPageNumber, maxPages, allResults, resolve, reject); @@ -39,7 +39,7 @@ Twitter.prototype.getSinglePageOfResultsFromLast7Days = function (searchTerm, ne // and the we're definately not on the last page // add the data so far to allResults const tweets = data.statuses.map(tweet => { - return { text: tweet.full_text, location: tweet.user.location, retweets: tweet.retweet_count, favourites: tweet.favorite_count } + return { text: tweet.full_text, location: tweet.user.location, retweets: tweet.retweet_count, favourites: tweet.favorite_count, id: tweet.id } }); tweets.forEach((tweet) => { @@ -47,11 +47,11 @@ Twitter.prototype.getSinglePageOfResultsFromLast7Days = function (searchTerm, ne }); // allResults.push(tweets); - console.log("next results = ", data.search_metadata.next_results); if (data.search_metadata.next_results && (currentPageNumber < maxPages)) { // set nextResults to the value from the metadata nextResultsQuery = data.search_metadata.next_results; let nextPageNumber = currentPageNumber + 1; + console.log('Fetched a page', currentPageNumber, searchTerm, nextResultsQuery); // call this method recursively with the new values from the first call this.getSinglePageOfResultsFromLast7Days(searchTerm, nextResultsQuery, nextPageNumber, maxPages, allResults, resolve, reject); // otherwise, exit now and resolve with the data