-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBt_24.py
More file actions
197 lines (151 loc) · 6 KB
/
SBt_24.py
File metadata and controls
197 lines (151 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import urllib.request
import time
import facebook
import csv
import textwrap
from PIL import Image, ImageDraw, ImageFont
import random
import os
import requests
import io
import urllib3
import datascience
import numpy as np
import re
from bs4 import BeautifulSoup
##DIALOGUE GEN##
def dialogue_gen (corpus, num):
story = open(corpus, encoding='utf8').read()
dialogue = story.split()
d_markov = secondOrderMarkov(dialogue, num)
dialogue = d_markov.rsplit('.',1)[0]+"."
return dialogue
##MARKOV##
def secondOrderMarkov(corpus,words):
def make_pairs(corpus):
for i in range(len(corpus)-1):
yield (corpus[i], corpus[i+1])
pairs = make_pairs(corpus)
word_dict = {}
for word_1, word_2 in pairs:
if word_1 in word_dict.keys():
word_dict[word_1].append(word_2)
else:
word_dict[word_1] = [word_2]
first_word=random.choice(corpus)
while first_word.islower():
first_word = np.random.choice(corpus)
chain = [first_word]
n_words = words
for i in range(n_words):
chain.append(np.random.choice(word_dict[chain[-1]]))
return ' '.join(chain)
def post_to_fb(txt, img):
access_token='EAACVS6jUj0QBAJZCSXTsVSminS2ogpxFKrPvV6HqZBErPtM71jU01riPKb9jFJyvkaZA6phVKZBUXhkZCZCZCFHkKTz4dEUZBRwYo0yNaZCgatG48cMCYnoH1AemUlLo8hgSqTjwzrodV1J67qSUD4FjvEXQUxayfSx7Da74UQFfE8BnZBR000Lucs7WbCdf6Dl5sZD'
graph = facebook.GraphAPI(access_token, version="3.0")
post_id = graph.put_photo(image=open(img, 'rb'), message=txt)
return print("The post "+post_id["id"]+" went live on "+time.ctime(time.time())+".")
def script_runner():
##LOCATION##
with open('SB1902/final_cities.txt') as a:
f_c = list(a)
for i in np.arange(len(f_c)):
f_c[i] = f_c[i][:-1]
prefix = ['INT.', 'EXT.', 'INT/EXT.']
rand_city = random.choice(f_c)
locale = ['HOUSE', 'KITCHEN', 'ALLEY', 'THEATRE', 'BAR', 'BEDROOM', 'FIELD', 'FOREST']
TOD = ['EARLIER', 'LATER', 'TOMORROW', 'EVENING', 'DAWN', 'DUSK', 'MIDDLE OF NIGHT']
location = random.choice(prefix)+" "+random.choice(f_c)+" "+random.choice(locale)+" - "+random.choice(TOD)
##Characters##
with open('SB1902/names.csv', newline='') as csvfile:
scotland = csv.reader(csvfile, delimiter=' ', quotechar='|')
names = []
for row in scotland:
name = row[0]
names.append(name)
characters = random.sample(names, 2)
character1 = characters[0]
character2 = characters[1]
##DESC##
story = open('SB1902/stories/2.txt', encoding='utf8').read()
corpus = story.split()
##DESC CONTD##
description = secondOrderMarkov(corpus, 40).rsplit('.',1)
final_desc = description[0].replace('ZORP', random.choice(characters))+"."
##DIALOGUE CONTD##
dialogue_1 = dialogue_gen("SB1902/dialogue.txt", random.randint(8,20))
dialogue_2 = dialogue_gen("SB1902/dialogue.txt", random.randint(8,20))
##ACTION GEN##
with open('SB1902/parts of speech word files/present_tense.csv') as f:
present_tense = list(f)[0]
with open('SB1902/parts of speech word files/adverbs.csv') as a:
adverbs = list(a)
ly_adverbs = []
for i in adverbs:
i.split('\n')
if i.endswith('ly\n'):
ly_adverbs = np.append(ly_adverbs, i.lower()[:-1] )
else:
pass
present_list = present_tense.split(',')
action = character1+" "+random.choice(present_list)+" "+random.choice(ly_adverbs)
##IMG COMPILER##
##structure
img = Image.new('RGB', (500,500), color = "white")
fnt = ImageFont.truetype('SB1902/Courier New Bold.ttf', 15)
l = ImageDraw.Draw(img)
l.text((25,25), location.upper(), font=fnt, fill=(0, 0, 0))
desc = textwrap.wrap(final_desc, width=50)
y_desc = 70
for d in desc:
width, height = fnt.getsize(d)
l.text((25, y_desc), d, font=fnt, fill=(0,0,0))
y_desc += height
## Structure Randomizer
elements=['cd1'] * 35 + ['cd2'] * 35 + ['a'] * 30
structure = list(['','','','',''])
for i in np.arange(len(structure)):
structure[i] = random.choice(elements)
while structure[i]==structure[i-1]:
structure[i] = random.choice(elements)
# Structure Applicator
for i in np.arange(len(structure)):
output = []
y_mod = i*60
if structure[i] == 'cd1':
l.text((200,160 + y_mod), character1, font=fnt,fill=(0,0,0))
dia1 = textwrap.wrap(dialogue_gen("SB1902/stories/final_dialogu.txt", random.randint(10,18)), width=45)
y_dia1 = 180 + y_mod
for d in dia1:
width, height = fnt.getsize(d)
l.text((45, y_dia1), d, font=fnt, fill=(0,0,0))
y_dia1 += height
elif structure[i] == 'cd2':
l.text((200,160 + y_mod), character2, font=fnt,fill=(0,0,0))
dia2 = textwrap.wrap(dialogue_gen("SB1902/stories/final_dialogu.txt", random.randint(10,18)), width=45)
y_dia2 = 180 + y_mod
for d in dia2:
width, height = fnt.getsize(d)
l.text((45, y_dia2), d, font=fnt, fill=(0,0,0))
y_dia2 += height
else:
l.text((80,175+y_mod), random.choice(characters)+" "+random.choice(present_list)+" "+random.choice(ly_adverbs), font=fnt,fill=(0,0,0))
save_file = 'SB1902/timed_hop/script_'+location.replace(' ', "").replace('-', "").replace('/', "")+"_"+character1+"_"+character2+'.png'
img.save(save_file)
output=post_to_fb(location.upper()+": "+character1+", "+character2, save_file)
return output
def timer(hours):
starttime=time.time()
posts=[]
i = 0
while i <= hours:
script_runner()
time.sleep((30.0) - ((time.time() - starttime) % (30.0)))
script_runner()
posts = np.append(posts, 'post')
time.sleep((86400.0) - ((time.time() - starttime) % (86400.0)))
i += 1
return len(posts)
print('initialized: timer')
timer(1000)
print('terminated: action')