parent
38b5f2dbeb
commit
2edb3ceba7
7 changed files with 163 additions and 28 deletions
@ -0,0 +1,46 @@ |
|||||||
|
import logging |
||||||
|
logging.basicConfig( |
||||||
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
||||||
|
level=logging.DEBUG) |
||||||
|
|
||||||
|
import requests |
||||||
|
import time |
||||||
|
from bs4 import BeautifulSoup |
||||||
|
|
||||||
|
USER_AGENT = 'Twitterbot/1.0' |
||||||
|
|
||||||
|
def api(route): |
||||||
|
try: |
||||||
|
headers = {'User-Agent': USER_AGENT} |
||||||
|
r = requests.get(route, headers=headers, timeout=5) |
||||||
|
if r.status_code != 200: |
||||||
|
raise Exception('Bad response code ' + str(r.status_code)) |
||||||
|
return r.text |
||||||
|
except KeyboardInterrupt: |
||||||
|
raise |
||||||
|
except BaseException as e: |
||||||
|
logging.error('Problem hitting manual website: {}'.format(str(e))) |
||||||
|
return False |
||||||
|
|
||||||
|
def story(ref): |
||||||
|
html = api(ref) |
||||||
|
if not html: return False |
||||||
|
|
||||||
|
soup = BeautifulSoup(html, features='html.parser') |
||||||
|
|
||||||
|
s = {} |
||||||
|
s['author'] = 'manual submission' |
||||||
|
s['author_link'] = 'https://news.t0.vc' |
||||||
|
s['score'] = 0 |
||||||
|
s['date'] = int(time.time()) |
||||||
|
s['title'] = str(soup.title.string) |
||||||
|
s['link'] = ref |
||||||
|
s['url'] = ref |
||||||
|
s['comments'] = [] |
||||||
|
s['num_comments'] = 0 |
||||||
|
|
||||||
|
return s |
||||||
|
|
||||||
|
# scratchpad so I can quickly develop the parser |
||||||
|
if __name__ == '__main__': |
||||||
|
print(story('https://www.backblaze.com/blog/what-smart-stats-indicate-hard-drive-failures/')) |
@ -0,0 +1,54 @@ |
|||||||
|
import React, { Component } from 'react'; |
||||||
|
import { withRouter } from 'react-router-dom'; |
||||||
|
|
||||||
|
class Submit extends Component { |
||||||
|
constructor(props) { |
||||||
|
super(props); |
||||||
|
|
||||||
|
this.state = { |
||||||
|
progress: null, |
||||||
|
}; |
||||||
|
|
||||||
|
this.inputRef = React.createRef(); |
||||||
|
} |
||||||
|
|
||||||
|
submitArticle = (event) => { |
||||||
|
event.preventDefault(); |
||||||
|
const url = event.target[0].value; |
||||||
|
this.inputRef.current.blur(); |
||||||
|
|
||||||
|
this.setState({ progress: 'Submitting...' }); |
||||||
|
|
||||||
|
let data = new FormData(); |
||||||
|
data.append('url', url); |
||||||
|
|
||||||
|
fetch('/api/submit', { method: 'POST', body: data }) |
||||||
|
.then(res => res.json()) |
||||||
|
.then( |
||||||
|
(result) => { |
||||||
|
this.props.history.replace('/' + result.nid); |
||||||
|
}, |
||||||
|
(error) => { |
||||||
|
this.setState({ progress: 'Error' }); |
||||||
|
} |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
render() { |
||||||
|
const progress = this.state.progress; |
||||||
|
|
||||||
|
return ( |
||||||
|
<span className='search'> |
||||||
|
<form onSubmit={this.submitArticle}> |
||||||
|
<input |
||||||
|
placeholder='Submit Article' |
||||||
|
ref={this.inputRef} |
||||||
|
/> |
||||||
|
</form> |
||||||
|
{progress ? progress : ''} |
||||||
|
</span> |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export default withRouter(Submit); |
Loading…
Reference in new issue