· 9 years ago · Oct 18, 2016, 03:36 PM
1__author__ = 'We are Content Providers'
2
3import json
4from twython import Twython, TwythonError, TwythonStreamer
5import pymongo
6import re
7from time import sleep, gmtime, strftime
8import random
9import numpy as np
10from botcontrol import My_Bot
11
12APP_KEY = '' # Consumer key
13APP_SECRET = '' # Consumer secret
14
15OAUTH_TOKEN = '' # Access token
16OAUTH_TOKEN_SECRET = '' # Access token secret
17
18twitter = Twython(APP_KEY, APP_SECRET, oauth_version=2)
19ACCESS_TOKEN = twitter.obtain_access_token()
20
21twitter = Twython(APP_KEY, access_token=ACCESS_TOKEN)
22
23connection = pymongo.Connection('localhost', 27017)
24db = connection.ContentProviders
25
26class Streamer(TwythonStreamer):
27 def on_success(self, data):
28 x = db.tweets.count()
29 if x <= 5:
30 if 'user' in data:
31
32 #print data['user']['description']
33 try:
34 if "I " in data['user']['description']:
35 x = data['user']['description']
36 m = re.search("([I][^\.!?]*?I [^\.!?]*[\.!?]|[I][^\.!?]*[\.!?])", x)
37 rep = m.group(0)
38 new_dict = {}
39 foundtime = strftime("%d/%m/%Y %H:%M:%S", gmtime())
40 new_dict["Time"] = foundtime
41 new_dict["Sentence"] = rep
42 new_dict["User"] = data['user']['screen_name']
43 db.tweets.insert(new_dict)
44
45 elif "I'm " in data['user']['description']:
46 x = data['user']['description']
47 m = re.search("([I][^\.!?]*?I [^\.!?]*[\.!?]|[I][^\.!?]*[\.!?])", x)
48 rep = m.group(0)
49 new_dict = {}
50 foundtime = strftime("%d/%m/%Y %H:%M:%S", gmtime())
51 new_dict["Time"] = foundtime
52 new_dict["Sentence"] = rep
53 new_dict["User"] = data['user']['screen_name']
54 db.tweets.insert(new_dict)
55
56 else:
57 pass
58
59 except:
60 pass
61 else:
62 self.disconnect()
63
64 def on_error(self, status_code, data):
65 print status_code, data
66
67
68def Me_and_You(traits):
69 traits = np.random.permutation(traits)
70 my_trait = traits[0]
71
72 your_trait = traits[1]
73 #your_trait = your_trait.replace("my", "your")
74 #your_trait = your_trait.replace("am", "are")
75 if len(my_trait) <= 120:
76 if len(your_trait) <= 120:
77 return my_trait, your_trait
78 else:
79 Me_and_You(traits)
80 else:
81 Me_and_You(traits)
82
83def WillItWork():
84 with open('Twitter-Poem-Questions.txt') as f:
85 questions = f.read().splitlines()
86 question = random.choice(questions)
87 willitwork = random.choice(["Yes", "No"])
88 return question, willitwork
89
90def Pillow_Talk(me, you, question, willitwork):
91
92 alex = My_Bot(config_file='alex_config.txt')
93 lenni = My_Bot(config_file='lenni_config.txt')
94
95 alex.send_tweet('@ContentProvide2 '+me+'')
96 sleep(6)
97 reply_id = lenni.get_user_timeline(screen_name='ContentProvide1')[0]['id_str']
98 print reply_id
99 lenni.send_tweet('@ContentProvide1 '+you+'', in_reply_to=reply_id)
100 sleep(6)
101 question_id = alex.get_user_timeline(screen_name='ContentProvide2')[0]['id_str']
102 alex.send_tweet('@ContentProvide2 '+question+'', in_reply_to=question_id)
103 sleep(6)
104 last_id = lenni.get_user_timeline(screen_name='ContentProvide1')[0]['id_str']
105 lenni.send_tweet('@ContentProvide1 '+willitwork+".", in_reply_to=last_id)
106
107def choose_traits():
108
109 traits = db.tweets.find().limit(21)
110 our_traits = []
111 for trait in traits:
112 if trait['Sentence'].startswith('I I'):
113 trait_sentence = trait['Sentence'].replace('I I', 'I')
114 our_traits.append(trait_sentence)
115 db.seenUsers.insert(trait)
116 db.tweets.remove({'User': trait['User']})
117 else:
118 our_traits.append(trait['Sentence'])
119 db.seenUsers.insert(trait)
120 db.tweets.remove({'User': trait['User']})
121 return our_traits
122
123
124def main():
125
126
127 stream = Streamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
128 stream.statuses.filter(locations = '-122.75,36.8,-121.75,37.8,-74,40,-73,41')
129
130 our_traits = choose_traits()
131 me, you = Me_and_You(our_traits)
132
133 question, willitwork = WillItWork()
134 Pillow_Talk(me, you, question, willitwork)
135 print "DONE"
136
137
138if __name__ == "__main__":
139 main()