Skip to content
Browse files

Refactored facebook scripts so that HTML files and JS data are refere…

…nced consistently with other parts of the book.
  • Loading branch information...
1 parent 57fe1cd commit cc7673f66804fd10b7779c7ec5f064ca63e5668b @ptwobrussell committed Jan 2, 2011
View
26 python_code/facebook__filter_rgraph_output_by_group.py
@@ -5,10 +5,15 @@
import json
import facebook
import webbrowser
+import shutil
from facebook__fql_query import FQL
from facebook__login import login
-# load the rgraph data
+HTML_TEMPLATE = '../web_code/jit/rgraph/rgraph.html'
+OUT = os.path.basename(HTML_TEMPLATE)
+
+# Reuses out/facebook.friends.json written out by
+# facebook__get_friends_rgraph.py
DATA = sys.argv[1]
rgraph = json.loads(open(DATA).read())
@@ -66,14 +71,21 @@
if not os.path.isdir('out'):
os.mkdir('out')
-filename = os.path.join('out', 'facebook.rgraph_groups.js')
-f = open(filename, 'w')
-f.write('var graph = %s;' % (json.dumps(filtered_rgraph, indent=4), ))
+# HTML_TEMPLATE references some dependencies that we need to
+# copy into out/
+
+shutil.rmtree('out/jit', ignore_errors=True)
+
+shutil.copytree('../web_code/jit',
+ 'out/jit')
+
+html = open(HTML_TEMPLATE).read() % (json.dumps(filtered_rgraph),)
+f = open(os.path.join(os.getcwd(), 'out', 'jit', 'rgraph', OUT), 'w')
+f.write(html)
f.close()
-print 'Data file written to: %s' % filename
+print 'Data file written to: %s' % f.name
# Open up the web page in your browser
-webbrowser.open('file://' + os.path.join(os.getcwd(), '..', 'web_code', 'jit',
- 'rgraph', 'rgraph.html'))
+webbrowser.open('file://' + f.name)
View
31 python_code/facebook__get_friends_rgraph.py
@@ -4,9 +4,13 @@
import sys
import json
import webbrowser
+import shutil
from facebook__fql_query import FQL
from facebook__login import login
+HTML_TEMPLATE = '../web_code/jit/rgraph/rgraph.html'
+OUT = os.path.basename(HTML_TEMPLATE)
+
try:
ACCESS_TOKEN = open('out/facebook.access_token').read()
except IOError, e:
@@ -109,21 +113,30 @@
if not os.path.isdir('out'):
os.mkdir('out')
-f = open(os.path.join('out', 'facebook.rgraph.js'), 'w')
-f.write('var graph = %s;' % (json.dumps(jit_output, indent=4), ))
+# HTML_TEMPLATE references some dependencies that we need to
+# copy into out/
+
+shutil.rmtree('out/jit', ignore_errors=True)
+
+shutil.copytree('../web_code/jit',
+ 'out/jit')
+
+html = open(HTML_TEMPLATE).read() % (json.dumps(jit_output),)
+f = open(os.path.join(os.getcwd(), 'out', 'jit', 'rgraph', OUT), 'w')
+f.write(html)
f.close()
-print 'JS data file written to: %s' % f.name
+print >> sys.stderr, 'Data file written to: %s' % f.name
# Write out another file that's standard JSON for additional analysis
+# and potential use later (by facebook_sunburst.py, for example)
-f = open(os.path.join('out', 'facebook.friends.json'), 'w')
-f.write(json.dumps(jit_output, indent=4))
-f.close()
+json_f = open(os.path.join('out', 'facebook.friends.json'), 'w')
+json_f.write(json.dumps(jit_output, indent=4))
+json_f.close()
-print 'JSON data file written to: %s' % f.name
+print 'Data file written to: %s' % json_f.name
# Open up the web page in your browser
-webbrowser.open('file://' + os.path.join(os.getcwd(), '..', 'web_code', 'jit',
- 'rgraph', 'rgraph.html'))
+webbrowser.open('file://' + f.name)
View
25 python_code/facebook__sunburst.py
@@ -4,8 +4,12 @@
import sys
import json
import webbrowser
+import shutil
from copy import deepcopy
+HTML_TEMPLATE = '../web_code/jit/sunburst/sunburst.html'
+OUT = os.path.basename(HTML_TEMPLATE)
+
# Reuses out/facebook.friends.json written out by
# facebook__get_friends_rgraph.py
DATA = sys.argv[1]
@@ -18,7 +22,7 @@
# The primary output to collect input
-out = {
+jit_output = {
'id': 'friends',
'name': 'friends',
'data': {'$type': 'none'},
@@ -60,21 +64,30 @@
go['children'].append(fo)
- out['children'].append(go)
+ jit_output['children'].append(go)
i += 1
# Emit the output expected by the JIT Sunburst
if not os.path.isdir('out'):
os.mkdir('out')
-f = open(os.path.join('out', 'facebook.sunburst.js'), 'w')
-f.write('var json = %s;' % (json.dumps(out, indent=4), ))
+# HTML_TEMPLATE references some dependencies that we need to
+# copy into out/
+
+shutil.rmtree('out/jit', ignore_errors=True)
+
+shutil.copytree('../web_code/jit',
+ 'out/jit')
+
+
+html = open(HTML_TEMPLATE).read() % (json.dumps(jit_output),)
+f = open(os.path.join(os.getcwd(), 'out', 'jit', 'sunburst', OUT), 'w')
+f.write(html)
f.close()
print 'Data file written to: %s' % f.name
# Open up the web page in your browser
-webbrowser.open('file://' + os.path.join(os.getcwd(), '..', 'web_code', 'jit',
- 'sunburst', 'sunburst.html'))
+webbrowser.open('file://' + f.name)
View
8 web_code/jit/rgraph/rgraph.html
@@ -12,11 +12,13 @@
<!-- JIT Library File -->
<script language="javascript" type="text/javascript" src="../jit.js"></script>
- <!-- Data - this points to where it would be written out by python code -->
- <script language="javascript" type="text/javascript" src="../../../python_code/out/facebook.rgraph.js"></script>
-
<!-- JIT code to process the data -->
<script language="javascript" type="text/javascript" src="rgraph.js"></script>
+
+ <!-- Data -->
+ <script language="javascript" type="text/javascript">
+ var graph = %s; // Substituted by Python
+ </script>
</head>
<body onload="init();">
View
10 web_code/jit/sunburst/sunburst.html
@@ -12,11 +12,15 @@
<!-- JIT Library File -->
<script language="javascript" type="text/javascript" src="../jit.js"></script>
-<!-- Data -->
-<script language="javascript" type="text/javascript" src="../../../python_code/out/facebook.sunburst.js"></script>
-
<!-- Sunburst -->
<script language="javascript" type="text/javascript" src="sunburst.js"></script>
+
+<!-- Data -->
+<script language="javascript" type="text/javascript">
+ var json = %s; // Substituted by Python
+</script>
+
+
</head>
<body onload="init();">

0 comments on commit cc7673f

Please sign in to comment.
Something went wrong with that request. Please try again.