Jakiś czas temu w ramach powrotu do programowania popełniłem prosty kodzik który obsługuje graph api facebooka.
Proszę jednak traktować to jako ciekawostke/przykład implementacji z racji tego iż trochę poszperałem i ogólnie zajęło to trochę czasu, może ułatwi to niektórym ogólne rozpoczęcie.
Poza tym kod pozwala zalogować się na stronę www ( nie jestem w stanie powiedzieć czy dowolną)
Kod: Zaznacz cały
# Author : gridlok
# simple python grap api handler
# gridlok.mail[at]gmail.com
import re
import requests
import facebook
import json
#
# LOG WEBSITE CLASS
#
class Logging_into_website:
def __init__(self,headers,login_data,url,message_url,session):
self.headers = headers
self.login_data = login_data
self.url = url
self.message_url = message_url
self.session = session
def login(self):
r = s.get(self.url)
cok = __r.cookies
self.session.post(self.url, data=self.login_data, headers=self.headers, cookies=cok)
#
# LOG API CLASS
#
class Bot_page:
def __init__(self,access_token):
self.access_token = access_token
def __del__(self):
self.access_token = None
self.session = None
def get_messages(self,url,session):
self.__url = url
self.__session = session
try:
r = self.__session.get(self.__url + 'access_token=' + self.access_token)
req = r.json()
print(req['conversations']['data'][0]['messages']['data'][0]['message'])
except Exception as e:
print('ERROR in get_messages() : ',str(e))
return str(e)
def post_wall_message(self,url,message,session):
self.__url = url
self.__message = message
self.__session = session
self.__message['access_token'] = self.access_token
try:
r = self.__session.post(self.__url,data=self.__message)
print(r.raise_for_status())
return r
except Exception as e:
print('ERROR in post_wall_message() : ', str(e))
return str(e)
def get_posts_from_feed(self,url,session):
self.__url = url
self.__session = session
try:
r = self.__session.get(self.__url + 'access_token=' + self.access_token)
req = json.loads(r.text)
print(req[u'feed'][u'data'][0][u'message'])
return req
except Exception as e:
print('ERROR in get_messages() : ', str(e))
return str(e)
def parse_post_from_feed(self,req):
self.req = req
__x = (len(self.req[u'feed'][u'data'][0][u'message']))
for i in range(__x):
print(self.req[u'feed'][u'data'][i][u'message'])
cfg = {
"page_id" : "PAGE_ID_HERE",
"access_token" : "ACCESS_TOKEN_HERE"
}
post_message_payload = {}
post_message_payload['message'] = 'ss34'
post_message_payload['link'] = 'http://example.com'
get_urls = {'url1' : 'https://graph.facebook.com/me?&',
'url2' : 'https://graph.facebook.com/me?fields=conversations{messages}&',
'url3' : 'https://graph.facebook.com/me?fields=feed{message}&',
'url4' : 'https://graph.facebook.com/me/feed?'}
bot = Bot_page(cfg['access_token'])
with requests.Session() as s:
try:
bot.get_messages(get_urls['url2'],s) #pobiera wiadomosc z chatu fanpage (nie mozliwe w przypadku uzytkownika )
#req = bot.get_posts_from_feed(get_urls['url3'],s) #pobiera ostatni post z tablicy
#bot.parse_post_from_feed(req) # to jest mocno opocjonalne, nie pamietam nawet czy działa ta funkcja
#bot.post_wall_message(get_urls['url4'],post_message_payload,s) #wstawia post na tablice fanpage
except Exception as e:
print(str(e))
del bot