Removed parts of old script

This commit is contained in:
Jesper Wrang 2018-01-31 07:41:49 +01:00
parent 13fd5a14ce
commit 98aaeaa647
13 changed files with 133 additions and 195 deletions

View file

@ -1,162 +1,44 @@
"use strict";
// Little display-bar in the upper right corner
var Status = (function(){
var loadingText = document.getElementById("status-text");
var statusImage = document.getElementById("status-image");
return {
loading: function(msg) {
statusImage.src = "/static/images/loading.gif";
loadingText.innerHTML = msg;
},
success: function(msg) {
statusImage.src = "/static/images/done.png";
loadingText.innerHTML = _.defaultTo(msg, "");
},
error: function(msg) {
statusImage.src = "/static/images/error.png";
loadingText.innerHTML = "<b>"+msg+"</b>";
console.error(msg);
}
};
})();
// Check browser support
var isSupported = self.fetch && self._ && self.Promise;
// If not supported show error
if(!isSupported) {
(function() {
var isMissing = [];
if(!self.fetch) isMissing.push("fetch");
if(!self.Promise) isMissing.push("promise");
if(!self._) isMissing.push("lodash");
// And a link with an error report to reddit... maybe not best way but its something
var url = "https://www.reddit.com/message/compose/?to=Jubbeart";
url += "&subject="+encodeURIComponent("Missing dependencies: " + isMissing.join(" "));
url += "&message="+encodeURIComponent("Comment (optional): ");
url += "%0A%0A"+encodeURIComponent("Browser details: "+navigator.userAgent);
Status.error("Error: Missing dependencies ("+isMissing+"). Contact <a href=\""+url+"\">/u/jubbeart</a>");
})();
}
var Reddit = (function() {
var subreddit = _.defaultTo(window.location.pathname.split("/")[2], 'all');
return {
subreddit: subreddit,
isAll: _.toLower(subreddit) === 'all',
threadID: window.location.pathname.split("/")[4],
permalink: window.location.pathname.split("/")[6]
};
})();
var Fetch2 = (function(){
return {
get: function(url, error){
return fetch(url)
.catch(function(error){
return Promise.reject(error);
})
.then(function(response){
if(response.ok) {
return response;
} else {
return Promise.reject(error);
}
});
},
json: function(response) {
return response.json();
},
handleError: function(error) {
if(_.includes(_.toLower(error), "error")) {
Status.error(error);
} else {
Status.error("Error: "+error);
}
}
};
})();
var ElasticSearch = (function() {
var elastic = "https://elastic.pushshift.io";
return {
thread: function(){},
comment: function(){},
threadURL: elastic+"/rs/submissions/_search?source=",
commentURL: elastic+"/rc//comments/_search?source="
};
})();
// HTML parsing
var HTML = (function (){
return {
parse: function(htmlString) {
var tmpDiv = document.createElement('div');
tmpDiv.innerHTML = htmlString;
return tmpDiv.childNodes.length === 0 ? "" : tmpDiv.childNodes[0].nodeValue;
},
main: document.getElementById("main")
};
})();
const HTML = (function () {
return {
parse(htmlString) {
const tmpDiv = document.createElement('div')
tmpDiv.innerHTML = htmlString
return tmpDiv.childNodes.length === 0 ? '' : tmpDiv.childNodes[0].nodeValue
},
main: document.getElementById('main'),
}
}())
// UTC time handling, very usefull when dealing with elasticsearch
var Time = (function(){
return {
utc: function() {
return Math.floor(_.now()/1000);
},
var Time = (function () {
return {
utc() {
return Math.floor(_.now() / 1000)
},
toUTC: function(timeString) {
var parts = timeString.split(/[a-zA-Z]+/);
var times = 1;
toUTC(timeString) {
const parts = timeString.split(/[a-zA-Z]+/)
let times = 1
// Number before the time (e.g. 2years or 12hour)
if(parts.length === 2 && parts[0] !== "") {
times = _.parseInt(parts[0]);
}
// Number before the time (e.g. 2years or 12hour)
if (parts.length === 2 && parts[0] !== '') {
times = _.parseInt(parts[0])
}
if(_.includes(timeString, "hour")) return times * 3600;
if(_.includes(timeString, "day")) return times * 86400;
if(_.includes(timeString, "week")) return times * 604800;
if(_.includes(timeString, "month")) return times * 2592000;
if(_.includes(timeString, "year")) return times * 31536000;
return Time.utc();
},
difference: function(timeString) {
return Time.utc() - Time.toUTC(timeString);
}
};
})();
// The get variables used in the url
var GetVars = (function(){
return {
get: function(key) {
var pairs = _.defaultTo(window.location.href.split("?")[1], "").split("&");
for(var i = 0, len = pairs.length; i < len; i++) {
if(pairs[i].split("=")[0] === key) {
return pairs[i].split("=")[1];
}
}
return undefined;
}
};
})();
if (_.includes(timeString, 'hour')) return times * 3600
if (_.includes(timeString, 'day')) return times * 86400
if (_.includes(timeString, 'week')) return times * 604800
if (_.includes(timeString, 'month')) return times * 2592000
if (_.includes(timeString, 'year')) return times * 31536000
return Time.utc()
},
difference(timeString) {
return Time.utc() - Time.toUTC(timeString)
},
}
}())

View file

@ -1,8 +1,8 @@
"use strict";
'use strict';
var app = (function(){
return {
loadPage: function() {
let app = (function () {
return {
loadPage() {
Status.loading("Loading subreddit...");
document.title = Reddit.isAll ? "all subreddits" : Reddit.subreddit;
HTML.main.innerHTML += _.templates.subredditInfo({subreddit: Reddit.subreddit, time: Vars.time});
@ -30,21 +30,7 @@ return {
Status.success();
})
.catch(Fetch2.handleError);
}
};
})();
},
}
}());
var Vars = (function(){
return {
postPerPage: 50,
time: _.defaultTo(GetVars.get("t"), Reddit.isAll ? "12hour" : "day"),
page: _.defaultTo(_.parseInt(GetVars.get("page")), 1),
reload: function(event) {
document.location.href = document.location.href.split("?")[0] + "?t=" + event.value;
}
};
})();
if(isSupported) {
app.loadPage();
}

View file

@ -1,16 +1,70 @@
<!--
_-`````-, ,- '- .
.' .- - | | - -. `.
/.' / `. \
:/ : _... ..._ `` :
:: : /._ .`:'_.._\. || :
:: `._ ./ ,` : \ . _.'' .
`:. / | -. \-. \\_ /
\:._ _/ .' .@) \@) ` `\ ,.'
_/,--' .- .\,-.`--`.
,'/'' (( \ ` )
/'/' \ `-' (
'/'' `._,-----'
''/' .,---'
''/' ;:
''/'' ''/
''/''/''
'/'/'
`;
-->
<!--
__ __
(_ |_ _ || _ _ _ | | _ _ |__). _ |_ |_
__)|_(_||||||(_|| ) |/\|(_|_) | \ |(_)| )|_
_/
-->
<!--
.88888888:.
88888888.88888.
.8888888888888888.
888888888888888888
88' _`88'_ `88888
88 88 88 88 88888
88_88_::_88_:88888
88:::,::,:::::8888
88`:::::::::'`8888
.88 `::::' 8:88.
8888 `8:888.
.8888' `888888.
.8888:.. .::. ...:'8888888:.
.8888.' :' `'::`88:88888
.8888 ' `.888:8888.
888:8 . 888:88888
.888:88 .: 888:88888:
8888888. :: 88:888888
`.::.888. :: .88888888
.::::::.888. :: :::`8888'.:.
::::::::::.888 ' .::::::::::::
::::::::::::.8 ' .:8::::::::::::.
.::::::::::::::. .:888:::::::::::::
:::::::::::::::88:.__..:88888:::::::::::'
`'.:::::::::::88888888888.88:::::::::'
`':::_:' -- '' -'-' `':_::::'`
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Removeddit</title>
<meta charset="UTF-8">
<meta type="description" content="Display removed and deleted comments from Reddit">
<meta type="author" content="Jesper Wrang">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="/images/favicon.ico" rel="shotcut icon">
<title>Removeddit</title>
<meta charset="UTF-8">
<meta type="description" content="Display removed and deleted comments from Reddit">
<meta type="author" content="Jesper Wrang">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="/images/favicon.ico" rel="shotcut icon">
</head>
<body>
<script>console.time('scripts loaded')</script>
<div id="app"></div>
<script>console.time('scripts loaded')</script>
<div id="app"></div>
</body>
</html>

