-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfbparser.py
More file actions
299 lines (224 loc) · 8.03 KB
/
fbparser.py
File metadata and controls
299 lines (224 loc) · 8.03 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# System imports
from bs4 import BeautifulSoup
import re
import sys
import csv
import urlparse
import time
import argparse
import ConfigParser
# Third-party imports
import mechanize
BASE_URL = "https://mbasic.facebook.com/"
# Function use to strip html tags
def strip_html(data):
# Extract the list from the first position of the array and convert it to
# string
cleandata = to_text(data)
# we strip the html tags from the string
p = re.compile(r'<.*?>')
return p.sub('', cleandata)
# Transform and concat all the data to string
def to_text(data):
data = to_string(data)
fullstring = ''
# Reverse the list to put the messages in the good chronological order
data = list(reversed(data))
for elem in data:
if len(data) > 1:
fullstring += str(elem) + "\n"
else:
fullstring += str(elem)
return fullstring
# Transform int to string
def to_string(data):
if isinstance(data, int):
return str(data)
else:
return data
# Logger function
def Logger(data):
global args
#[SAFEGUARD]
if args.log is False:
return
log = data + get_date_hour()
write_text_to_file("log", log)
def Debugger(data):
global args
#[SAFEGUARD]
if args.debug is False:
return
debug = data + get_date_hour()
print(debug)
def get_date_hour():
return " - " + time.strftime("%d-%m-%Y") + "-" + time.strftime("%H:%M:%S")
# We extract
def save_media(response):
# We get the html source code
soup = BeautifulSoup(html, "lxml")
cols = soup.findAll('div', attrs={"id": 'messageGroup'})
try:
messages = soup.select("#messageGroup > div:nth-of-type(2) > div")
Debugger("\n" + str((len(messages))) + " messages on this page \n")
for mess in reversed(messages):
# We get the global var counter
global counter
textmess = mess.findAll("span")
sender = mess.select("div:nth-of-type(1) > a > strong")
date = mess.findAll("abbr")
# print strip_html(date)+"\n"
# We put all the stripped data in a dic
data = {"nb": strip_html(counter), "text": strip_html(textmess),
"sender": strip_html(sender), "date": strip_html(date)}
global args
# To Refacto
if args.format == 'txt':
print strip_html(date) + "\n"
write_to_file(args.name, data)
if args.format == 'csv':
print strip_html(date) + "\n"
write_to_csv(args.name, data)
if args.format == 'console':
write_to_console(data)
# Increment and print it
counter += 1
#Logger("Total number of messages saved : " + str(counter))
Debugger("\n Total number of messages saved : " + str(counter));
older = soup.find('div', attrs={"id": 'see_older'}).find('a')
return older['href']
except AttributeError:
return None
def browse(url):
Debugger(url)
Logger(url)
html = get_page(url)
while html is None:
print "\n !!!! html is none, incrementing !!!! \n"
Logger("html is none, incrementing")
url = increment_start(url)
html = get_page(url)
return html
def get_page(url):
# We try to open the page
try:
resp = browser.open(url)
return resp.read()
except mechanize.HTTPError, e:
# handle http errors explicit by code
if int(e.code) == 500:
print "\n ///////////// ERROR 500 ///////////// \n"
return None
elif int(e.code) == 404:
print "\n \n"
Logger(url + " : ///////////// ERROR 404 : PAGE NOT FOUND /////////////")
raise Exception('This is the exception you expect to handle')
else:
raise e # if http error code is not 500, reraise the exception
return None
def send_message(url, message):
try:
# We try to open the page
browser.open(url)
# We select the second form
browser.select_form(nr=1)
# we write in the textearea with 'name = "body"'
browser['body'] = message
browser.submit()
except Exception as error:
print("Message couldn't be sent ...")
def increment_start(url):
parsed = urlparse.urlparse(url)
start = urlparse.parse_qs(parsed.query)['start']
start = int(start[0]) + 5
return BASE_URL + "/messages/read/?tid=" + convid + "&start=" + str(start)
def write_to_file(file, data):
f = open(file + '.txt', 'a')
f.write("\n")
f.write(data['sender'] + "\n")
f.write(data['text'] + "\n")
f.write(data['date'] + "\n")
f.close()
def write_to_csv(file, data):
with open(file + '.csv', 'a') as csvfile:
fieldnames = ['nb', 'sender', 'text', 'date']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# writer.writeheader()
writer.writerow({'nb': data['nb'], 'sender': data['sender'],
'text': data['text'], 'date': data['date']})
def write_text_to_file(file, text):
f = open(file + '.txt', 'a')
f.write("\n")
f.write(text + "\n")
f.close()
def write_to_console(data):
print data['text']
def fill_requirements():
# Read the configuration file
config = ConfigParser.ConfigParser()
config.readfp(open('conf.cfg'))
# print config.get('Ids', 'email')
# print config.get('Ids', 'password')
parser = argparse.ArgumentParser()
parser.add_argument(
"--verbosity", help="increase output verbosity", default="1")
# Email and passwords, default from conf.cfg, can be overwritten
parser.add_argument("--email", help="Overwrite email adress",
default=config.get('Ids', 'email'))
parser.add_argument("--password", help="Overwrite password",
default=config.get('Ids', 'password'))
# !! Required !!
parser.add_argument("--convid", help="Id of the conversation")
parser.add_argument(
"--name", help="Name you want to give to the output file")
parser.add_argument(
"--format", help="Choose default storing format (txt or csv)", default='txt')
# action='store_true' permet d'ajouter une option sans argument (donc
# "--debug"" suffit :))
parser.add_argument(
"--log", help="change to true to log parsing", action='store_true', default=False)
parser.add_argument(
"--debug", help="change to true to print debug messages", action='store_true', default=False)
parser.add_argument("--start", help="Start from a certain page", default=0)
global args
args = parser.parse_args()
args.name = args.name + get_date_hour()
#/////////////Main Logic/////////////
# Create the Browser Agent
browser = mechanize.Browser()
browser.set_handle_robots(False)
cookies = mechanize.CookieJar()
browser.set_cookiejar(cookies)
browser.addheaders = [
('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7')]
browser.set_handle_refresh(False)
fill_requirements()
global args
# Login
url = BASE_URL + '/login.php'
browser.open(url)
browser.select_form(nr=0) # This is login-password form -> nr = number = 0
browser.form['email'] = args.email
browser.form['pass'] = args.password
response = browser.submit()
# Initialization of the messages's counter
global counter
counter = 0
try:
if args.start != 0:
html = browse(BASE_URL + '/messages/read/?tid=' +
args.convid + "&start=" + args.start)
else:
html = browse(
BASE_URL + '/messages/read/?tid=' + args.convid)
except Exception as error:
print("Page's conversation couldn't be reached.")
exit()
see_older = save_media(html)
# sys.exit()
# While there is a 'see older' link in the page
while see_older:
html = browse(BASE_URL + see_older)
see_older = save_media(html)
write_text_to_file(args.name, str(counter) + " messages ")
print ("///////////////////////////// END ! /////////////////////////////////")