Created
November 2, 2016 19:09
-
-
Save johndavidback/1e87539b047139f64d501b6634a8da84 to your computer and use it in GitHub Desktop.
Basic consumers to connect, start a scraper, and that's it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from channels import Group | |
from channels.sessions import channel_session | |
from channels.auth import channel_session_user_from_http, channel_session_user | |
from scraper.tasks import scrape_twitter | |
from django.conf import settings | |
import json | |
@channel_session_user_from_http | |
def ws_connect(message): | |
if message.content['path'] == '/feed/': | |
g = 'twitter-feed-{user_id}'.format(user_id=message.user.id) | |
Group(g).add(message.reply_channel) | |
@channel_session_user | |
def ws_message(message): | |
content = json.loads(message.content['text']) | |
g = 'twitter-feed-{user_id}'.format(user_id=message.user.id) | |
# THIS IS THE STREAM / ITERATOR THAT JUST RUNS FOREVER UNTIL THE | |
# END OF EARTH IN A FIERY MIASMA OF HORROR. | |
scrape_twitter( | |
user_id=message.user.id, | |
access_token=settings.TWITTER_ACCESS_TOKEN_KEY, | |
access_token_secret=settings.TWITTER_ACCESS_TOKEN_SECRET, | |
track=track, | |
) | |
@channel_session_user | |
def ws_disconnect(message): | |
# Can I kill that thing up there somehow? | |
g = 'twitter-feed-{user_id}'.format(user_id=message.user.id) | |
Group(g).discard(message.reply_channel) | |
def msg_consumer(message): | |
""" | |
Push this message down to the web who may be watching it. | |
Args: | |
message: the text from twitter | |
Returns: nuttin | |
""" | |
if message.content['type'] == 'tweet': | |
payload = json.dumps({ | |
'text': message.content['text'], | |
'id': message.content['tweet_id'], | |
'screen_name': message.content['screen_name'], | |
'coordinates': message.content['coordinates'], | |
'image': message.content.get('image'), | |
'url': message.content.get('url'), | |
'type': 'tweet' | |
}) | |
else: | |
payload = json.dumps({ | |
'type': 'instagram', | |
'caption': message.content['caption'], | |
'image': message.content['image'], | |
'username': message.content['username'], | |
}) | |
try: | |
g = 'twitter-feed-{user_id}'.format(user_id=message.content['user_id']) | |
Group(g).send({'text': payload}) | |
except ValueError: | |
print('Sorry cannot do it') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment