Skip to content
Snippets Groups Projects
Commit 292379f3 authored by Timur's avatar Timur Committed by GitHub
Browse files

Merge branch 'master' into stable

parents 4201e1ea a4cbc4c6
No related branches found
No related tags found
No related merge requests found
import facebook
from slacker import Slacker
from config import bot_secret_
from vk import Session, API
 
 
Loading
Loading
@@ -9,14 +9,16 @@ def auth_slack():
try:
api.auth.test()
except Exception as e:
print('not authed\n', e)
print('slack not authed\n', e)
 
return api
 
 
def auth_vk():
session = Session()
api = API(session)
try:
session = Session()
api = API(session)
except Exception as e:
print('vk not authed\n', e)
 
return api
\ No newline at end of file
Loading
Loading
@@ -5,5 +5,7 @@ try:
bot_secret_ = os.environ.get('SLACK_BOT_SECRET')
channel_ = os.environ.get('CHANNEL')
text_ = os.environ.get('TEXT')
page_id_ = os.environ.get('PAGE_ID')
access_token_ = os.environ.get('ACCESS_TOKEN')
except KeyError:
print('no eniromental variables detected\nmake sure to set them')
Loading
Loading
@@ -21,8 +21,15 @@ def callback():
 
if request.json['type'] == 'wall_post_new':
post = request.json['object']
slack_message = Slack(post=post).create_message()
Slack.send_message(auth=slack, channel=channel_, text=text_, attachments=slack_message)
facebook_post = Facebook(post=post)
facebook_attachment = facebook_post.create_message()
facebook_text = facebook_post.get_text(post=post)
Facebook.send_message(auth=facebook, text=facebook_text, attachment=facebook_attachment)
return 'ok', 200
 
 
Loading
Loading
Loading
Loading
@@ -46,9 +46,11 @@ class Slack(object):
self.author_name, self.author_link, self.author_icon = \
None, None, None
try:
if post['attachments'] and post['attachments'][0]['type'] == 'photo':
image = post['attachments'][0]['photo']
self.image_url, self.thumb_url = get_image(image)
if post['attachments']:
for attachment in post['attachments']:
if attachment['type'] == 'photo':
image = post['attachments'][0]['photo']
self.image_url, self.thumb_url = get_image(image)
except KeyError:
self.image_url, self.thumb_url = None, None
 
Loading
Loading
@@ -98,6 +100,43 @@ class Slack(object):
as_user=as_user)
 
 
class Facebook(object):
def __init__(self, post):
try:
if post['attachments']:
for attachment in post['attachments']:
if attachment['type'] == 'photo':
image = post['attachments'][0]['photo']
image_url, _ = get_image(image)
elif attachment['type'] == 'link':
link = attachment['link']
self.picture = image_url
self.name = link['title']
self.link = link['url']
self.caption = link['title']
self.description = link['description']
except KeyError:
self.name, self.link, self.caption, self.description, self.picture = \
None, None, None, None, None
def create_message(self):
return {
'name' : self.name,
'link' : self.link,
'caption' : self.caption,
'description': self.description,
'picture' : self.picture,
}
@staticmethod
def get_text(post):
return post['text']
@staticmethod
def send_message(auth, text, attachment):
auth.put_wall_post(message=text, attachment=attachment)
def get_image(photo):
try:
image_url = photo['photo_1280']
Loading
Loading
@@ -110,4 +149,3 @@ def get_image(photo):
thumb_url = photo['photo_75']
 
return image_url, thumb_url
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment