/
daily_script.py
executable file
·121 lines (90 loc) · 5.3 KB
/
daily_script.py
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
#!/UlyssesRedux/scripts/bin/python3
# -*- coding: utf-8 -*-
"""Script to call modules that generate the blog's content, then posts that
content to the ulyssesredux.tumblr.com.
Each chapter is written by a different script that resides in the
chapter_scripts/ directory. Each of these scripts then draw from
texts in the corpora/ directory.
This program is licensed under the GPL v3 or, at your option, any later
version. See the file LICENSE.md for a copy of this licence.
"""
import datetime
import html
import importlib
import json
import math
import re
import sys
sys.path.append('/UlyssesRedux/scripts/')
from directory_structure import * # Gets us the listing of file and directory locations.
import utility_scripts.current_run_data_utils as cr_data
import patrick_logger, introspection # From https://github.com/patrick-brian-mooney/personal-library
from introspection import dump_str
from patrick_logger import log_it
import social_media # From https://github.com/patrick-brian-mooney/personal-library
with open('/social_media_auth.json', encoding='utf-8') as auth_file:
ulysses_client = social_media.Tumblpy_from_dict(json.loads(auth_file.read())['ulysses_client'])
recurring_tags = ['Ulysses (novel)', 'James Joyce', '1922', 'automatically generated text', 'Patrick Mooney']
ulysses_chapters = open(ulysses_chapter_titles_file).readlines()
patrick_logger.verbosity_level = 3
# First, set up parameters.
blog_url = 'http://ulyssesredux.tumblr.com/'
# Some utility routines
def out_of_content_warning():
"""Remind me that we're out of content."""
log_it("WARNING: There's work to be done! You have to reset the blog state on ulyssesredux.tumblr.com to get it working again! A full Ulysses project is done and needs to be cleared!")
log_it(" REMINDER: make this a more prominent warning!") # FIXME
sys.exit(2)
if __name__ == "__main__":
current_run_data = cr_data.read_current_run_parameters()
try:
with open('%s/index.html' % current_run_directory, 'r') as index_file :
the_lines = index_file.readlines()
which_script = 1 + len(the_lines) # If so far we've got, say, six lines in the file, we need to run script #7.
except (FileNotFoundError,):
which_script = 1
the_lines = [][:]
if which_script not in range(1,19):
out_of_content_warning()
# Post parameters
the_title = ulysses_chapters[ which_script - 1 ].strip()
recurring_tags.append(the_title)
current_chapter_temporary_tags = current_run_data['ch%02dtags' % which_script]
temporary_tags = [l.strip() for l in open('%s/temporary-tags' % current_run_directory).readlines()]
the_tags = ', '.join(recurring_tags + temporary_tags) + ', ' + current_chapter_temporary_tags
script_path = '%s.ch%02d' % (daily_scripts_directory, which_script)
print("INFO: About to run script %s.py." % script_path)
# OK, import the relevant chapter script as a module and write the story.
the_script = importlib.import_module(script_path)
the_content = the_script.write_story()
print("content generated ...\n\n ... postprocessing...")
content_lines = the_content.split("\n")
# Now, split the first paragraph into sentences, keeping the final punctuation and joining it back to the end of the sentence.
first_sentence = ''.join(list(filter(None, re.split("([!?.]+)", content_lines[0])))[0:2]) # We'll use this as the summary in the table of contents.
content_lines = [ "<p>" + the_line.strip() + "</p>" for the_line in content_lines if len(the_line.strip()) > 0 ]
the_content = '\n'.join(content_lines)
print("INFO: postprocessed content is:\n\n" + "\n".join(content_lines))
print('INFO: Chapter title is "%s."' % the_title)
print("INFO: tags are %s." % str(recurring_tags + temporary_tags))
# All right, post this content
print('\nINFO: Attempting to post the content')
the_status, the_tumblr_data = social_media.tumblr_text_post(ulysses_client, the_tags, the_title, the_content)
print('\nINFO: the_status is: ' + dump_str(the_status))
print('\nINFO: the_tumblr_data is: ' + dump_str(the_tumblr_data))
new_post_url = blog_url + "post/" + str(the_status['id'])
# Assemble some text to write to the index file
html_tags = ' | '.join([ '<a rel="me muse" href="%s">%s</a>' % (html.escape(blog_url + "tagged/" + the_tag), the_tag) for the_tag in the_tags.split(', ') ])
# Avoid using a really really long first sentence as a summary (a problem sometimes in tests with "Penelope").
while len(first_sentence) > 600 or len(first_sentence.split(' ')) > 150:
first_sentence = ' '.join(first_sentence.split(' ')[0 : math.floor(len(first_sentence.split(' ')) * 0.75)]) + '…' # Lop off the last quarter and try again.
the_line = '<li><a rel="me muse" href="%s">%s</a>' % (new_post_url, the_title)
the_line += ' (%s), ' % datetime.date.today().strftime("%d %B %Y")
the_line += current_run_data[ 'ch%02ddesc' % which_script ]
the_line += ': <blockquote><p>%s</p>' % first_sentence
the_line += '<p><small>tags: ' + html_tags + '</small></p>'
the_line += '</blockquote></li>\n'
# Now record the new line to the index file.
the_lines.append(the_line)
index_file = open('%s/index.html' % current_run_directory, 'w')
index_file.writelines(the_lines)
index_file.close()