-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_json.py
More file actions
128 lines (121 loc) · 6.23 KB
/
read_json.py
File metadata and controls
128 lines (121 loc) · 6.23 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
import sys, os, subprocess, argparse, time
import json as json
import xml.etree.ElementTree as ET
import xml.dom.minidom as mindom
from xml.sax.saxutils import unescape
#json and xml modules are already standard at python >2.6
#Sangbaek Lee, Jan. 30 2019
#keep every digit in epochtime in the stamps
#do not use 3rd party libraries if possible e.g. pip
#rss follows xml format with very specific tags: title, link, description(required) + some optional tags (e.g. media, pubDate, guid, url,...)
#
class rss_object:
def __init__(self, tag_main, title='Test',link='https://clas12mon.jlab.org/status/decoding/',description='description',pubDate=None,guid=None,url=None):
self.type = "rss_object"
self.tag_main= tag_main
self.tag_title= ET.SubElement(self.tag_main,"title")
self.tag_title.text = title
self.tag_link = ET.SubElement(self.tag_main,"link")
self.tag_link.text = link
self.tag_description = ET.SubElement(self.tag_main,"description")
self.tag_description.text = unescape(description)
if pubDate != None:
self.tag_pubDate= ET.SubElement(self.tag_main,"pubDate")
self.tag_pubDate.text = pubDate
if guid != None:
self.tag_guid= ET.SubElement(self.tag_main,"guid")
self.tag_guid.text = guid
if url != None:
self.tag_url= ET.SubElement(self.tag_main,"url")
self.tag_url.text = url
#Classes for making the very first rss file.
#Initial Setup of essential components for an RSS feed.
#Call it rss main
class rss_main(rss_object):
def __init__(self, title="Test: converting a json to an rss file",link='https://clas12mon.jlab.org/status/decoding/',description='main description',save=False,fileout='out.xml'):
self.type = "rss_main"
self.tag_namespace = ET.Element('rss')
self.tag_namespace.set("version","2.0")
self.tag_channel = ET.SubElement(self.tag_namespace,"channel")
#sub objects under channel
rss_object.__init__(self,self.tag_channel, title,link,description)
if fileout == None:
self.fileout = fileout
if save:
self.savexml(fileout)
#a simple ftn to write rss file
def savexml(self,fileout=None):
if fileout == None:
fileout=self.fileout
self.dom=mindom.parseString(ET.tostring(self.tag_namespace))
self.prettyprint=self.dom.toprettyxml()
self.prettyprint= self.prettyprint.replace(""","\"")
outfile = open(fileout,"w")
outfile.write(unescape(self.prettyprint))
outfile.close()
# add rss object
def add_item(self, title='item_title',link='https://clas12mon.jlab.org/status/decoding/',description='item_description',pubDate=None,guid=None,url=None,save=False, fileout='out.xml'):
self.tag_item = ET.SubElement(self.tag_channel,"item")
self.item = rss_object( self.tag_item,title,link,description, pubDate, guid, url)
self.dom=mindom.parseString(ET.tostring(self.tag_namespace))
self.prettyprint=self.dom.toprettyxml()
if save:
savexml(fileout)
def nodoublelinebreak(string):
target_string=['\n\n','\n\t\n\t','\n\t\t\n\t\t','\n\t\t\t\n\t\t\t']#,'\n\t\t\t\t\n\t\t\t\t']
# desired_string = ['\n','\n\t','\n\t\t','\n\t\t\t']#,'\n\t\t\t\t']
for i in range(0,len(target_string)):
while string.find(target_string[i]) >= 0:
string = string[:string.find(target_string[i])]+string[string.find(target_string[i])+i+1:]
return string
#Modify an rss file existing and add some items
class rss_modify(rss_object):
def __init__(self, title='item_title',link='https://clas12mon.jlab.org/status/decoding/',description='item_description',pubDate=None,guid=None,url=None,save=False, item_add = False, filein = 'in.xml', fileout=None):
self.type = "rss_modify"
self.tree = ET.parse(filein)
self.tag_namespace = self.tree.getroot()
self.tag_channel = self.tag_namespace[0]
self.filein = filein
self.fileout = fileout
# self.dom=mindom.parseString(ET.tostring(self.tag_namespace))
self.prettyprint=ET.tostring(self.tag_namespace)
if item_add:
self.tag_item = ET.SubElement(self.tag_channel,"item")
self.item = rss_object( self.tag_item,title,link,description, pubDate, guid, url)
self.add_item(title,link,description,pubDate,guid,url)
if fileout == None:
self.fileout = self.filein
if save:
self.savexml(self.fileout)
def savexml(self,fileout=None):
if fileout == None:
fileout=self.fileout
outfile = open(fileout,"w")
self.prettyprint = nodoublelinebreak(str(self.prettyprint))
if self.prettyprint.find('\n\t\n</rss>')>0:
self.prettyprint=self.prettyprint[:self.prettyprint.find('\n\t\n</rss>')]+self.prettyprint[self.prettyprint.find('\n\t\n</rss>')+2:]
self.prettyprint= self.prettyprint.replace(""","\"")
self.prettyprint=self.prettyprint.replace('<![CDATA[','')
self.prettyprint=self.prettyprint.replace(']]>','')
self.prettyprint=self.prettyprint.replace("<TABLE", "<![CDATA[<TABLE")
self.prettyprint=self.prettyprint.replace("</TABLE>", "</TABLE>]]>")
outfile.write(unescape(self.prettyprint))
outfile.close()
def add_item(self, title='item_title',link='https://clas12mon.jlab.org/status/decoding/',description='item_description',pubDate=None,guid=None,url=None,save=False, fileout=None):
self.tag_item = ET.SubElement(self.tag_channel,"item")
self.item = rss_object( self.tag_item,title,link,description, pubDate, guid, url)
self.dom=mindom.parseString(ET.tostring(self.tag_namespace))
self.prettyprint=self.dom.toprettyxml()
self.prettyprint = nodoublelinebreak(str(self.prettyprint))
if self.prettyprint.find('\n\t\n</rss>')>0:
self.prettyprint=self.prettyprint[:self.prettyprint.find('\n\t\n</rss>')]+self.prettyprint[self.prettyprint.find('\n\t\n</rss>')+2:]
if fileout == None:
fileout=self.fileout
if save:
savexml(fileout)
def json_data(myjsonfile):
try:
json_data = json.load(myjsonfile)
except ValueError, e:
return False
return json_data