diff --git a/client/app/components/admin/PirateSearch.jsx b/client/app/components/admin/PirateSearch.jsx index c60e7c2..5ed6abb 100644 --- a/client/app/components/admin/PirateSearch.jsx +++ b/client/app/components/admin/PirateSearch.jsx @@ -44,10 +44,16 @@ class PirateSearch extends Component { fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/search?query='+query+'&type='+type, 'GET') // fetchJSON('http://localhost:31459/api/v1/pirate/search?query='+query+'&type='+type, 'GET') .then((response) => { - this.setState({ - torrentResponse: response.torrents, - loading: null, - }) + console.log('this is the first response: ', response) + if (response.success === true) { + this.setState({ + torrentResponse: response.torrents, + loading: null, + }) + } + else { + console.error(response.message) + } }) .catch((error) => { console.error(error); diff --git a/client/app/components/admin/TorrentTable.jsx b/client/app/components/admin/TorrentTable.jsx new file mode 100644 index 0000000..4cb56fd --- /dev/null +++ b/client/app/components/admin/TorrentTable.jsx @@ -0,0 +1,196 @@ +import React, { Component } from 'react'; + +import { fetchJSON } from '../http.jsx'; + +import torrentTableCSS from '../styles/adminTorrentTable.jsx'; + +class TorrentTable extends Component { + constructor() { + super(); + this.state = { + torrentResponse: [], + listElements: undefined, + showTable: false, + filterQuery: '', + sortValue: 'name', + sortDesc: true, + } + + this.UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + } + + componentWillReceiveProps(props) { + if (props.response !== undefined && props.response !== this.state.torrentResponse) { + console.log('not called', props) + this.setState({ + torrentResponse: props.response, + showTable: true, + }) + } else { + this.setState({ + showTable: false, + }) + } + } + + + // BORROWED FROM GITHUB user sindresorhus + // Link to repo: https://github.com/sindresorhus/pretty-bytes + convertSizeToHumanSize(num) { + if (!Number.isFinite(num)) { + throw new TypeError(`Expected a finite number, got ${typeof num}: ${num}`); + } + const neg = num < 0; + + if (neg) { + num = -num; + } + + if (num < 1) { + return (neg ? '-' : '') + num + ' B'; + } + + const exponent = Math.min(Math.floor(Math.log10(num) / 3), this.UNITS.length - 1); + const numStr = Number((num / Math.pow(1000, exponent)).toPrecision(3)); + const unit = this.UNITS[exponent]; + + return (neg ? '-' : '') + numStr + ' ' + unit; + } + + sendToDownload(magnet) { + const apiData = { + magnet: magnet, + } + + fetchJSON('https://apollo.kevinmidboe.com/api/v1/pirate/add', 'POST', apiData) + // fetchJSON('http://localhost:31459/api/v1/pirate/add', 'POST', apiData) + .then((response) => { + console.log('Response, addMagnet: ', response) + // TODO Display the feedback in a notification component (text, status) + }) + } + + // Updates the internal state of the query filter and updates the list to only + // display names matching the query. This is real-time filtering. + updateFilterQuery(event) { + const query = event.target.value; + + let filteredByQuery = this.props.response.map((item, index) => { + if (item.name.toLowerCase().indexOf(query.toLowerCase()) != -1) + return item + }) + + console.log(filteredByQuery) + + this.setState({ + torrentResponse: filteredByQuery, + filterQuery: query, + }); + } + + + sortTable(col) { + let direction = this.state.sortDesc; + if (col === this.state.sortValue) + direction = !direction; + else + direction = true + + let sortedItems = this.state.torrentResponse.sort((a,b) => { + // This is so we also can sort string that only contain numbers + let valueA = isNaN(a[col]) ? a[col] : parseInt(a[col]) + let valueB = isNaN(b[col]) ? b[col] : parseInt(b[col]) + + if (direction) + return valueAvalueB?-1:0; + else + return valueA>valueB? 1:valueA +
+ this.updateFilterQuery(event)} + value={this.state.filterQuery}/> + + + + + + + + +
+ + ) + } + + generateListElements() { + let listElements = this.state.torrentResponse.map((item, index) => { + if (item !== undefined) { + let title = item.name + let size = this.convertSizeToHumanSize(item.size) + + return ( + + { item.name } + { item.uploader } + { size } + { item.seed } + + + ) + } + }) + return listElements + } + + render() { + return ( +
+ { this.generateFilterSearch() } + + + + + + + + + + + + {this.generateListElements()} + +
this.sortTable('name') }> + Title + + this.sortTable('uploader') }> + Uploader + + this.sortTable('size') }> + Size + + this.sortTable('seed') }> + Seeds + + Magnet
+
+ ) + } +} + +export default TorrentTable; \ No newline at end of file diff --git a/client/app/components/styles/adminTorrentTable.jsx b/client/app/components/styles/adminTorrentTable.jsx index f6098bd..ce85a79 100644 --- a/client/app/components/styles/adminTorrentTable.jsx +++ b/client/app/components/styles/adminTorrentTable.jsx @@ -4,6 +4,17 @@ export default { marginRight: 'auto', marginLeft: 'auto', }, + tableHeader: { + }, + col: { + cursor: 'pointer', + borderBottom: '1px solid #e0e0e0', + paddingBottom: '0.5em', + textAlign: 'left', + }, + bodyCol: { + marginTop: '0.5em', + }, searchSidebar: { height: '4em', diff --git a/seasoned_api/src/pirate/pirateRepository.js b/seasoned_api/src/pirate/pirateRepository.js index f0680ac..ca2a4fb 100644 --- a/seasoned_api/src/pirate/pirateRepository.js +++ b/seasoned_api/src/pirate/pirateRepository.js @@ -1,14 +1,13 @@ const assert = require('assert'); var PythonShell = require('python-shell'); var async = require('async'); -var PythonShell = require('python-shell'); async function find(searchterm, callback) { var options = { pythonPath: '/usr/bin/python3', // pythonPath: '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3', - args: [searchterm, '-s', 'jackett', '--print'] + args: [searchterm, '-s', 'piratebay', '--print'] } PythonShell.run('../app/torrent_search/torrentSearch/search.py', options, callback); @@ -27,10 +26,16 @@ async function callPythonAddMagnet(magnet, callback) { } async function SearchPiratebay(query) { - return await new Promise((resolve) => { + return await new Promise((resolve, reject) => { return find(query, function(err, results) { - console.log('err', err, '. result', results); - resolve(JSON.parse(results, null, '\t')); + if (err) { + console.log('THERE WAS A FUCKING ERROR!') + reject(Error('There was a error when searching for torrents')) + } + if (results) { + console.log('result', results); + resolve(JSON.parse(results, null, '\t')); + } }) }) }