Tim Swast avatar Tim Swast committed 43b1fa5

Added simple Facebook posting.

Comments (0)

Files changed (2)

+"""Holds player information collected from Facebook.
+"""
+
+import sqlite3
+import httplib
+import urllib
+import FacePy
+
+_sqlconn = sqlite3.connect("highscore.db")
+_c = _sqlconn.cursor()
+try:
+    _c.execute("""create table highscore
+        (fid integer,
+        songid integer,
+        score integer,
+        primary key (fid, songid))""")
+except sqlite3.OperationalError:
+    pass # highscore table already exists
+
+
+class FBPlayer(object):
+    def __init__(self, fid=None, access_token=None):
+        self.fid = fid
+        self.access_token = access_token
+
+    def submit_highscore(self, songid, score):
+        if self.fid is None:
+            return
+        _c.execute("""select score from highscore
+                where fid=? and songid=?""", (self.fid, songid))
+        oldscore = _c.fetchone()
+
+        # score not present, so insert
+        if oldscore is None or score > oldscore[0]:
+            # post to facebook
+            if self.access_token is not None:
+                hconn = httplib.HTTPSConnection("graph.facebook.com")
+                hconn.request("POST", "/me/feed?%s" %
+                        urllib.urlencode({
+                            "access_token": self.access_token,
+                            "message": "I just got %i points on Kakofony." %
+                                score,
+                            "link": "http://apps.facebook.com/kakofony/"}))
+                print hconn.getresponse().read()
+                hconn.close()
+
+            # add to database
+            if oldscore is None:
+                _c.execute("""insert into highscore
+                        (fid, songid, score) values
+                        (?, ?, ?)""", (self.fid, songid, score))
+            else:
+                _c.execute("""update highscore
+                        set score=?
+                        where fid=? and songid=?""", (score, self.fid, songid))
+            _sqlconn.commit()
+
+
+player = FBPlayer(1606500361) # TODO: login later with FacePy
+def setup_fbplayer():
+    pass
+
+
+if __name__ == "__main__":
+    player.submit_highscore(0, 1000)
+    player.submit_highscore(0, 9000)
+    player.submit_highscore(0, 900)
+
+    f = FacePy.FacePy('164228856942567', 'f6cea7852ee1553cb54b6ada1d281899',
+            port=8080)
+    access_token = f.get_permission('publish_stream')
+    player = FBPlayer(1606500361, access_token) # TODO: login later with FacePy
+    player.submit_highscore(0, 1000000)
+

player.py

-"""Holds player information collected from Facebook.
-"""
-
-import sqlite3
-
-_sqlconn = sqlite3.connect("highscore.db")
-_c = _sqlconn.cursor()
-try:
-    _c.execute("""create table highscore
-        (fid integer,
-        songid integer,
-        score integer,
-        primary key (fid, songid))""")
-except sqlite3.OperationalError:
-    pass # highscore table already exists
-
-
-class Player(object):
-    def __init__(self, fid=None, access_token=None):
-        self.fid = fid
-
-    def submit_highscore(self, songid, score):
-        if self.fid is None:
-            return
-        _c.execute("""select score from highscore
-                where fid=? and songid=?""", (self.fid, songid))
-        oldscore = _c.fetchone()
-
-        # score not present, so insert
-        if oldscore is None:
-            _c.execute("""insert into highscore
-                    (fid, songid, score) values
-                    (?, ?, ?)""", (self.fid, songid, score))
-            _sqlconn.commit()
-        elif score > oldscore[0]:
-            _c.execute("""update highscore
-                    set score=?
-                    where fid=? and songid=?""", (score, self.fid, songid))
-            _sqlconn.commit()
-
-
-
-player = Player(1606500361) # TODO: login later with FacePy
-
-
-if __name__ == "__main__":
-    player.submit_highscore(0, 1000)
-    player.submit_highscore(0, 9000)
-    player.submit_highscore(0, 900)
-
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.