-
Notifications
You must be signed in to change notification settings - Fork 6
/
poster.py
84 lines (67 loc) · 2.24 KB
/
poster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import praw
import json
import urllib
import settingslocal
REDDIT_USERNAME = ''
REDDIT_PASSWORD = ''
try:
from settingslocal import *
except ImportError:
pass
def main():
print 'starting'
#Load an RSS feed of the Hacker News homepage.
url = "http://api.ihackernews.com/page"
try:
result = json.load(urllib.urlopen(url))
except Exception, e:
return
items = result['items'][:-1]
#Log in to Reddit
reddit = praw.Reddit(user_agent='HackerNews bot by /u/mpdavis')
reddit.login(REDDIT_USERNAME, REDDIT_PASSWORD)
link_submitted = False
for link in items:
if link_submitted:
return
try:
#Check to make sure the post is a link and not a post to another HN page.
if not 'item?id=' in link['url'] and not '/comments/' in link['url']:
submission = list(reddit.get_info(url=str(link['url'])))
if not submission:
subreddit = get_subreddit(str(link['title']))
print "Submitting link to %s: %s" % (subreddit, link['url'])
resp = reddit.submit(subreddit, str(link['title']), url=str(link['url']))
link_submitted = True
except Exception, e:
print e
pass
def get_subreddit(original_title):
title = original_title.lower()
apple = ['osx', 'apple', 'macintosh', 'steve jobs', 'woz']
python = ['python', 'pycon', 'guido van rossum']
webdev = ['.js', 'javascript', 'jquery']
linux = ['linux', 'debian', 'redhat', 'linus', 'torvalds']
programming = ['c++', 'programm', '.js', 'javascript', 'jquery', 'ruby']
gaming = ['playstation', 'xbox', 'wii', 'nintendo']
for word in apple:
if word in title:
return 'apple'
for word in python:
if word in title:
return 'python'
for word in webdev:
if word in title:
return 'webdev'
for word in linux:
if word in title:
return 'linux'
for word in programming:
if word in title:
return 'programming'
for word in gaming:
if word in title:
return 'gaming'
return 'technology'
if __name__ == "__main__":
main()