-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_articles.py
125 lines (96 loc) · 3.63 KB
/
load_articles.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from bs4 import BeautifulSoup
from sets import Set
import ConfigParser, os
import os.path
import pickle
import urllib
import praw
import sys
really_post = True
config = ConfigParser.RawConfigParser()
config.read('boise.cfg')
reload(sys)
sys.setdefaultencoding('utf8')
def post_to_reddit(reddit_obj, title, summary, link, picture = '', style = 'self_post'):
target_sub = 'IdahoPolitics' ## change to 'test' if testing
#target_sub = 'test'
if style == 'self_post':
summary = summary + "\n\n" + '[>>Full Story](' + link + ')'
got_error = False
try:
if style == 'self_post':
reddit_obj.subreddit(target_sub).submit(title, summary)
else:
reddit_obj.subreddit(target_sub).submit(title, url=link)
except (RuntimeError, TypeError, NameError, praw.exceptions.APIException) as err :
print("Oops! didnt work: ", err)
got_error = err
return got_error
reddit = praw.Reddit(client_id=config.get('Main', 'client_id'), client_secret=config.get('Main', 'client_secret'), password=config.get('Main', 'password'), user_agent=config.get('Main', 'user_agent'), username=config.get('Main', 'username') )
previous_urls_file = 'prev_urls.p'
if os.path.isfile(previous_urls_file):
print "Found Pickle"
prev_article_set = pickle.load( open( previous_urls_file, "rb" ) )
else:
print "didnt find pickle"
prev_article_set = Set()
## Loop through Betsy Russels articles and post
html_doc = 'http://www.spokesman.com/blogs/boise/'
r = urllib.urlopen(html_doc).read()
soup = BeautifulSoup(r, 'html.parser')
articles = soup.find_all('article')
count = 0
for cur_article in articles:
count = count + 1
if count > 13:
break
cur_href = cur_article.find_all("a")[0]["href"]
cur_title = cur_article.find_all("a")[0].contents[0].encode(sys.stdout.encoding, errors='replace')
cur_summary = cur_article.find_all("p")[1].contents[0].encode(sys.stdout.encoding, errors='replace')
if cur_href.strip() != '' and cur_title.strip() != '':
cur_href = 'http://www.spokesman.com' + cur_href
print cur_title
if cur_href in prev_article_set:
print "already posted"
else:
print "new to me"
if really_post:
got_error = post_to_reddit(reddit, cur_title + ' [BR]', cur_summary, cur_href)
if got_error == False:
prev_article_set.add(cur_href)
pickle.dump( prev_article_set, open(previous_urls_file, "wb" ) )
else:
print 'Post Error:' + got_error
print "=================="
print "Looping statesman politics "
## Loop through Idaho statesman local politics and post
html_doc = 'http://www.idahostatesman.com/news/politics-government/state-politics/'
r = urllib.urlopen(html_doc).read()
soup = BeautifulSoup(r, 'html.parser')
articles = soup.find_all('article')
count = 0
for cur_article in articles:
if count > 3:
break
cur_href = cur_article.find_all("a")[2]['href']
cur_title = cur_article.find_all("a")[2].contents[0].strip().encode(sys.stdout.encoding, errors='replace')
if len(cur_article.find_all("p")) >= 2:
cur_summary = cur_article.find_all("p")[1].contents[0].strip().encode(sys.stdout.encoding, errors='replace')
count = count + 1
else:
continue
if cur_href.strip() != '' and cur_title.strip() != '':
print cur_title
if cur_href in prev_article_set:
print "already posted"
else:
print "new to me"
if really_post:
got_error = post_to_reddit(reddit, cur_title + ' [IS]', cur_summary, cur_href)
if got_error == False:
prev_article_set.add(cur_href)
pickle.dump( prev_article_set, open(previous_urls_file, "wb" ) )
else:
print 'Post Error:' + got_error
print "=================="
pickle.dump( prev_article_set, open(previous_urls_file, "wb" ) )