Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11', '3.12']
python-version: ['3.12']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ sudo apt install -y python3.9-distutils
pip install -r requirements.txt
pip install -r questions/inference_server/model-requirements.txt
pip install -r dev-requirements.txt
pip install -r requirements-test.txt
```

Using cuda is important to speed up inference.
Expand Down
20 changes: 9 additions & 11 deletions gameon/facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

"""

import cgi
import time
import urllib
import urllib2
import httplib
Expand Down Expand Up @@ -218,7 +216,7 @@ def put_photo(self, image, message=None, album_id=None, **kwargs):
data = urllib2.urlopen(req).read()
#For Python 3 use this:
#except urllib2.HTTPError as e:
except urllib2.HTTPError, e:
except urllib2.HTTPError as e:
data = e.read() # Facebook sends OAuth errors as 400, and urllib2
# throws an exception, we want a GraphAPIError
try:
Expand Down Expand Up @@ -263,7 +261,7 @@ def _encode_multipart_form(self, fields):
else:
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
if isinstance(value, unicode):
if isinstance(value, str):
logging.debug("Convert to ascii")
value = value.encode('ascii')
L.append(value)
Expand Down Expand Up @@ -293,7 +291,7 @@ def request(self, path, args=None, post_args=None):
file = urllib2.urlopen("https://graph.facebook.com/" + path + "?" +
urllib.urlencode(args),
post_data, timeout=self.timeout)
except urllib2.HTTPError, e:
except urllib2.HTTPError as e:
response = _parse_json(e.read())
raise GraphAPIError(response)
except TypeError:
Expand Down Expand Up @@ -340,7 +338,7 @@ def fql(self, query, args=None, post_args=None):
use the multiquery method
else use single query
"""
if not isinstance(query, basestring):
if not isinstance(query, str):
args["queries"] = query
fql_method = 'fql.multiquery'
else:
Expand All @@ -367,7 +365,7 @@ def fql(self, query, args=None, post_args=None):
#Return a list if success, return a dictionary if failed
if type(response) is dict and "error_code" in response:
raise GraphAPIError(response)
except Exception, e:
except Exception as e:
raise e
finally:
file.close()
Expand Down Expand Up @@ -408,21 +406,21 @@ def __init__(self, result):
self.result = result
try:
self.type = result["error_code"]
except:
except Exception:
self.type = ""

# OAuth 2.0 Draft 10
try:
self.message = result["error_description"]
except:
except Exception:
# OAuth 2.0 Draft 00
try:
self.message = result["error"]["message"]
except:
except Exception:
# REST server style
try:
self.message = result["error_msg"]
except:
except Exception:
self.message = result

Exception.__init__(self, self.message)
Expand Down
3 changes: 2 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pytest==7.3.1
pytest-cov==4.1.0
ruff
numpy
ruff==0.11.10
httpx
colorama
39 changes: 39 additions & 0 deletions tests/unit/test_facebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import base64
import hashlib
import hmac
import json

import sys
import urllib.request as urllib_request
import http.client as http_client
import urllib.parse as urllib_parse
import cgi

sys.modules.setdefault("urllib2", urllib_request)
sys.modules.setdefault("httplib", http_client)
sys.modules.setdefault("urlparse", urllib_parse)
cgi.parse_qs = urllib_parse.parse_qs

from gameon.facebook import parse_signed_request # noqa: E402


def make_signed_request(data: dict, secret: str) -> str:
payload = base64.urlsafe_b64encode(json.dumps(data).encode()).rstrip(b"=").decode()
sig = hmac.new(secret.encode('ascii'), msg=payload.encode('ascii'), digestmod=hashlib.sha256).digest()
encoded_sig = base64.urlsafe_b64encode(sig).rstrip(b"=").decode()
return f"{encoded_sig}.{payload}"


def test_parse_signed_request_valid():
secret = "secret"
data = {"algorithm": "HMAC-SHA256", "user_id": "123"}
sr = make_signed_request(data, secret)
result = parse_signed_request(sr, secret)
assert result == data


def test_parse_signed_request_invalid_signature():
secret = "secret"
data = {"algorithm": "HMAC-SHA256", "user_id": "123"}
sr = make_signed_request(data, secret)
assert parse_signed_request(sr, "wrong") is False
Loading