-
Notifications
You must be signed in to change notification settings - Fork 0
/
briefs.py
163 lines (142 loc) · 5.63 KB
/
briefs.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
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
import re
#
# We want to create individual briefs from the text that was
# pasted from the PHP page.
# The page gives instructions to select the copy from Newsgate
# in WYSIWIG mode. This gives us tags that we can hang
# a subsitution on.
#
# We want to be ready for either briefhead or subhed tags.
# And these are where we want to do the spliit.
# other than that, we wanna pick out notesface and
# a few basic tags such as an endnote.
#
# Paragraphs are separated as in the AP Exchange feed.
#
# Profile ID comes from a pulldown on the PHP page,
# and it identifies the briefs as state, local region or crime.
#
# The Saxotech slug is created from the 'M' prefix familiar from Coyote,
# the date, the stamp from the PHP write and a suffix applied here to
# distinguish briefs in the same batch.
def break_up(text,filename):
pubText = text
# print "pubText ="
# print pubText
#pubText = re.sub(r'subhead_lead>',r'generic_head>',pubText)
pubText = generalizeHeds(pubText)
pubText = re.sub(r'<generic_head>',r'<split_element><generic_head>',pubText)
briefsList = pubText.split('<split_element>')
profileID = briefsList.pop(0)
print "profileId ="
print profileID
profileID = re.sub(r'.SOM..(\d{4}).',r'\1',profileID)
print "profileId ="
print profileID
profileID = re.sub(r'.EP.',r'',profileID)
print "profileId ="
print profileID
profileID = re.sub(r'<.*',r'',profileID)
print "profileId ="
print profileID
sequenceLetter = "A"
for brief in briefsList:
slug=makeBriefSlug(profileID,filename,sequenceLetter)
prepForPublicus(brief, profileID,slug)
sequenceLetter = nextSequenceLetter(sequenceLetter)
return
def generalizeHeds(text):
pubText = text
pubText = re.sub(r'subhead_lead>',r'generic_head>',pubText)
pubText = re.sub(r'subhead>',r'generic_head>',pubText)
pubText = re.sub(r'subhead_lead>',r'generic_head>',pubText)
return pubText
def makeBriefSlug(profileID,filename,sequenceLetter):
from slugTools import slugTomorrowString
theSlug = "M"
theDate = slugTomorrowString()
theChunk = filename
theChunk = re.sub(r'FTP',r'BRF',theChunk)
theSlug = theSlug + theDate + theChunk + sequenceLetter
print "theslug = " + theSlug
return theSlug
def nextSequenceLetter(sequenceLetter):
if (sequenceLetter == 'Z'):
return '0'
if (sequenceLetter == '9'):
return 'a'
return chr(ord(sequenceLetter)+1)
def webFixed(theText):
theText = re.sub(r'(?i)<web.*?>','',theText)
theText = re.sub(r'(?i)<.web.*?>','',theText)
theText = re.sub(r'<[A-Z][A-Z]>','',theText)
theText = re.sub(r'<EL.{2,4}>','',theText)
theText = re.sub(r'(?i)<tag.*?>','',theText)
theText = re.sub(r'(?i)<.tag.*?>','',theText)
theText = re.sub(r'(?i)<end.*?>','',theText)
theText = re.sub(r'(?i)<.end.*?>','',theText)
return theText
def signatureFixed(theText):
theText = re.sub(r'<EP>(<.signature>)','\1',theText)
theText = re.sub(r'(<.signature>)(<signature_credit>)','\1, \2',theText)
theText = re.sub(r'<.signature>','',theText)
theText = re.sub(r'<signature>','',theText)
theText = re.sub(r'<.signature_credit>','',theText)
theText = re.sub(r'<signature_credit>.{1,4}([A-Z])','\1',theText)
theText = re.sub(r'<signature_credit>\W{1,4}','',theText)
theText = re.sub(r'<signature_credit>','',theText)
return theText
def prepForPublicus(brief,profileID,slug):
from slugTools import wrapAndWrite
from slugTools import openArticle
from slugTools import closeArticle
from slugTools import tomorrowString
filename=slug
#slug = slug + '.xml'
slug = '/Users/Editmac05/Documents/bin/briefs/workbin/' + slug + '.xml'
fileXML = open(slug,'w')
openArticle(fileXML)
briefHed = brief
briefBody = brief
briefHed = re.sub(r'(.*<.generic_head>).*',r'\1',briefHed)
briefHed = re.sub(r'<EP>',r' ',briefHed)
briefHed = re.sub(r' <.gen',r'</gen',briefHed)
print "brief hed = " + briefHed
briefHed = re.sub(r'generic_head','headline',briefHed)
briefHed = re.sub(r'<NO.*?>',r'<NO>',briefHed)
briefHed = re.sub(r'<NO>.*?<NO>',r'',briefHed)
wrapAndWrite("filename",filename,fileXML)
wrapAndWrite("profileId",profileID,fileXML)
wrapAndWrite("Gns","",fileXML)
wrapAndWrite("keyword","BRIEF",fileXML)
wrapAndWrite("pubdate",tomorrowString(),fileXML)
#wrapAndWrite("pubdate",'2/7/2012',fileXML)
wrapAndWrite("edition","",fileXML)
wrapAndWrite("section","",fileXML)
print >> fileXML, briefHed
briefBody = re.sub(r'.*_head>',r'',briefBody)
#print "briefBody = " + briefBody
hasDateline = re.search('<dateline>',briefBody)
#print "hasdateline = " + hasDateline
if hasDateline:
dateline = re.sub(r'(.*<.dateline>).*',r'\1',briefBody)
briefBody = re.sub(r'.*<.dateline>(.*)',r'\1',briefBody)
print "dateline = " + dateline
dateline = re.sub(r'.*(<dateline>)',r'\1',dateline)
print "dateline revised = " + dateline
#print "briefbody = " + briefBody
print >> fileXML, dateline
briefBody = re.sub(r'#EOM#',r'',briefBody)
#print "1. briefbody = " + briefBody
briefBody = re.sub(r'<NO.*?>',r'<NO>',briefBody)
#print "2. briefbody = " + briefBody
briefBody = re.sub(r'<NO>.*?<NO>',r'',briefBody)
#briefBody = re.sub(r'<EP>',r'</body_text><body_text>',briefBody)
briefGrafs = briefBody.split('<EP>')
for graf in briefGrafs:
graf = signatureFixed(graf)
graf = webFixed(graf)
wrapAndWrite('body_text',graf,fileXML)
#wrapAndWrite('body_text',briefBody,fileXML)
closeArticle(fileXML)
return