def post(self): bot = TwitterBot() user_id = self.request.get('id') logging.info('user id: ' + str(user_id)) if self.request.get('task') == 'refollow': bot.refollow(user_id) else: bot.unfollow(user_id)
def test_split_message_two_messages(self): test_message = ("This is a test string with more than 140 characters" ". Adding useless words to make the string longer. This was not" " enough we need even more words. Adding a few more words to make" " it more distinct.") test_message1 = test_message[:133] + " (1/2)" test_message2 = test_message[134:] + " (2/2)" self.assertEqual([test_message1, test_message2], TwitterBot.split_message(self, test_message))
def get(self): logging.info('start unfollow ...') bot = TwitterBot() not_firend_users = bot.get_not_friend_users() if not not_firend_users: self.response.headers['Content-Type'] = 'text/plain' self.response.write('Not the user who is only follow\n') return for i, not_friend_id in enumerate(not_firend_users): if i >= TwitterBot.NUM_OF_FOLLOW: break taskqueue.add(queue_name='follow', url='/follow', params={'task': 'unfollow', 'id': not_friend_id})
def test_split_message_two_messages(self): test_message = ( "This is a test string with more than 140 characters" ". Adding useless words to make the string longer. This was not" " enough we need even more words. Adding a few more words to make" " it more distinct.") test_message1 = test_message[:133] + " (1/2)" test_message2 = test_message[134:] + " (2/2)" self.assertEqual([test_message1, test_message2], TwitterBot.split_message(self, test_message))
def test_split_message_three_messages(self): test_message = ("This is a test string with more than 280 characters" ". Adding useless words to make the string longer. This was " "not enough we need even more words. Mary had a little lamb. " "His fleece was white as snow. And everywhere that Mary went, " "the lamb was sure to go. He followed her to school one day. " "Which was against the rule. It made the children laugh and " "play, to see a lamb at school.") test_message1 = test_message[:133] + " (1/3)" test_message2 = test_message[134:267] + " (2/3)" test_message3 = test_message[268:] + " (3/3)" self.assertEqual([test_message1, test_message2, test_message3], TwitterBot.split_message(self, test_message))
def test_split_message_three_messages(self): test_message = ( "This is a test string with more than 280 characters" ". Adding useless words to make the string longer. This was " "not enough we need even more words. Mary had a little lamb. " "His fleece was white as snow. And everywhere that Mary went, " "the lamb was sure to go. He followed her to school one day. " "Which was against the rule. It made the children laugh and " "play, to see a lamb at school.") test_message1 = test_message[:133] + " (1/3)" test_message2 = test_message[134:267] + " (2/3)" test_message3 = test_message[268:] + " (3/3)" self.assertEqual([test_message1, test_message2, test_message3], TwitterBot.split_message(self, test_message))
def setUpClass(cls): if (os.environ["TRAVIS_SECURE_ENV_VARS"] == "true"): cls.test_api = True API_KEY_TEST = os.environ["API_KEY_TEST"] API_SECRET_TEST = os.environ["API_SECRET_TEST"] ACCESS_TOKEN_TEST = os.environ["ACCESS_TOKEN_TEST"] ACCESS_TOKEN_SECRET_TEST = os.environ["ACCESS_TOKEN_SECRET_TEST"] cls.test_string = "Test string: " + datetime.now().isoformat() cls.bot_test = TwitterBot("", [[cls.test_string]], API_KEY_TEST, API_SECRET_TEST, ACCESS_TOKEN_TEST, ACCESS_TOKEN_SECRET_TEST) else: cls.test_api = False
def test_split_message_one_message(self): test_message = "This is a test string with less than 140 characters." self.assertEqual([test_message], TwitterBot.split_message(self, test_message))
def post(self): msg = self.request.get('message') bot = TwitterBot() bot.tweet(msg)
""" A simple web server to execute the bot functions """ import os from flask import Flask, jsonify from flask_httpauth import HTTPTokenAuth from hashlib import blake2b from twitter import get_api from bot import TwitterBot bot = TwitterBot(get_api()) app = Flask(__name__) auth = HTTPTokenAuth(scheme='Token') @auth.verify_token def verify_token(token): token_hash = blake2b(bytes(token, encoding='utf8')).hexdigest() return token_hash == os.environ['TOKEN_HASH'] @app.route('/') def index(): """ Return total number of tweets bot has tweeted so far """ return jsonify({ 'num_tweets': bot.get_num_tweets(),
def main(): bot = TwitterBot() bot.run()
## %cd ~/twitter-bots-smw-2016/ import re from bot import TwitterBot bot1 = TwitterBot('test1') def strip_links(text): text = re.sub(r"http\S+", "", text, flags=re.MULTILINE) return text.encode('ascii', 'ignore').lower().strip() tl = tw.GetUserTimeline(screen_name='KanyeWest', count=200) f = open('data/kanye/tweets.txt', 'ab') while len(tl) > 1: for t in tl: max_id = t.id print max_id f.write(strip_links(t.text)) f.write('\n') tl = tw.GetUserTimeline(screen_name='KanyeWest', count=200, max_id=max_id)
from bot import TwitterBot from message import Message from config import * from pathlib import Path from timer import Timer import os import subprocess MAX_SIZE = 3072 bot = TwitterBot(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET) os.chdir('media') # Delete any '.DS_Store files' subprocess.run(["find", ".", "-name", ".DS_Store", "-delete"]) for gif in os.listdir('.'): # Get the size of the file gif_size = (Path(gif).stat().st_size) // 1000 print("{} ({} kb)".format(gif, gif_size)) if os.path.isdir(gif): print("Not a .GIF, skipping.") elif gif_size > MAX_SIZE: print("File size is over limit by {} kb".format(gif_size - MAX_SIZE)) # Move file to Resize folder. print("Moving {:s} to resize directory\n".format(gif)) os.rename(gif, os.path.join("resize", gif)) else: bot.tweet(Message().body, gif)
from bot import TwitterBot bot = TwitterBot(cashtag=["bitcoin"]) bot.run()