View file

@ -5,21 +5,22 @@ import {
Route,
} from 'react-router-dom'
import Menu from 'components/Menu'
import Header from 'components/Header'
import { About, Thread, Subreddit } from 'pages'
export default props => (
<BrowserRouter basename={__dirname}>
<>
<Menu status={props.status} />
<React.Fragment>
<Header status={props.status} />
<div className='main'>
<Switch>
<Route exact path='/' component={About} />
<Route path='/about' component={About} />
<Route path='/r/:subreddit/comments/:threadID' component={Thread} />
<Route path='/r/:subreddit/comments/:threadID/:junk/:commentID' component={Thread} />
</Switch>
</div>
</>
</React.Fragment>
</BrowserRouter>
)

View file

@ -1,6 +1,6 @@
import React from 'react'
import { Link } from 'react-router-dom'
import StatusBox from 'components/StatusBox'
import StatusBox from 'containers/StatusBox'
export default () => (
<header>

View file

@ -1,7 +1,7 @@
import React from 'react'
export default (props) => {
let pageination = <></>
let pageination = <React.Fragment />
for (let i = props.start; i <= props.end; i++) {
if (props.currentPage === i) {
@ -11,13 +11,14 @@ export default (props) => {
}
}
return (
<div id="pagination">
<div id='pagination'>
Page:
{props.start > 1 &&
<>
<React.Fragment>
<a href={`${props.url + 1}`}>1</a>
<span> ...</span>
</>}
</React.Fragment>
}
{pageination}
</div>
)

View file

@ -1,11 +1,11 @@
import React from 'react'
export default (props) => (
<>
<React.Fragment>
<div id='comment-info'>
removed comments: {props.removedComments}/${props.totalComments}
({ ((100 * props.removedComments) / props.totalComments).toFixed(1) }%)
</div>
<div id='comment-sort'>sorted by: top</div>
</>
</React.Fragment>
)

View file

@ -16,11 +16,21 @@
// store.dispatch(setStatusLoading('hi'))
import { getCommentIDs, getThread, getComments } from 'api/pushshift'
import { getCommentIDs } from 'api/reddit'
import { getCommentIDs as getAllCommentIDs } from 'api/pushshift'
import { difference } from './utils/index'
Promise.all([
getAllCommentIDs('6z1hch'),
getCommentIDs('TwoXChromosomes', '6z1hch'),
])
.then(resp => console.log(resp[1].length, difference(resp[0], resp[1])))
// getCommentIDs('6z1hch')
// getThread('6z1hch').then(console.log)
getComments(['dmt88s6', 'dmtqxj8', 'dmt1euz']).then(console.log)
// getComments(['dmt88s6', 'dmtqxj8', 'dmt1euz']).then(console.log)
// .then(ids => console.log(unique(ids)))

View file

@ -8,8 +8,12 @@ export const flatten = arr => arr.reduce(
[]
)
// Same as: array => Set => array
export const unique = arr => arr.filter((value, index) => arr.indexOf(value) === index)
// Everything in arr1 that is not in arr2
export const difference = (arr1, arr2) => arr1.filter(x => !arr2.includes(x))
// Take on big array and split it into an array of chunks with correct size
export const chunk = (arr, size) => {
const chunks = []