/
fbcontacts.py
263 lines (220 loc) · 11.1 KB
/
fbcontacts.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
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
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Project: fbcontacts
# Language: Python
#
# License: GNU Public License
# This file is part of the project.
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# <http://www.gnu.org/licenses/>
#
# Author: Albert De La Fuente (www.albertdelafuente.com)
# E-Mail: http://www.google.com/recaptcha/mailhide/d?k=01eb_9W_IYJ4Pm_Y9ALRIPug==&c=L15IEH_kstH8WRWfqnRyeW4IDQuZPzNDRB0KCzMTbHQ=
#
# Description: Creates a CSV file with facebook contacts data (name and email)
# from yahoo TrueSwitch
#
# Limitations: I've done this in about an hour, so don't expect too much!
# - Unicode characters are not correctly handled, patches are welcome!
# Database tables used: None
# Thread Safe: No
# Extendable: No
# Platform Dependencies: Linux (openSUSE used)
# Compiler Options:
"""
Creates a CSV file with facebook contacts data (name and email) from yahoo TrueSwitch
PROCEDURE:
* Requirement: You need a yahoo & hotmail mail account, create them if needed.
AT HOTMAIL:
* At hotmail go to people (up left corner)
* Click on the Facebook icon
* Click on okay, to authorize facebook to share contacts data with hotmail
AT YAHOO:
* At yahoo mail go to contacts
* Click on Import contacts
* Click on the hotmail icon
* Put your credentials and check "I give Yahoo! permission..."
* The "Step 2" window will show you a list of your contacts. Save the page with save page as
* It will create a /transfercontacts_files folder with an index.html file within
This is going to be the program input!
Command Line Usage:
python fbcontacts.py ./transfercontacts_files/index.html
Author: Albert De La Fuente (www.albertdelafuente.com)
E-Mail: http://www.google.com/recaptcha/mailhide/d?k=01eb_9W_IYJ4Pm_Y9ALRIPug==&c=L15IEH_kstH8WRWfqnRyeW4IDQuZPzNDRB0KCzMTbHQ=
Why I've done this: I've done this because I believe that I own my fb account data,
and I should be able to export it if I want.
"""
#import argparse
import codecs, sys, mmap, re, csv
#import os
from vlog import vlogger
VERB_NON = 0
VERB_MIN = 1
VERB_MED = 2
VERB_MAX = 3
def split_on_caps(str):
rs = re.findall('[A-Z][^A-Z]*',str)
fs = ""
for word in rs:
fs += " "+word
return fs
def conv(s):
if isinstance(s, unicode):
s = s.encode('iso-8859-1')
return s.decode('string-escape').decode('utf-8')
class fbcontactdata():
def __init__(self, fullname, first, middle, last, email):
# Public
self.fullname = fullname
self.first = first
self.middle = middle
self.last = last
self.email = email
class fbcontactsparser():
def __init__(self, filename):
# Public
self.filename = filename
self.outfile = 'out.csv'
self.clist = []
# Init vlogger
self.__verbosity = VERB_MAX
self.vlog = vlogger(self.__verbosity, sys.stdout)
#self.vlog = self.__log()
# Init mmap
self.__file = codecs.open(filename, encoding='utf-8', mode='r') # open(filename, 'r')
self.vlog(VERB_MIN, "opening file: %s" % filename)
self.__f = mmap.mmap(self.__file.fileno(), 0, access=mmap.ACCESS_READ)
self.__f.seek(0) # rewind
pass
def parse(self):
"""
Fills the funlist list with all the parsed functionalities based on the index.
"""
self.vlog(VERB_MED, "-> %s" % __name__)
self.__f.seek(0)
self.__f.readline()
loc = self.__f.tell()
endloc = self.__f.size() - 1
#self.vlog(VERB_MAX, "beginloc = %d" % beginloc)
#self.vlog(VERB_MAX, "endloc = %d" % endloc)
self.clist = []
count = 0
biggestline = ''
while (loc < endloc):
line = self.__f.readline()
self.vlog(VERB_MAX, "reading line '%s' bytes = %d" % (line, loc))
if len(line) > len(biggestline):
biggestline = line
loc = self.__f.tell()
#self.vlog(VERB_MED, "<- getfunlist()")
#self.vlog(VERB_MED, "<- %s" % __name__)
#self.vlog(VERB_MAX, "biggestline = %s" % (biggestline))
if self.outfile is not '':
fh = open(self.outfile, 'wb')
#fh = codecs.open(self.outfile, "wb", "utf-8")
#fh = codecs.open(self.outfile, 'wb', encoding="utf-8")
else:
fh = sys.stdout
# csvhdlr = csv.writer(fh, quotechar='"', quoting=csv.QUOTE_MINIMAL)
#csvhdlr.writerow("Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,E-mail 1 - Type,E-mail 1 - Value".split(','))
#clist = biggestline.split('"}]},')
clist = biggestline.split(b'</label></div></li>')
for contact in clist:
fullname = ''
first = ''
middle = ''
last = ''
email = ''
# RECORD
#<li><div class="clearfix"><label><input checked="checked" name="contact_553" value="{"name":"Vinicius Bufoni ","email":"vinicius@bufoni.com.br","fields":[{"type":"name","first":"Vinicius","middle":"Bufoni"},{"type":"email","data":"vinicius@bufoni.com.br"}],"category":"Msn"}" type="checkbox">Vinicius Bufoni <b>vinicius@bufoni.com.br</b></label></div></li>
m = re.compile(b'<li><div class="clearfix"><label><input checked="checked" name="contact_(.*?)" value=').search(contact)
if m:
cid = m.group(1)
#"name":"\\u00c1tila Ocanha "
m = re.compile(b'"name":"(.*?)"').search(contact)
if m:
fullname = m.group(1)
m = re.compile(b'"email":"(.*?)"').search(contact)
if m:
email = m.group(1)
m = re.compile(b'"name","first":"(.*?)"').search(contact)
#m = re.compile(r'"first":"(.*?)"').search(contact)
if m:
first = m.group(1)
m = re.compile(b'"last":"(.*?)"').search(contact)
#m = re.compile(r'"last":"(.*?)"').search(contact)
if m:
last = m.group(1)
#if last is not '':
m = re.compile(b'"middle":"(.*?)"').search(contact)
#m = re.compile(r'"middle":"(.*?)"').search(contact)
#else:
#m = re.compile(r'"middle":"(.*?)"},').search(contact)
if m:
middle = m.group(1)
#print(fullname)
fullname = re.sub('\s+', ' ', split_on_caps(fullname)).strip()
#print(fullname)
#print(conv(fullname))
fullname = fullname.decode('unicode_escape')
#first = first.decode('unicode_escape')
#middle = middle.decode('unicode_escape')
#newlast = last.decode('unicode_escape')
#print(fullname)
#fullname = unicode(fullname, "iso-8859-1")
#fullname = fullname.decode('latin9').encode('utf8')
#fullname = fullname.decode('Latin-1').encode('utf8')
#print(fullname)
contactobject = fbcontactdata(fullname, first, middle, last, email)
self.clist.append(contactobject)
#self.vlog(VERB_MAX, "--- %s" % (cid))
#self.vlog(VERB_MAX, "raw data = %s" % (contact))
print(fullname)
#self.vlog(VERB_MAX, "fullname = %s" % (fullname))
#self.vlog(VERB_MAX, "first = %s" % (first))
#self.vlog(VERB_MAX, "middle = %s" % (middle))
#self.vlog(VERB_MAX, "last = %s" % (newlast))
#self.vlog(VERB_MAX, "email = %s" % (email))
#row = fullname + ',,,,,,,,,,,,,,,,,,,,,,,,,,fbcontacts ::: * My Contacts,* Home,' + email
#csvhdlr.writerow(row.split(','))
#,Yasmin,L\u00f3pez,L\u00f3pez,,,,,,,,,,,,,,,,,,,,,,,fbcontacts ::: * My Contacts,* Home,yasminlq@msn.com
#print('first = 'first + "|" + middle + "|" + last + "|" + email)
#print(contact)
#print clist
#self.vlog(VERB_MAX, "result = %s" % (self.clist))
#self.funlist += [result]
#return self.funlist
def writecsv(self):
fh = sys.stdout
if self.outfile is not '':
fh = open(self.outfile, 'wb')
#fh = codecs.open(self.outfile, "wb", "utf-8")
#fh = codecs.open(self.outfile, 'wb', encoding="unicode_escape")
csvhdlr = csv.writer(fh, quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvhdlr.writerow("Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,E-mail 1 - Type,E-mail 1 - Value".split(','))
for contact in self.clist:
#csvhdlr.writerow(dict((vname, vtype, vnotes, vstereotype, vauthor, valias, vgenfile.encode('utf-8')) for vname, vtype, vnotes, vstereotype, vauthor, valias, vgenfile in row.iteritems()))
row = contact.fullname + b',,,,,,,,,,,,,,,,,,,,,,,,,,fbcontacts ::: * My Contacts,* Home,' + contact.email
row = row.encode('utf8')
values = row.split(',')
print(values[0])
csvhdlr.writerow(values)
def main(args):
#parser = fbcontactsparser(args.filename)
parser = fbcontactsparser(args[1])
parser.parse()
parser.writecsv()
if __name__ == '__main__':
#parser = argparse.ArgumentParser(description = __doc__) #"Creates a CSV file with facebook contacts data (name and email) from yahoo TrueSwitch")
#parser.add_argument("filename")
#args = parser.parse_args()
#main(args)
sys.exit(main(sys.argv))