Advertisement
s243a

pearl_python.py

Aug 9th, 2022
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.81 KB | None | 0 0
  1. from lxml import etree, objectify
  2.  
  3. NSMAP = {
  4.     "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  5.     "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
  6.     "dcterms" : "http://purl.org/dc/elements/1.1/",
  7.     "foaf" : "http://xmlns.com/foaf/0.1/",
  8.     "owl" : "http://www.w3.org/2002/07/owl#",
  9.     "sioc" : "http://rdfs.org/sioc/ns#",
  10.     "pt" : "http://www.pearltrees.com/rdf/0.1/#"
  11.     }
  12. # Import this code as follows:
  13. #with open("variables.py") as fi:
  14. #    exec(fi.read())
  15. #
  16. #
  17. # To understand this code see:
  18. # #https://lxml.de/objectify.html
  19. #
  20. # Random URL for now readson:
  21. # https://medium.com/@snehalgawas/back-to-basics-jupyter-notebooks-dfcdc19c54bc
  22. #
  23. #<?xml version="1.0" encoding="utf-8"?>  
  24. #
  25. #<!--
  26. #*******************************************************************************************#*****************
  27. #* This is a Pearltrees export of your account.
  28. #*
  29. #* You are the full owner of the data you have stored in Pearltrees.
  30. #* This RDF file is your Pearltrees account turned into a W3C-compliant format.
  31. #*
  32. #* To developers:
  33. #* - The author is identified by a FOAF profile with a SIOC account.
  34. #* - The most important classes are "pt:Tree" and "pt:PagePearl".
  35. #* - "pt:Pearl" refers to its parent tree with the "pt:parentTree" property.
  36. #* - "pt:leftPos" and "pt:rightPos" are nested set values (see #"http://en.wikipedia.org/wiki/Nested_set_model")
  37. #* - Most properties and classes are not stable.
  38. #* More to come:
  39. #* - Full documentation.
  40. #* - Publishing schema and OWL descriptions.
  41. #* - Dereferenceable URIs.
  42. #* Interested in more RDF data? Any suggestion? Send us an email at #<participation@pearltrees.net>!
  43. #*******************************************************************************************#*****************
  44. #-->
  45. #
  46. #
  47. #<?xml version="1.0" encoding="UTF-8"?>
  48. #
  49. #<!-- 0.1 : First rough version -->
  50. #
  51. #<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  52. #         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  53. #         xmlns:foaf="http://xmlns.com/foaf/0.1/"
  54. #         xmlns:dcterms="http://purl.org/dc/elements/1.1/"
  55. #         xmlns:pt="http://www.pearltrees.com/rdf/0.1/#">
  56. #
  57. #<!-- Main Classes -->
  58.  
  59. root=objectify.Element("{http://www.w3.org/1999/02/22-rdf-syntax-ns#}rdf",nsmap=NSMAP)
  60. print("type of root:" + str(type(root)))
  61. #<foaf:Person rdf:about="https://www.pearltrees.com/somebody#foaf">
  62. #   <foaf:nick>somebody</foaf:nick>
  63. #   <foaf:mbox_sha1sum>somesha1sum/foaf:mbox_sha1sum>
  64. #   <foaf:mbox>somebody@hotmail.com</foaf:mbox>
  65. #   <sioc:UserAccount rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  66. #</foaf:Person>
  67.  
  68. person=objectify.SubElement(root,"{http://xmlns.com/foaf/0.1/}Person")
  69. print("type of person:" + str(type(person)))
  70. #<sioc:UserAccount rdf:about="https://www.pearltrees.com/somebody#sioc">
  71. #   <pt:rootTree rdf:resource="https://www.pearltrees.com/somebody" />
  72. #   <sioc:account_of rdf:resource="https://www.pearltrees.com/somebody#foaf" />
  73. #   <sioc:ip_address>someIPaddr</sioc:ip_address>
  74. #</sioc:UserAccount>
  75.  
  76. account=objectify.SubElement(root,"{http://rdfs.org/sioc/ns#}UserAccount")
  77. print("type of account:" + str(type(account)))
  78. # - In the orginal pearltrees a tree once was analoges to a mind map for a given
  79. # topic. (e.g. ...)
  80. # - In PT2.0 and on the mindmap concept was replaced by a grid like strucures
  81. # and each node of the tree was replaced by a buch of cards.
  82. # - In either case the tree is the principle web page for each topic containing a number
  83. # of sub items. One major difference is that later version of pearltree lose much of the
  84. # tree struture.
  85. #<pt:Tree rdf:about="url_for_some_tree">
  86. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  87. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  88. #   <pt:treeId>tree_id</pt:treeId>
  89. #   <pt:assoId>tree_id</pt:assoId>
  90. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  91. #   <pt:privacy>0</pt:privacy>
  92. #</pt:Tree>
  93.  
  94. curr_tree=objectify.SubElement(root,"{http://www.pearltrees.com/rdf/0.1/#}Tree",nsmap=NSMAP)
  95. curr_title=objectify.SubElement(curr_tree,"{http://purl.org/dc/elements/1.1/}title",nsmap=NSMAP)
  96. curr_title._setText('<![CDATA["some title"]]>')
  97. curr_creator=objectify.SubElement(curr_tree,"{http://purl.org/dc/elements/1.1/}creator",nsmap=NSMAP)
  98. curr_creator._setText("creator")
  99. curr_tree_Id=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}treeId",nsmap=NSMAP)
  100. curr_tree_Id._setText("tree_id")
  101. curr_assoId=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}assoId",nsmap=NSMAP)
  102. curr_assoId._setText("tree_id")
  103. curr_lastUpdate=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}lastUpdate",nsmap=NSMAP)
  104. curr_lastUpdate._setText("last_id")
  105. curr_privacy=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}privacy",nsmap=NSMAP)
  106. curr_privacy._setText("privacy")
  107. #print(etree.tostring(curr_tree, pretty_print=True))
  108. print(etree.tostring(curr_tree, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  109. print("type of curr_tree:" + str(type(curr_tree)))
  110. # - When pearltrees used mindmaps the rootPearl was the root of the tree.
  111. # - In later version this idea was kept even if the tree structure was abandoned.
  112. # - As a consequence all items of type PearlTree have a root node.
  113. # - The root node always has PosOrder=1, which is equivalent to (for the root node):
  114. #   leftPos=posORder
  115. #   rightPos=[The number of pearls multipled by 2]
  116. #   where leftPos and rightPos define the postion of the root pearl in the nexted set model
  117. #   and all pearls are nested within the root node but no deeper.
  118. #
  119. #   this creats an equivalence between the new linear model (using posOrder) and the previous
  120. #   tree model which used leftPos and rightPos to encode a nested set model.
  121. #  
  122. #
  123. #
  124. #<pt:RootPearl rdf:about="https://url_for_some_tree#rootPearl">
  125. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  126. #   <pt:parentTree rdf:resource="https://url_for_some_tree" />
  127. #   <pt:inTreeSinceDate>2012-05-23T20:00:32</pt:inTreeSinceDate>
  128. #   <pt:posOrder>1</pt:posOrder>
  129. #</pt:RootPearl>
  130.  
  131. curr_pearl=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}RootPearl")
  132. curr_title=objectify.SubElement(curr_pearl,"{http://purl.org/dc/elements/1.1/}title",nsmap=NSMAP)
  133. curr_title._setText('<![CDATA["some title"]]>')
  134. #print(etree.tostring(curr_tree, pretty_print=True))
  135. curr_parentTree=objectify.SubElement(curr_pearl,"{http://www.pearltrees.com/rdf/0.1/#}parentTree",nsmap=NSMAP)
  136. curr_parentTree._setText(curr_tree_Id.text)
  137. curr_inTreeSinceDate=objectify.SubElement(curr_pearl,"{http://www.pearltrees.com/rdf/0.1/#}inTreeSinceDate",nsmap=NSMAP)
  138. curr_inTreeSinceDate._setText("some_date")
  139. curr_posOrder=objectify.SubElement(curr_pearl,"{http://www.pearltrees.com/rdf/0.1/#}inTreeSinceDate",nsmap=NSMAP)
  140. curr_posOrder._setText("some_date")
  141. print(etree.tostring(curr_pearl, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  142. print("type of curr_pearl:" + str(type(curr_pearl)))
  143. # Recall that a Tree is like the principle page about a topic and it can contain sevearl items. One item it
  144. # can contain is a link to an external web page. Such items are called "PagePearl". These items store additional
  145. # information about the external URL beyond the url, such a comments, text, and images.
  146. #
  147. #<pt:PagePearl rdf:about="https://www.pearltrees.com/t/hacktivism/irl-hacktivists-groups/id2492374#item18082864">
  148. #   <dcterms:title><![CDATA[/tmp/lab | Creative use of technology]]></dcterms:title>
  149. #   <dcterms:identifier>http://www.tmplab.org/</dcterms:identifier>
  150. #   <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism/irl-hacktivists-groups/id2492374" />
  151. #   <pt:inTreeSinceDate>2011-02-07T02:42:18</pt:inTreeSinceDate>
  152. #   <pt:posOrder>2</pt:posOrder>
  153. #</pt:PagePearl>
  154.  
  155. curr_PagePearl=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1#}PagePearl",nsmap=NSMAP)
  156. curr_title=objectify.SubElement(curr_PagePearl,"{http://purl.org/dc/elements/1.1/}title",nsmap=NSMAP)
  157. curr_title._setText('<![CDATA["some title"]]>')
  158. #print(etree.tostring(curr_tree, pretty_print=True))
  159. curr_identifier=objectify.SubElement(curr_PagePearl,"{http://www.pearltrees.com/rdf/0.1/#}identifier",nsmap=NSMAP)
  160. curr_identifier._setText("some_wepage_url")
  161. curr_inTreeSinceDate=objectify.SubElement(curr_PagePearl,"{http://www.pearltrees.com/rdf/0.1/#}inTreeSinceDate",nsmap=NSMAP)
  162. curr_inTreeSinceDate._setText("some_date")
  163. curr_posOrder=objectify.SubElement(curr_PagePearl,"{http://www.pearltrees.com/rdf/0.1/#}posOrder",nsmap=NSMAP)
  164. curr_posOrder._setText("some_date")
  165. print(etree.tostring(curr_PagePearl, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  166. print("type of curr_PagePearl:" + str(type(curr_PagePearl)))
  167. #
  168. # t
  169. #
  170. #
  171. # <pt:AliasPearl rdf:about="https://www.pearltrees.com/t/hacktivism/id2492215#item18110176&amp;show=item,18110176">
  172. #    <dcterms:title><![CDATA[Anonymous Culture]]></dcterms:title>
  173. #    <rdfs:seeAlso rdf:resource="https://www.pearltrees.com/anon/anonymous-culture/id2421304" />
  174. #    <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism/id2492215" />
  175. #    <pt:inTreeSinceDate>2011-02-07T14:40:33</pt:inTreeSinceDate>
  176. #    <pt:posOrder>2</pt:posOrder>
  177. # </pt:AliasPearl>
  178.  
  179.  
  180. curr_AliasPearl=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}AliasPearl",nsmap=NSMAP)
  181. curr_title=objectify.SubElement(curr_AliasPearl,"{http://purl.org/dc/elements/1.1/}title",nsmap=NSMAP)
  182. curr_title._setText('<![CDATA["some title"]]>')
  183. #print(etree.tostring(curr_tree, pretty_print=True))
  184. curr_parentTree=objectify.SubElement(curr_AliasPearl,"{http://www.pearltrees.com/rdf/0.1/#}parentTree",nsmap=NSMAP)
  185. curr_parentTree._setText(curr_tree_Id.text)
  186. curr_inTreeSinceDate=objectify.SubElement(curr_AliasPearl,"{http://www.pearltrees.com/rdf/0.1/#}inTreeSinceDate",nsmap=NSMAP)
  187. curr_inTreeSinceDate._setText("some_date")
  188. curr_posOrder=objectify.SubElement(curr_AliasPearl,"{http://www.pearltrees.com/rdf/0.1/#}posOrder",nsmap=NSMAP)
  189. curr_posOrder._setText("some_posOrder")
  190. print(etree.tostring(curr_AliasPearl, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  191. print("type of curr_AliasPearl:" + str(type(curr_AliasPearl)))
  192. # <pt:RefPearl rdf:about="https://www.pearltrees.com/t/hacktivism-s-litteracy/id2492416?show=item,18806157">
  193. #   <dcterms:title><![CDATA[EN]]></dcterms:title>
  194. #   <rdfs:seeAlso rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/en/id2595429" />
  195. #   <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/id2492416" />
  196. #   <pt:inTreeSinceDate>2011-02-28T19:42:18</pt:inTreeSinceDate>
  197. #   <pt:posOrder>2</pt:posOrder>
  198. # </pt:RefPearl>
  199.  
  200. curr_RefPearl=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}RefPearl",nsmap=NSMAP)
  201. curr_title=objectify.SubElement(curr_RefPearl,"{http://purl.org/dc/elements/1.1/}title",nsmap=NSMAP)
  202. curr_title._setText('<![CDATA["some title"]]>')
  203. #print(etree.tostring(curr_tree, pretty_print=True))
  204. curr_seeAlso=objectify.SubElement(curr_RefPearl,"{http://www.pearltree/rdf/0.1/#}seeAlso",nsmap=NSMAP)
  205. curr_seeAlso._setText("some_seealso")
  206. curr_parentTree=objectify.SubElement(curr_RefPearl,"{http://www.pearltrees.com/rdf/0.1/#}parentTree",nsmap=NSMAP)
  207. curr_parentTree._setText(curr_tree_Id.text)
  208. curr_inTreeSinceDate=objectify.SubElement(curr_RefPearl,"{http://www.pearltrees.com/rdf/0.1/#}inTreeSinceDate",nsmap=NSMAP)
  209. curr_inTreeSinceDate._setText("some_date")
  210. curr_posOrder=objectify.SubElement(curr_RefPearl,"{http://www.pearltrees.com/rdf/0.1/#}posOrder",nsmap=NSMAP)
  211. curr_posOrder._setText("some_posOrder")
  212. print(etree.tostring(curr_RefPearl, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  213. print("type of curr_RefPearl:" + str(type(curr_RefPearl)))
  214. # <pt:Note>
  215. #    <dcterms:creator rdf:resource="https://www.pearltrees.com/zarby#sioc" />
  216. #    <dcterms:date>2011-03-01T11:57:52</dcterms:date>
  217. #    <pt:noteText><![CDATA[En effet, les Pearl directs aux articles ne peuvent être changé. Mais merci! C'est une bonne idée!]]></pt:noteText>
  218. #    <pt:parentPearl rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/pdf-livres/id2595431" />
  219. # </pt:Note>
  220.  
  221. curr_Note=objectify.SubElement(curr_tree,"{http://www.pearltrees.com/rdf/0.1/#}Note",nsmap=NSMAP)
  222. curr_creator=objectify.SubElement(curr_Note,"{http://purl.org/dc/elements/1.1/}creator",nsmap=NSMAP)
  223. curr_creator._setText("creator")
  224. curr_date=objectify.SubElement(curr_Note,"{http://purl.org/dc/elements/1.1/}date",nsmap=NSMAP)
  225. curr_date._setText("creator")
  226. curr_noteText=objectify.SubElement(curr_Note,"{http://www.pearltrees.com/rdf/0.1/#}noteText",nsmap=NSMAP)
  227. curr_noteText._setText("some_note_text")
  228. print(etree.tostring(curr_Note, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  229. print("type of curr_Note:" + str(type(curr_Note)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement