-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit-news-str.py
56 lines (49 loc) · 1.7 KB
/
reddit-news-str.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
#!/usr/bin/env python3
import praw
import time
import os
import sys
sys.stdout.reconfigure(encoding='utf-8')
cwd = os.getcwd()
t = time.ctime()
titleFile = cwd + "\\titles.js"
def pull_news(cid, cs):
reddit = praw.Reddit(client_id=cid,
client_secret=cs,
user_agent="news ticker script",
redirect_uri="http://localhost:8080")
f = open(titleFile, "w")
f.write(time.ctime() + '~') # timestamp for start of ticker
f.close()
# top 50 posts for the day
for submission in reddit.subreddit('news').hot(limit=50):
title = submission.title
# remove troublesome characters
atitle = title.replace("\"", "\'")
btitle = atitle.replace("‘", "\'")
ctitle = btitle.replace("’", "\'")
dtitle = ctitle.replace("”", "\'")
etitle = dtitle.replace("“", "\'")
# what the hell is an emdash anyway
ftitle = etitle.replace("—", "-")
# print(ftitle)
f = open(titleFile, "a")
# add ~ between each title so we can replace it later (it is unlikely to be in a title)
f.write(ftitle + " ~ ")
f.close()
f = open(titleFile, "r")
x = f.read()
f.close()
c = x[:-2] # remove last two characters from string
f = open(titleFile, "w")
f.write(c)
f.close()
p = open(titleFile, "r")
q = p.read()
# print titles all at once so it's sent to stdout to be picked up by node_helper.js
print(q)
p.close()
sys.exit(0)
# allow script to be run with arguments by node_helper.js
if __name__ == "__main__":
pull_news(str(sys.argv[1]), str(sys.argv[2]))