-
Notifications
You must be signed in to change notification settings - Fork 0
/
tCrawler.py
207 lines (168 loc) · 7.55 KB
/
tCrawler.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from bs4 import BeautifulSoup
import urllib2, json
import os, errno
import sys
# Function to check whether the path exists or not
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
# List of TechCrunch URLs for crawling
URLs=[sys.argv[1]
#"http://techcrunch.com/2012/01/09/61-percent-disqus-comments-pseudonyms/"#,
#"http://techcrunch.com/2014/03/20/google-wants-everyone-to-stop-hating-on-glass/"
]
# List of TechCrunch URLs for getting the comments using Facebook Graph API
graphURLs=[
"https://graph.facebook.com/comments/?limit=500&ids="+sys.argv[1]# http://techcrunch.com/2012/01/09/61-percent-disqus-comments-pseudonyms/"#,
#"https://graph.facebook.com/comments/?limit=500&ids=http://techcrunch.com/2014/03/20/google-wants-everyone-to-stop-hating-on-glass/"
]
i = len(URLs)
j = 0
datasetPath = "./dataset"
print "Let the crawling begin!\nTotal no. of blogs = " + str(i)
while j < i:
netDirPath = os.path.join(datasetPath, "Blog " + str(j+1))
make_sure_path_exists(netDirPath)
# blogFile contains all the blogs with title, body etc.
netBlogPath = os.path.join(netDirPath, "blogDataset.json")
if os.path.exists(netBlogPath):
os.remove(netBlogPath)
blogFile = open(netBlogPath, 'w')
# commentsFile contains all the comments
netCommentsPath = os.path.join(netDirPath, "commentsDataset.json")
if os.path.exists(netCommentsPath):
os.remove(netCommentsPath)
commentsFile = open(netCommentsPath, 'w')
blogFile.write('{\n\t"blog" : {\n')
responseComments = urllib2.urlopen(graphURLs[j])
dataComments = json.loads(responseComments.read())
commentsFile.write((json.dumps(dataComments, indent = 2)))
responseBlog = urllib2.urlopen(URLs[j])
html = responseBlog.read()
soup = BeautifulSoup(html)
#blogFile.write(soup.h1.contents[0].encode('utf-8'))
blogFile.write('\n\t\t\t"url": "' + URLs[j] + '",\n' + '\t\t\t"title": "' + soup.h1.contents[0].encode('utf-8') + '",\n')
for links in soup.find_all('div'):
if links.get('class')!=None:
if links.get('class')[0]=="title-left":
dateAuthor = links.get_text()
dateAuthor = dateAuthor.strip().split(' ')
indexBy = dateAuthor.index("by")
k = 1
blogDateParts = ""
while k < indexBy:
blogDateParts += dateAuthor[k] + " "
k = k + 1
blogDate = blogDateParts
blogAuthorList = dateAuthor[indexBy + 2].split('\n')
blogAuthorLastName = ""
nextStr = 0
while nextStr < len(blogAuthorList):
if len(blogAuthorList[nextStr]) != 0 and "More" not in blogAuthorList[nextStr]:
blogAuthorLastName = blogAuthorList[nextStr]
nextStr = nextStr + 1
blogAuthor = dateAuthor[indexBy + 1]+ ' ' + blogAuthorLastName
blogFile.write('\t\t\t"date" : "' + blogDate + '",\n')
blogFile.write('\t\t\t"author" : "' + blogAuthor + '",\n')
blogBody = ""
for links in soup.find_all('p'):
blogBodyPart = links.get_text().strip()
if len(blogBodyPart) > 0 and blogBodyPart != "More" and not "Latest headlines delivered to you daily" in blogBodyPart:
#blogBody += blogBodyPart.encode('utf-8' + '\n')
blogBody += blogBodyPart.encode('ascii','ignore')
blogFile.write('\t\t\t"body" : "' + blogBody + '"\n')
blogFile.write('\t}\n}')
blogFile.close()
commentsFile.close()
j = j + 1
print "Blog " + str(j) + " done"
print "Cool. Done with Blogs and Comments!\nblogDataset.json => blog title, date, author, body\ncommentsDataset.json => comments from graph API"
print "\nOnto the replies part now..."
i=len(URLs)
j=0
path = "replies"
userSubscribers = "userSubscribers"
while j < i:
print "Getting replies for all the comments in Blog " + str(j + 1) + " " + URLs[j]
# Reading commentsDataset to get the replies for each comment
netDirPath = os.path.join(datasetPath, "Blog " + str(j+1))
netCommentsPath = os.path.join(netDirPath, "commentsDataset.json")
commentsFile = open(netCommentsPath,'r')
commentsJSON = json.load(commentsFile)
# In "replies" folder, a separate folder for each blog will be created
# which will contain replies of its respective comments with name as commentID
commentsIDs = []
userIDs = []
numberOfComments = len(commentsJSON[URLs[j]]["comments"]["data"])
for k in range(numberOfComments):
commentsIDs.append(commentsJSON[URLs[j]]["comments"]["data"][k]["id"].encode('ascii', 'ignore'))
if "from" in commentsJSON[URLs[j]]["comments"]["data"][k]:
#print commentsJSON[URLs[j]]["comments"]["data"][k]["from"]["id"].encode('ascii', 'ignore')
userIDs.append(commentsJSON[URLs[j]]["comments"]["data"][k]["from"]["id"].encode('ascii', 'ignore'))
netReplyPath = os.path.join(netDirPath, path)
if os.path.exists(netReplyPath):
for the_file in os.listdir(netReplyPath):
file_path = os.path.join(netReplyPath, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception, e:
print e
make_sure_path_exists(netReplyPath)
netUsersPath = os.path.join(netDirPath, userSubscribers)
if os.path.exists(netUsersPath):
for the_file in os.listdir(netUsersPath):
file_path = os.path.join(netUsersPath, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception, e:
print e
make_sure_path_exists(netUsersPath)
for commentsID in commentsIDs:
# Creating a replies file for each comment with name as commentID
repliesFileName = 'replyFor-' + commentsID + '.json'
netPathRepliesFile = os.path.join(netReplyPath, repliesFileName)
if os.path.exists(netPathRepliesFile):
os.remove(netPathRepliesFile)
repliesFile = open((netPathRepliesFile), 'w')
repliesURL = "https://graph.facebook.com/" + commentsID + "/comments/?limit=500"
responseReplies = urllib2.urlopen(repliesURL)
dataReplies = json.loads(responseReplies.read())
repliesFile.write((json.dumps(dataReplies, indent = 2)))
repliesFile.close()
repliesFile = open(netPathRepliesFile,'r')
repliesJSON = json.load(repliesFile)
numberOfReplies = len(repliesJSON["data"])
for k in range(numberOfReplies):
if "from" in repliesJSON["data"][k]:
#print repliesJSON["data"][k]["from"]["id"].encode('ascii', 'ignore')
userIDs.append(repliesJSON["data"][k]["from"]["id"].encode('ascii', 'ignore'))
repliesFile.close()
print "Getting subscribers for all the users in Blog " + str(j + 1) + " " + URLs[j]
for userID in userIDs:
# Creating a replies file for each comment with name as commentID
userFileName = userID + '.json'
netUserSubscribersFile = os.path.join(netUsersPath, userFileName)
if os.path.exists(netUserSubscribersFile):
os.remove(netUserSubscribersFile)
userSubscribersFile = open((netUserSubscribersFile), 'w')
#print "fetching subscribers for user " + userID
userSubscribersURL = "https://graph.facebook.com/" + userID + "/subscribers?access_token=CAAIq7L6i2AABALWkTAzc8FBdb99ZCf41airxD65xVTKts2wxpwYow81vYDMO3npWRbfo2fdJJisrWJ6NHLMc4eqhk8j2DUAmZAnJhClC7dwb5jwzWkvntZBonFsvTWc9YMmFY8zaNieZCrsAwlcX9DcNSjK5eeoXUZBr0OwTZBgMPFzQH2I6Hujxs8N2oKYw4ZD"
requestSubscribers = urllib2.Request(userSubscribersURL)
try:
responseSubscribers = urllib2.urlopen(requestSubscribers)
#print responseSubscribers
dataSubscribers = json.loads(responseSubscribers.read())
userSubscribersFile.write((json.dumps(dataSubscribers, indent = 2)))
except urllib2.URLError, e:
if e:
#print "404"
userSubscribersFile.write('{\n\t"data": [],\n\t"summary": {\n\t\t "total_count": 0\n\t }\n}')
userSubscribersFile.close()
commentsFile.close()
j = j + 1
print "Done! Check the 'replies' and 'userSubscribers' folder"