Commit d95be5fc authored by Alexey Ternavskiy's avatar Alexey Ternavskiy

Minor fixes.

parent e974bef8
{"last_post_id": 889433495747080192}
\ No newline at end of file
{"last_post_id": 892295179343994880}
\ No newline at end of file
TWITTER_CONSUMER_KEY=None
TWITTER_CONSUMER_SECRET=None
TWITTER_ACCESS_TOKEN=None
TWITTER_ACCESS_TOKEN_SECRET=None
\ No newline at end of file
TWITTER_ACCESS_TOKEN_SECRET=None
SLACK_CLIENT_ID=None
SLACK_CLIENT_TOKEN=None
SLACK_OAUTH_ACCESS_TOKEN=None
SLACK_BOT_TOKEN=None
\ No newline at end of file
from py_digest.slack import write_into_channel
from py_digest.twitter import get_user_posts
if __name__ == '__main__':
posts = get_user_posts()
if posts:
for post in reversed(posts):
write_into_channel(post)
from .api import write_into_channel
\ No newline at end of file
from slackclient import SlackClient
import settings
def check_channel():
slack_client = SlackClient(settings.SLACK_OAUTH_ACCESS_TOKEN)
response = slack_client.api_call('channels.list', exclude_archived=1)
if 'ok' in response:
channels = response.get('channels')
slack_channel = [
channel for channel in channels
if channel.get('name').lower() == settings.SLACK_CHANNEL_NAME
]
if slack_channel:
return slack_channel[0]
else:
response = slack_client.api_call('channels.create', name=settings.SLACK_CHANNEL_NAME)
if 'ok' in response:
return response.get('channel')
raise Exception(response.get('error'))
channel = check_channel()
def write_into_channel(post):
slack_client = SlackClient(settings.SLACK_BOT_TOKEN)
slack_client.api_call('chat.postMessage',
channel=channel.get('id'),
text=post,
as_user=False)
from .api import get_user_posts
\ No newline at end of file
import asyncio
import logging
from asyncio import Semaphore, ensure_future, gather, get_event_loop
from json import load, dumps
from urllib.parse import urlparse
import twitter
import uvloop
from aiohttp import ClientSession
from bs4 import BeautifulSoup
from os.path import join as path_join, getsize
import settings
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
logger = logging.getLogger(__name__)
FILE_PATH = path_join(settings.BASE_DIR, 'data.json')
......@@ -21,7 +24,7 @@ api = twitter.Api(consumer_key=settings.TWITTER_CONSUMER_KEY,
def get_last_post_id():
with open(FILE_PATH) as file:
with open(FILE_PATH, 'w') as file:
if not getsize(FILE_PATH):
return None
try:
......@@ -69,13 +72,12 @@ async def bound_fetch(sem, obj, session):
async def run(urls):
tasks = []
sem = Semaphore(50)
sem = asyncio.Semaphore(50)
async with ClientSession() as session:
for obj in urls:
task = ensure_future(bound_fetch(sem, obj, session))
task = asyncio.ensure_future(bound_fetch(sem, obj, session))
tasks.append(task)
responses = gather(*tasks)
responses = asyncio.gather(*tasks)
return await responses
......@@ -86,8 +88,8 @@ def get_user_posts():
if not results:
return None
update_last_post_id(results[0].id)
loop = get_event_loop()
future = ensure_future(run(results))
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(results))
posts = loop.run_until_complete(future)
loop.close()
return posts
......@@ -3,3 +3,5 @@ python-dotenv
lxml
beautifulsoup4
aiohttp
slackclient
uvloop
\ No newline at end of file
......@@ -12,3 +12,9 @@ TWITTER_ACCESS_TOKEN = os.environ.get('TWITTER_ACCESS_TOKEN')
TWITTER_ACCESS_TOKEN_SECRET = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')
PYDIGEST_USER_NAME = 'pydigest'
SLACK_CHANNEL_NAME = 'py-digest'
SLACK_CLIENT_ID = os.environ.get('SLACK_CLIENT_ID')
SLACK_CLIENT_TOKEN = os.environ.get('SLACK_CLIENT_TOKEN')
SLACK_OAUTH_ACCESS_TOKEN = os.environ.get('SLACK_OAUTH_ACCESS_TOKEN')
SLACK_BOT_TOKEN = os.environ.get('SLACK_BOT_TOKEN')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment