-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
178 lines (137 loc) · 5.37 KB
/
bot.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Imports
"""
from presidency.models import *
from lxml import html
import requests
import json
import datetime
from twython import Twython
import os
import time
import sys
"""
Set UTF-8 for everything.
"""
reload(sys)
sys.setdefaultencoding("utf-8")
# Establish Base URL.
base_url = os.environ.get('WHITE_HOUSE_URL') + ""
# Establish all pages to scrape.
pages = {
"/briefing-room/speeches-and-remarks": "Speeches and Remarks",
"/briefing-room/press-briefings": "Press Briefings",
"/briefing-room/statements-and-releases": "Statements and Releases",
"/briefing-room/presidential-actions/executive-orders": "Executive Orders",
"/briefing-room/presidential-actions/presidential-memoranda": "Presidential Memoranda",
"/briefing-room/presidential-actions/proclamations": "Proclamations",
"/briefing-room/presidential-actions/related-omb-material": "Related OMB Material",
# "/briefing-room/pending-legislation": "Pending Legislation",
# "/briefing-room/signed-legislation": "Signed Legislation",
# "/briefing-room/vetoed-legislation": "Vetoed Legislation",
"/briefing-room/statements-administration-policy": "Statements of Administration Policy"
}
# Scrape each page.
for key, value in pages.iteritems():
print("Scanning " + value)
# Make request and transform into tree.
page_url = base_url + key
response = requests.get(page_url)
tree = html.document_fromstring(response.text)
# Deterimine number of total pages.
pagecount = int(tree.xpath('//li[@class="pager-current"]')[0].text_content().split(' of ')[1]) if len(tree.xpath('//li[@class="pager-current"]')) > 0 else 1
# Keep iterating through pages until you reach a page that has been fully scraped. Then stop.
for i in range(0, pagecount):
# Use ?page= parameter to scrape, starting with page 0.
response = requests.get(page_url)
print("PAGE URL: " + page_url)
tree = html.document_fromstring(response.text)
# Build the resulting dictionary objects for each document on that page.
objects = [{
"document_date": x.xpath('div[contains(@class, "views-field-created")]')[0].text_content().strip() if len(x.xpath('div[contains(@class, "views-field-created")]')) > 0 else x.xpath('div')[0].text_content().split(' on ')[1],
"title": x.xpath('div[contains(@class, "views-field-title")]')[0].text_content().strip(),
"uri": x.xpath('div[contains(@class, "views-field-title")]')[0].xpath('h3')[0].xpath('a')[0].attrib['href'].strip(),
"category_slug": key,
"category_name": value,
"full_url": os.environ.get('WHITE_HOUSE_URL') + x.xpath('div[contains(@class, "views-field-title")]')[0].xpath('h3')[0].xpath('a')[0].attrib['href'].strip()
} for x in tree.xpath('//div[contains(@class, "views-row")]')]
# Add url's to object.
for i in range(0, len(objects)):
url = requests.post('https://www.googleapis.com/urlshortener/v1/url?key=' + os.environ.get('GOOGLE_URL_SHORTENER_API_KEY'), json={"longUrl": os.environ.get('WHITE_HOUSE_URL') + objects[i]['uri']})
if url.status_code == 200:
objects[i]['short_url'] = url.json()['id']
else:
objects[i]['short_url'] = objects[i]['short_url']
# Create database objects for all of these.
records = [WhiteHouse(x['title'], x['uri'], x['category_slug'], x['category_name'], x['document_date'], x['full_url'], x['short_url']) for x in objects]
# Track number of records successfully added. Those not added will be duplicates.
record_counter = 0
# Iterate through records.
for x in records:
# Attempt to persist.
try:
db.session.add(x)
db.session.commit()
record_counter = record_counter + 1
print("Added " + x.title + " successfully.")
# Fallback,
except Exception as e:
# Flush old commit that did not persist.
db.session.rollback()
# Try to save an error message.
"""
try:
db.session.add(Error(str(e)))
db.session.commit()
except:
db.session.rollback()
"""
print("Failed to add " + x.title + " successfully: " + str(e))
# If 0 records were added to the database, everything henceforth is old in this topic.
# Break, go to next slug.
pager = tree.xpath('//li[contains(@class, "pager-next")]')
try:
print(pager[0].xpath('a')[0].attrib['href'])
page_url = base_url + pager[0].xpath('a')[0].attrib['href']
except:
pass
# Retrieve all documents in descending order.
documents = WhiteHouse.query.filter_by(is_tweeted=False).order_by(WhiteHouse.document_date.asc())
print("New documents detected: %d" % (documents.count()))
# Set up Twitter bot.
twitter = Twython(
os.environ.get('TWITTER_CONSUMER_KEY'),
os.environ.get('TWITTER_CONSUMER_SECRET'),
os.environ.get('TWITTER_ACCESS_TOKEN'),
os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
)
# Go through all relevant documents and tweet them out.
for document in documents:
try:
tweet = document.title[0 : 113] + ("..." if len(document.title) > 113 else "") + " " + document.short_url
if os.environ.get('TWEET_ENV') == "TRUE":
try:
twitter.update_status( status=(tweet) )
document.is_tweeted = True
except Exception as e:
"""
db.session.add(Error(str(e)))
db.session.commit()
"""
continue
document.tweet = tweet
print("Tweeted: " + document.tweet)
db.session.add(document)
db.session.commit()
except Exception as e:
"""
try:
db.session.add(Error(str(e)))
db.session.commit()
except:
db.session.rollback()
"""
pass
# Time Delay
if os.environ.get('TWEET_ENV') == "TRUE":
time.sleep(10)