Sync.swift 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import CoreData
  22. import UIKit
  23. /**
  24. Class to handle server sync.
  25. */
  26. class Sync{
  27. var initial: Bool = false
  28. /**
  29. Starts the sync process.
  30. Always asynchronously.
  31. */
  32. init(){
  33. let url = buildUrl();
  34. sync(url: url)
  35. }
  36. /**
  37. Starts the sync process, synchronously or asynchronously.
  38. :param: synchronous True for synchronous sync, false for asynchronously
  39. */
  40. init(synchronous: Bool){
  41. if synchronous == true{
  42. NSLog(":SYNC:LOG: Synchronous sync started.")
  43. self.initial = true
  44. }
  45. let url = buildUrl();
  46. sync(url: url)
  47. }
  48. /**
  49. Builds the URL to perform the sync against.
  50. :returns: The URL.
  51. */
  52. func buildUrl() -> URL{
  53. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app")
  54. //TODO add parameters
  55. return url!
  56. }
  57. /**
  58. Performs an asynchronous sync.
  59. It fethes the info from the server and stores as Core Data
  60. :param: url The url to sync.
  61. */
  62. func sync(url: URL){
  63. NSLog(":SYNC:LOG: Sync started.")
  64. //Synchronously get data
  65. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  66. guard error == nil else {
  67. NSLog(":SYNC:ERROR: Unknown error.")
  68. return
  69. }
  70. guard let data = data else {
  71. NSLog(":SYNC:ERROR: Data is empty.")
  72. return
  73. }
  74. NSLog(":SYNC:LOG: Data received.")
  75. var strData = String(data:data, encoding: String.Encoding.utf8)
  76. let dataIdx = strData?.indexOf(target: "{\"data\"")
  77. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  78. //TODO: do something with versions
  79. //Get tables
  80. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  81. let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  82. let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  83. let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  84. let dataActivityItinerary : [String] = self.getTable(data: strData!, table: "activity_itinerary")
  85. let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  86. let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  87. let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  88. let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  89. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  90. let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  91. let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  92. let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  93. let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  94. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  95. let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  96. let dataPost : [String] = self.getTable(data: strData!, table: "post")
  97. let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  98. let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  99. let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  100. let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  101. let dataSettings: [String] = self.getTable(data: strData!, table: "settings")
  102. //One by one, save the received data
  103. self.saveTableActivity(entries : dataActivity)
  104. self.saveTableActivityComment(entries : dataActivityComment)
  105. self.saveTableActivityImage(entries : dataActivityImage)
  106. self.saveTableActivityTag(entries : dataActivityTag)
  107. self.saveTableActivityItinerary(entries : dataActivityItinerary)
  108. self.saveTablePost(entries: dataPost)
  109. self.saveTablePostComment(entries: dataPostComment)
  110. self.saveTablePostImage(entries: dataPostImage)
  111. self.saveTablePostTag(entries: dataPostTag)
  112. self.saveTablePlace(entries: dataPlace)
  113. self.saveTablePeople(entries: dataPeople)
  114. self.saveTableAlbum(entries: dataAlbum)
  115. self.saveTablePhoto(entries: dataPhoto)
  116. self.saveTablePhotoAlbum(entries: dataPhotoAlbum)
  117. self.saveTableFestival(entries: dataFestival)
  118. self.saveTableFestivalDay(entries: dataFestivalDay)
  119. self.saveTableFestivalOffer(entries: dataFestivalOffer)
  120. self.saveTableFestivalEvent(entries: dataFestivalEvent)
  121. self.saveTableSponsor(entries: dataSponsor)
  122. self.saveSettings(entries: dataSettings)
  123. // If it's the initial sync, hide the segue
  124. if self.initial == true{
  125. NSLog(":SYNC:LOG: Finishing synchronous sync.")
  126. let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
  127. delegate.controller?.initialSync(showScreen: false)
  128. }
  129. }
  130. task.resume()
  131. }
  132. /**
  133. Extracts table rows from the raw data received.
  134. :param: param Received data in JSON format.
  135. :param: table The name of the table to look for.
  136. :return: Array of strings containing the rows of the table, in JSON format.
  137. */
  138. func getTable(data: String, table: String) -> [String]{
  139. //Does the table exists?
  140. if data.indexOf(target: "\"\(table)\":[") == nil{
  141. NSLog(":SYNC:LOG: Empty table: \(table).")
  142. return [String]()
  143. }
  144. let iS : Int? = data.indexOf(target: "\"\(table)\":[")
  145. var str = data.subStr(start: iS!, end: data.length - 1)
  146. let iE : Int? = str.indexOf(target: "}]}")!
  147. str = str.subStr(start: 0, end: iE!)
  148. str = str.subStr(start: table.length + 3,end: str.length - 1)
  149. //Separate the entries
  150. var entries : [String] = str.components(separatedBy: "},{")
  151. //Modify the first and the last one to remove bracers
  152. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  153. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  154. NSLog(":SYNC:LOG: Table \(table) has \(entries.count) rows.")
  155. //Return the array
  156. return entries
  157. }
  158. /**
  159. Saves the data in the table.
  160. :param: entries Array of strings containing the rows of the table, in JSON format.
  161. */
  162. func saveTablePlace(entries : [String]){
  163. NSLog(":SYNC:LOG: Saving table Place.")
  164. //Set up context
  165. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  166. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  167. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  168. let entity = NSEntityDescription.entity(forEntityName: "Place", in: context)
  169. var row: NSManagedObject
  170. //Delete all previous entries
  171. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Place")
  172. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  173. do {
  174. try context.execute(request)
  175. } catch let error as NSError {
  176. NSLog(":SYNC:ERROR: Could not clean up Places entity: \(error), \(error.userInfo).")
  177. } catch {
  178. NSLog(":SYNC:ERROR: Could not clean up Places entity.")
  179. }
  180. //Loop new entries
  181. for entry in entries{
  182. //Get id
  183. var str = entry
  184. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  185. //Get name_es
  186. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  187. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  188. //Get name_en
  189. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  190. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  191. //Get name_eu
  192. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  193. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  194. //Get address_es
  195. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  196. let address_es : String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  197. //Get address_en
  198. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  199. let address_en : String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  200. //Get address_eu
  201. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  202. let address_eu : String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  203. //Get cp
  204. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  205. let cp : String = str.subStr(start : str.indexOf(target : "\"cp\":")! + 6, end : str.indexOf(target : ",\"")! - 2)
  206. //Get lat
  207. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  208. let lat : Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  209. //Get lon
  210. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  211. let lon : Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  212. //Save CoreData
  213. row = NSManagedObject(entity: entity!, insertInto: context)
  214. row.setValue(id, forKey: "id")
  215. row.setValue(name_es, forKey: "name_es")
  216. row.setValue(name_en, forKey: "name_en")
  217. row.setValue(name_eu, forKey: "name_eu")
  218. row.setValue(address_es, forKey: "address_es")
  219. row.setValue(address_en, forKey: "address_en")
  220. row.setValue(address_eu, forKey: "address_eu")
  221. row.setValue(cp, forKey: "cp")
  222. row.setValue(lat, forKey: "lat")
  223. row.setValue(lon, forKey: "lon")
  224. do {
  225. try context.save()
  226. } catch let error as NSError {
  227. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  228. } catch {
  229. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  230. }
  231. }
  232. }
  233. /**
  234. Saves the data in the table.
  235. :param: entries Array of strings containing the rows of the table, in JSON format.
  236. */
  237. func saveTablePeople(entries : [String]){
  238. NSLog(":SYNC:LOG: Saving table People.")
  239. //Set up context
  240. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  241. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  242. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  243. let entity = NSEntityDescription.entity(forEntityName: "People", in: context)
  244. var row: NSManagedObject
  245. //Delete all previous entries
  246. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "People")
  247. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  248. do {
  249. try context.execute(request)
  250. } catch let error as NSError {
  251. NSLog(":SYNC:ERROR: Could not clean up People entity: \(error), \(error.userInfo).")
  252. } catch {
  253. NSLog(":SYNC:ERROR: Could not clean up People entity.")
  254. }
  255. //Loop new entries
  256. for entry in entries{
  257. row = NSManagedObject(entity: entity!, insertInto: context)
  258. //Get id
  259. var str = entry
  260. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  261. row.setValue(id, forKey: "id")
  262. //Get name_es
  263. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  264. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  265. row.setValue(name_es, forKey: "name_es")
  266. //Get name_en
  267. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  268. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  269. row.setValue(name_en, forKey: "name_en")
  270. //Get name_eu
  271. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  272. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  273. row.setValue(name_eu, forKey: "name_eu")
  274. //Get link
  275. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  276. let link : String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.length - 2)
  277. row.setValue(link, forKey: "link")
  278. //Save CoreData
  279. do {
  280. try context.save()
  281. } catch let error as NSError {
  282. NSLog(":SYNC:ERROR: Could not store Place with id \(id): \(error), \(error.userInfo).")
  283. } catch {
  284. NSLog(":SYNC:ERROR: Could not store Place with id \(id).")
  285. }
  286. }
  287. }
  288. /**
  289. Saves the data in the table.
  290. :param: entries Array of strings containing the rows of the table, in JSON format.
  291. */
  292. func saveTablePost(entries : [String]){
  293. NSLog(":SYNC:LOG: Saving table Post.")
  294. let dateFormatter = DateFormatter()
  295. dateFormatter.timeZone = TimeZone.ReferenceType.local
  296. //Set up context
  297. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  298. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  299. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  300. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  301. var row: NSManagedObject
  302. //Delete all previous entries
  303. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  304. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  305. do {
  306. try context.execute(request)
  307. } catch let error as NSError {
  308. NSLog(":SYNC:ERROR: Could not clean up Post entity: \(error), \(error.userInfo).")
  309. } catch {
  310. NSLog(":SYNC:ERROR: Could not clean up Post entity.")
  311. }
  312. //Loop new entries
  313. for entry in entries{
  314. //Get id
  315. var str = entry
  316. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  317. //Get permalink
  318. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  319. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  320. //Get title_es
  321. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  322. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  323. //Get title_en
  324. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  325. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  326. //Get title_eu
  327. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  328. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  329. //Get text_es
  330. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  331. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  332. //Get text_en
  333. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  334. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  335. //Get text_eu
  336. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  337. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  338. //Get comments
  339. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  340. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  341. //Get username
  342. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  343. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  344. //Get dtime
  345. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  346. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  347. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  348. //Save CoreData
  349. row = NSManagedObject(entity: entity!, insertInto: context)
  350. //set the entity values
  351. row.setValue(id, forKey: "id")
  352. row.setValue(permalink, forKey: "permalink")
  353. row.setValue(dtime, forKey: "dtime")
  354. row.setValue(username, forKey: "username")
  355. row.setValue(comments, forKey: "comments")
  356. row.setValue(title_es, forKey: "title_es")
  357. row.setValue(title_en, forKey: "title_en")
  358. row.setValue(title_eu, forKey: "title_eu")
  359. row.setValue(text_es, forKey: "text_es")
  360. row.setValue(text_en, forKey: "text_en")
  361. row.setValue(text_eu, forKey: "text_eu")
  362. do {
  363. try context.save()
  364. } catch let error as NSError {
  365. NSLog(":SYNC:ERROR: Could not store Post with id \(id): \(error), \(error.userInfo).")
  366. } catch {
  367. NSLog(":SYNC:ERROR: Could not store Post with id \(id).")
  368. }
  369. }
  370. }
  371. /**
  372. Saves the data in the table.
  373. :param: entries Array of strings containing the rows of the table, in JSON format.
  374. */
  375. func saveTablePostComment(entries : [String]){
  376. NSLog(":SYNC:LOG: Saving table Post_comment.")
  377. let dateFormatter = DateFormatter()
  378. dateFormatter.timeZone = TimeZone.ReferenceType.local
  379. //Set up context
  380. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  381. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  382. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  383. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  384. var row: NSManagedObject
  385. //Delete all previous entries
  386. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  387. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  388. do {
  389. try context.execute(request)
  390. } catch let error as NSError {
  391. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  392. } catch {
  393. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  394. }
  395. //Loop new entries
  396. for entry in entries{
  397. //Get id
  398. var str = entry
  399. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  400. //Get activity
  401. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  402. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  403. //Get text
  404. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  405. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  406. //Get dtime
  407. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  408. dateFormatter.dateFormat = "yyyy-MM-dd"
  409. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  410. //Get username
  411. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  412. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  413. //Get lang
  414. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  415. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  416. //Save CoreData
  417. row = NSManagedObject(entity: entity!, insertInto: context)
  418. row.setValue(id, forKey: "id")
  419. row.setValue(post, forKey: "post")
  420. row.setValue(text, forKey: "text")
  421. row.setValue(dtime, forKey: "dtime")
  422. row.setValue(username, forKey: "username")
  423. row.setValue(lang, forKey: "lang")
  424. do {
  425. try context.save()
  426. } catch let error as NSError {
  427. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  428. } catch {
  429. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  430. }
  431. }
  432. }
  433. /**
  434. Saves the data in the table.
  435. :param: entries Array of strings containing the rows of the table, in JSON format.
  436. */
  437. func saveTablePostImage(entries : [String]){
  438. NSLog(":SYNC:LOG: Saving table Post_image.")
  439. //Set up context
  440. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  441. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  442. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  443. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  444. var row: NSManagedObject
  445. //Delete all previous entries
  446. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  447. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  448. do {
  449. try context.execute(request)
  450. } catch let error as NSError {
  451. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity: \(error), \(error.userInfo).")
  452. } catch {
  453. NSLog(":SYNC:ERROR: Could not clean up Post_comment entity.")
  454. }
  455. //Loop new entries
  456. for entry in entries{
  457. //Get id
  458. var str = entry
  459. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  460. //Get activity
  461. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  462. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  463. //Get image
  464. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  465. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  466. //Get idx
  467. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  468. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  469. //Save CoreData
  470. row = NSManagedObject(entity: entity!, insertInto: context)
  471. row.setValue(id, forKey: "id")
  472. row.setValue(post, forKey: "post")
  473. row.setValue(image, forKey: "image")
  474. row.setValue(idx, forKey: "idx")
  475. do {
  476. try context.save()
  477. } catch let error as NSError {
  478. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id): \(error), \(error.userInfo).")
  479. } catch {
  480. NSLog(":SYNC:ERROR: Could not store Post_image with id \(id).")
  481. }
  482. }
  483. }
  484. /**
  485. Saves the data in the table.
  486. :param: entries Array of strings containing the rows of the table, in JSON format.
  487. */
  488. func saveTablePostTag(entries : [String]){
  489. NSLog(":SYNC:LOG: Saving table Post_tag.")
  490. //Set up context
  491. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  492. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  493. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  494. let entity = NSEntityDescription.entity(forEntityName: "Post_tag", in: context)
  495. var row: NSManagedObject
  496. //Delete all previous entries
  497. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  498. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  499. do {
  500. try context.execute(request)
  501. } catch let error as NSError {
  502. NSLog(":SYNC:ERROR: Could not clean up Post_tag entity: \(error), \(error.userInfo).")
  503. } catch {
  504. NSLog(":SYNC:ERROR: Could not clean up Post_tag entity.")
  505. }
  506. //Loop new entries
  507. for entry in entries{
  508. //Get activity
  509. var str = entry
  510. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  511. //Get tag
  512. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  513. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  514. //Save CoreData
  515. row = NSManagedObject(entity: entity!, insertInto: context)
  516. row.setValue(post, forKey: "post")
  517. row.setValue(tag, forKey: "tag")
  518. do {
  519. try context.save()
  520. } catch let error as NSError {
  521. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag): \(error), \(error.userInfo).")
  522. } catch {
  523. NSLog(":SYNC:ERROR: Could not store Post_tag for post: \(post), tag: \(tag).")
  524. }
  525. }
  526. }
  527. /**
  528. Saves the data in the table.
  529. :param: entries Array of strings containing the rows of the table, in JSON format.
  530. */
  531. func saveTableActivity(entries : [String]){
  532. NSLog(":SYNC:LOG: Saving table Activity.")
  533. let dateFormatter = DateFormatter()
  534. dateFormatter.timeZone = TimeZone.ReferenceType.local
  535. //Set up context
  536. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  537. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  538. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  539. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  540. var row: NSManagedObject
  541. //Delete all previous entries
  542. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  543. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  544. do {
  545. try context.execute(request)
  546. } catch let error as NSError {
  547. NSLog(":SYNC:ERROR: Could not clean up Activity entity: \(error), \(error.userInfo).")
  548. } catch {
  549. NSLog(":SYNC:ERROR: Could not clean up Activity entity.")
  550. }
  551. //Loop new entries
  552. for entry in entries{
  553. //Get id
  554. var str = entry
  555. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  556. //Get permalink
  557. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  558. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  559. //Get date
  560. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  561. dateFormatter.dateFormat = "yyyy-MM-dd"
  562. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  563. //Get city
  564. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  565. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  566. //Get title_es
  567. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  568. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  569. //Get title_en
  570. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  571. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  572. //Get title_eu
  573. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  574. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  575. //Get text_es
  576. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  577. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  578. //Get text_eu
  579. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  580. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  581. //Get text_en
  582. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  583. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  584. //Get after_es
  585. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  586. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  587. if (after_es == "ul"){ //From "null"
  588. after_es = ""
  589. }
  590. //Get after_en
  591. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  592. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  593. if (after_en == "ul"){ //From "null"
  594. after_en = ""
  595. }
  596. //Get after_eu
  597. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  598. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  599. if (after_eu == "ul"){ //From "null"
  600. after_eu = ""
  601. }
  602. //Get price
  603. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  604. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  605. //Get inscription
  606. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  607. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  608. //Get max_people
  609. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  610. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  611. var maxPeople = -1
  612. if (maxPeoplePre != "ul"){ //From "null"
  613. maxPeople = Int(maxPeoplePre)!
  614. }
  615. //Get album
  616. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  617. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  618. var album = -1
  619. if (albumPre != "ul"){ //From "null"
  620. album = Int(albumPre)!
  621. }
  622. //Skipping dtime
  623. //Skipping comments
  624. //Save CoreData
  625. row = NSManagedObject(entity: entity!, insertInto: context)
  626. //set the entity values
  627. row.setValue(id, forKey: "id")
  628. row.setValue(permalink, forKey: "permalink")
  629. row.setValue(date, forKey: "date")
  630. row.setValue(city, forKey: "city")
  631. row.setValue(title_es, forKey: "title_es")
  632. row.setValue(title_en, forKey: "title_en")
  633. row.setValue(title_eu, forKey: "title_eu")
  634. row.setValue(text_es, forKey: "text_es")
  635. row.setValue(text_en, forKey: "text_en")
  636. row.setValue(text_eu, forKey: "text_eu")
  637. row.setValue(after_es, forKey: "after_es")
  638. row.setValue(after_en, forKey: "after_en")
  639. row.setValue(after_eu, forKey: "after_eu")
  640. row.setValue(price, forKey: "price")
  641. row.setValue(inscription, forKey: "inscription")
  642. row.setValue(maxPeople, forKey: "max_people")
  643. if (album != -1){
  644. row.setValue(album, forKey: "album")
  645. }
  646. do {
  647. try context.save()
  648. } catch let error as NSError {
  649. NSLog(":SYNC:ERROR: Could not store Activity with id \(id): \(error), \(error.userInfo).")
  650. } catch {
  651. NSLog(":SYNC:ERROR: Could not store Activity with id \(id).")
  652. }
  653. }
  654. }
  655. /**
  656. Saves the data in the table.
  657. :param: entries Array of strings containing the rows of the table, in JSON format.
  658. */
  659. func saveTableActivityComment(entries : [String]){
  660. NSLog(":SYNC:LOG: Saving table Activity_comment.")
  661. let dateFormatter = DateFormatter()
  662. dateFormatter.timeZone = TimeZone.ReferenceType.local
  663. //Set up context
  664. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  665. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  666. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  667. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  668. var row: NSManagedObject
  669. //Delete all previous entries
  670. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  671. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  672. do {
  673. try context.execute(request)
  674. } catch let error as NSError {
  675. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  676. } catch {
  677. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  678. }
  679. //Loop new entries
  680. for entry in entries{
  681. //Get id
  682. var str = entry
  683. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  684. //Get activity
  685. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  686. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  687. //Get text
  688. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  689. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  690. //Get dtime
  691. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  692. dateFormatter.dateFormat = "yyyy-MM-dd"
  693. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  694. //Get username
  695. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  696. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  697. //Get lang
  698. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  699. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  700. //Save CoreData
  701. row = NSManagedObject(entity: entity!, insertInto: context)
  702. row.setValue(id, forKey: "id")
  703. row.setValue(activity, forKey: "activity")
  704. row.setValue(text, forKey: "text")
  705. row.setValue(dtime, forKey: "dtime")
  706. row.setValue(username, forKey: "username")
  707. row.setValue(lang, forKey: "lang")
  708. do {
  709. try context.save()
  710. } catch let error as NSError {
  711. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id): \(error), \(error.userInfo).")
  712. } catch {
  713. NSLog(":SYNC:ERROR: Could not store Activity_comment with id \(id).")
  714. }
  715. }
  716. }
  717. /**
  718. Saves the data in the table.
  719. :param: entries Array of strings containing the rows of the table, in JSON format.
  720. */
  721. func saveTableActivityImage(entries : [String]){
  722. NSLog(":SYNC:LOG: Saving table Activity_image.")
  723. //Set up context
  724. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  725. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  726. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  727. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  728. var row: NSManagedObject
  729. //Delete all previous entries
  730. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  731. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  732. do {
  733. try context.execute(request)
  734. } catch let error as NSError {
  735. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity: \(error), \(error.userInfo).")
  736. } catch {
  737. NSLog(":SYNC:ERROR: Could not clean up Activity_comment entity.")
  738. }
  739. //Loop new entries
  740. for entry in entries{
  741. //Get id
  742. var str = entry
  743. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  744. //Get activity
  745. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  746. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  747. //Get image
  748. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  749. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  750. //Get idx
  751. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  752. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  753. //Save CoreData
  754. row = NSManagedObject(entity: entity!, insertInto: context)
  755. row.setValue(id, forKey: "id")
  756. row.setValue(activity, forKey: "activity")
  757. row.setValue(image, forKey: "image")
  758. row.setValue(idx, forKey: "idx")
  759. do {
  760. try context.save()
  761. } catch let error as NSError {
  762. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id): \(error), \(error.userInfo).")
  763. } catch {
  764. NSLog(":SYNC:ERROR: Could not store Activity_image with id \(id).")
  765. }
  766. }
  767. }
  768. /**
  769. Saves the data in the table.
  770. :param: entries Array of strings containing the rows of the table, in JSON format.
  771. */
  772. func saveTableActivityTag(entries : [String]){
  773. NSLog(":SYNC:LOG: Saving table Activity_tag.")
  774. //Set up context
  775. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  776. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  777. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  778. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  779. var row: NSManagedObject
  780. //Delete all previous entries
  781. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  782. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  783. do {
  784. try context.execute(request)
  785. } catch let error as NSError {
  786. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity: \(error), \(error.userInfo).")
  787. } catch {
  788. NSLog(":SYNC:ERROR: Could not clean up Activity_tag entity.")
  789. }
  790. //Loop new entries
  791. for entry in entries{
  792. //Get activity
  793. var str = entry
  794. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  795. //Get tag
  796. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  797. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  798. //Save CoreData
  799. row = NSManagedObject(entity: entity!, insertInto: context)
  800. row.setValue(activity, forKey: "activity")
  801. row.setValue(tag, forKey: "tag")
  802. do {
  803. try context.save()
  804. } catch let error as NSError {
  805. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag): Error: \(error), \(error.userInfo).")
  806. } catch {
  807. NSLog(":SYNC:ERROR: Could not store Activity_tag for activity \(activity), tag: \(tag).")
  808. }
  809. }
  810. }
  811. /**
  812. Saves the data in the table.
  813. :param: entries Array of strings containing the rows of the table, in JSON format.
  814. */
  815. //CHECK: New (and probably better format than the others.)
  816. func saveTableActivityItinerary(entries : [String]){
  817. NSLog(":SYNC:LOG: Saving table Activity_itinerary.")
  818. let dateFormatter = DateFormatter()
  819. dateFormatter.timeZone = TimeZone.ReferenceType.local
  820. //Set up context
  821. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  822. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  823. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  824. let entity = NSEntityDescription.entity(forEntityName: "Activity_itinerary", in: context)
  825. var row: NSManagedObject
  826. //Delete all previous entries
  827. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_itinerary")
  828. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  829. do {
  830. try context.execute(request)
  831. } catch let error as NSError {
  832. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity: \(error), \(error.userInfo).")
  833. } catch {
  834. NSLog(":SYNC:ERROR: Could not clean up Activity_itinerary entity.")
  835. }
  836. //Loop new entries
  837. for entry in entries{
  838. row = NSManagedObject(entity: entity!, insertInto: context)
  839. //Get id
  840. var str = entry
  841. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  842. row.setValue(id, forKey: "id")
  843. //Get activity
  844. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  845. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  846. row.setValue(activity, forKey: "activity")
  847. //Get name_es
  848. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  849. let name_es : String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  850. row.setValue(name_es, forKey: "name_es")
  851. //Get name_en
  852. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  853. let name_en : String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  854. row.setValue(name_en, forKey: "name_en")
  855. //Get name_eu
  856. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  857. let name_eu : String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  858. row.setValue(name_eu, forKey: "name_eu")
  859. //Get description_es
  860. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  861. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  862. if (description_es != "ul"){ //From "null"
  863. row.setValue(description_es, forKey: "description_es")
  864. }
  865. //Get description_en
  866. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  867. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  868. if (description_en != "ul"){ //From "null"
  869. row.setValue(description_en, forKey: "description_en")
  870. }
  871. //Get description_eu
  872. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  873. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  874. if (description_eu != "ul"){ //From "null"
  875. row.setValue(description_eu, forKey: "description_eu")
  876. }
  877. //Get start
  878. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  879. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  880. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  881. row.setValue(start, forKey: "start")
  882. //Get end
  883. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  884. let end = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  885. if (end != "ul"){ //From "null"
  886. row.setValue(dateFormatter.date(from: end), forKey: "end")
  887. }
  888. //Get place
  889. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  890. let place : Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.length - 2))!
  891. row.setValue(place, forKey: "place")
  892. //Save CoreData
  893. do {
  894. try context.save()
  895. } catch let error as NSError {
  896. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id): \(error), \(error.userInfo).")
  897. } catch {
  898. NSLog(":SYNC:ERROR: Could not store Activity_itinerary with id \(id).")
  899. }
  900. }
  901. }
  902. /**
  903. Saves the data in the table.
  904. :param: entries Array of strings containing the rows of the table, in JSON format.
  905. */
  906. func saveTableAlbum(entries : [String]){
  907. NSLog(":SYNC:LOG: Saving table Album.")
  908. //Set up context
  909. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  910. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  911. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  912. let entity = NSEntityDescription.entity(forEntityName: "Album", in: context)
  913. var row: NSManagedObject
  914. //Delete all previous entries
  915. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Album")
  916. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  917. do {
  918. try context.execute(request)
  919. } catch let error as NSError {
  920. NSLog(":SYNC:ERROR: Could not clean up Album: \(error), \(error.userInfo).")
  921. } catch {
  922. NSLog(":SYNC:ERROR: Could not clean up Album entity.")
  923. }
  924. //Loop new entries
  925. for entry in entries{
  926. row = NSManagedObject(entity: entity!, insertInto: context)
  927. //Get id
  928. var str = entry
  929. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  930. row.setValue(id, forKey: "id")
  931. //Get permalink
  932. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  933. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  934. row.setValue(permalink, forKey: "permalink")
  935. //Get title_es
  936. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  937. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  938. row.setValue(title_es, forKey: "title_es")
  939. //Get title_en
  940. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  941. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  942. row.setValue(title_en, forKey: "title_en")
  943. //Get title_eu
  944. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  945. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  946. row.setValue(title_eu, forKey: "title_eu")
  947. //Get description_es
  948. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  949. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  950. if (description_es != "ul"){ //From "null"
  951. row.setValue(description_es, forKey: "description_es")
  952. }
  953. //Get description_en
  954. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  955. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  956. if (description_en != "ul"){ //From "null"
  957. row.setValue(description_en, forKey: "description_en")
  958. }
  959. //Get description_eu
  960. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  961. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  962. if (description_eu != "ul"){ //From "null"
  963. row.setValue(description_eu, forKey: "description_eu")
  964. }
  965. //Get open
  966. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  967. let open : Int = Int(str.subStr(start : str.indexOf(target : "\"open\":")! + 8, end : str.length - 2))!
  968. row.setValue(open, forKey: "open")
  969. //Save CoreData
  970. do {
  971. try context.save()
  972. } catch let error as NSError {
  973. NSLog(":SYNC:ERROR: Could not store Album with id \(id): \(error), \(error.userInfo).")
  974. } catch {
  975. NSLog(":SYNC:ERROR: Could not store Album with id \(id).")
  976. }
  977. }
  978. }
  979. /**
  980. Saves the data in the table.
  981. :param: entries Array of strings containing the rows of the table, in JSON format.
  982. */
  983. func saveTablePhoto(entries : [String]){
  984. NSLog(":SYNC:LOG: Saving table Photo.")
  985. let dateFormatter = DateFormatter()
  986. dateFormatter.timeZone = TimeZone.ReferenceType.local
  987. //Set up context
  988. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  989. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  990. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  991. let entity = NSEntityDescription.entity(forEntityName: "Photo", in: context)
  992. var row: NSManagedObject
  993. //Delete all previous entries
  994. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo")
  995. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  996. do {
  997. try context.execute(request)
  998. } catch let error as NSError {
  999. NSLog(":SYNC:ERROR: Could not clean up Photo: \(error), \(error.userInfo).")
  1000. } catch {
  1001. NSLog(":SYNC:ERROR: Could not clean up Photo entity.")
  1002. }
  1003. //Loop new entries
  1004. for entry in entries{
  1005. row = NSManagedObject(entity: entity!, insertInto: context)
  1006. //Get id
  1007. var str = entry
  1008. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1009. row.setValue(id, forKey: "id")
  1010. //Get file
  1011. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1012. let file : String = str.subStr(start : str.indexOf(target : "\"file\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1013. row.setValue(file, forKey: "file")
  1014. //Get permalink
  1015. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1016. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  1017. row.setValue(permalink, forKey: "permalink")
  1018. //Get title_es
  1019. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1020. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1021. row.setValue(title_es, forKey: "title_es")
  1022. //Get title_en
  1023. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1024. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1025. row.setValue(title_en, forKey: "title_en")
  1026. //Get title_eu
  1027. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1028. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1029. row.setValue(title_eu, forKey: "title_eu")
  1030. //Get description_es
  1031. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1032. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1033. if (description_es != "ul"){ //From "null"
  1034. row.setValue(description_es, forKey: "description_es")
  1035. }
  1036. //Get description_en
  1037. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1038. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1039. if (description_en != "ul"){ //From "null"
  1040. row.setValue(description_en, forKey: "description_en")
  1041. }
  1042. //Get description_eu
  1043. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1044. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1045. if (description_eu != "ul"){ //From "null"
  1046. row.setValue(description_eu, forKey: "description_eu")
  1047. }
  1048. //Get uploaded
  1049. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1050. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1051. let uploaded = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"uploaded\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  1052. row.setValue(uploaded, forKey: "uploaded")
  1053. //TODO: Ignoting place
  1054. //TODO: Ignoring width
  1055. //TODO: Ignoring height
  1056. //TODO: Ignoring size
  1057. //TODO: Ignoring username
  1058. //Save CoreData
  1059. do {
  1060. try context.save()
  1061. } catch let error as NSError {
  1062. NSLog(":SYNC:ERROR: Could not store Photo with id \(id): \(error), \(error.userInfo).")
  1063. } catch {
  1064. NSLog(":SYNC:ERROR: Could not store Photo with id \(id).")
  1065. }
  1066. }
  1067. }
  1068. /**
  1069. Saves the data in the table.
  1070. :param: entries Array of strings containing the rows of the table, in JSON format.
  1071. */
  1072. func saveTablePhotoAlbum(entries : [String]){
  1073. NSLog(":SYNC:LOG: Saving table Photo_album.")
  1074. let dateFormatter = DateFormatter()
  1075. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1076. //Set up context
  1077. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1078. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1079. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1080. let entity = NSEntityDescription.entity(forEntityName: "Photo_album", in: context)
  1081. var row: NSManagedObject
  1082. //Delete all previous entries
  1083. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Photo_album")
  1084. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1085. do {
  1086. try context.execute(request)
  1087. } catch let error as NSError {
  1088. NSLog(":SYNC:ERROR: Could not clean up Photo_album: \(error), \(error.userInfo).")
  1089. } catch {
  1090. NSLog(":SYNC:ERROR: Could not clean up Photo_album entity.")
  1091. }
  1092. //Loop new entries
  1093. for entry in entries{
  1094. row = NSManagedObject(entity: entity!, insertInto: context)
  1095. //Get photo
  1096. var str = entry
  1097. let photo : Int = Int(str.subStr(start : str.indexOf(target : "\"photo\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1098. row.setValue(photo, forKey: "photo")
  1099. //Get album
  1100. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1101. let album : Int = Int(str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2))!
  1102. row.setValue(album, forKey: "album")
  1103. //Save CoreData
  1104. do {
  1105. try context.save()
  1106. } catch let error as NSError {
  1107. NSLog(":SYNC:ERROR: Could not store Photo_album entry \(error.userInfo).")
  1108. } catch {
  1109. NSLog(":SYNC:ERROR: Could not store Photo_album entry.")
  1110. }
  1111. }
  1112. }
  1113. /**
  1114. Saves the data in the table.
  1115. :param: entries Array of strings containing the rows of the table, in JSON format.
  1116. */
  1117. func saveTableFestival(entries : [String]){
  1118. NSLog(":SYNC:LOG: Saving table Festival.")
  1119. //Set up context
  1120. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1121. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1122. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1123. let entity = NSEntityDescription.entity(forEntityName: "Festival", in: context)
  1124. var row: NSManagedObject
  1125. //Delete all previous entries
  1126. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival")
  1127. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1128. do {
  1129. try context.execute(request)
  1130. } catch let error as NSError {
  1131. NSLog(":SYNC:ERROR: Could not clean up Festival: \(error), \(error.userInfo).")
  1132. } catch {
  1133. NSLog(":SYNC:ERROR: Could not clean up Festival entity.")
  1134. }
  1135. //Loop new entries
  1136. for entry in entries{
  1137. row = NSManagedObject(entity: entity!, insertInto: context)
  1138. //Get id
  1139. var str = entry
  1140. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1141. row.setValue(id, forKey: "id")
  1142. //Get year
  1143. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1144. let file: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1145. row.setValue(file, forKey: "year")
  1146. //Get text_es
  1147. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1148. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1149. row.setValue(text_es, forKey: "text_es")
  1150. //Get text_en
  1151. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1152. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1153. row.setValue(text_en, forKey: "text_en")
  1154. //Get text_eu
  1155. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1156. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1157. row.setValue(text_eu, forKey: "text_eu")
  1158. //Get summary_es
  1159. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1160. let summary_es: String = str.subStr(start : str.indexOf(target : "\"summary_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1161. row.setValue(summary_es, forKey: "summary_es")
  1162. //Get summary_en
  1163. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1164. let summary_en: String = str.subStr(start : str.indexOf(target : "\"summary_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1165. row.setValue(summary_en, forKey: "summary_en")
  1166. //Get summary_eu
  1167. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1168. let summary_eu: String = str.subStr(start : str.indexOf(target : "\"summary_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1169. row.setValue(summary_eu, forKey: "summary_eu")
  1170. //Get img
  1171. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1172. let img: String = str.subStr(start : str.indexOf(target : "\"img\":")! + 7, end : str.length - 2)
  1173. row.setValue(img, forKey: "img")
  1174. //Save CoreData
  1175. do {
  1176. try context.save()
  1177. } catch let error as NSError {
  1178. NSLog(":SYNC:ERROR: Could not store Festival entry \(error.userInfo).")
  1179. } catch {
  1180. NSLog(":SYNC:ERROR: Could not store Festival entry.")
  1181. }
  1182. }
  1183. }
  1184. /**
  1185. Saves the data in the table.
  1186. :param: entries Array of strings containing the rows of the table, in JSON format.
  1187. */
  1188. func saveTableFestivalDay(entries : [String]){
  1189. NSLog(":SYNC:LOG: Saving table FestivalDay.")
  1190. let dateFormatter = DateFormatter()
  1191. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1192. //Set up context
  1193. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1194. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1195. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1196. let entity = NSEntityDescription.entity(forEntityName: "Festival_day", in: context)
  1197. var row: NSManagedObject
  1198. //Delete all previous entries
  1199. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_day")
  1200. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1201. do {
  1202. try context.execute(request)
  1203. } catch let error as NSError {
  1204. NSLog(":SYNC:ERROR: Could not clean up Festival_day: \(error), \(error.userInfo).")
  1205. } catch {
  1206. NSLog(":SYNC:ERROR: Could not clean up Festival_day entity.")
  1207. }
  1208. //Loop new entries
  1209. for entry in entries{
  1210. row = NSManagedObject(entity: entity!, insertInto: context)
  1211. //Get id
  1212. var str = entry
  1213. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1214. row.setValue(id, forKey: "id")
  1215. //Get date
  1216. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1217. dateFormatter.dateFormat = "yyyy-MM-dd"
  1218. let date = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1219. row.setValue(date, forKey: "date")
  1220. //Get name_es
  1221. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1222. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1223. row.setValue(name_es, forKey: "name_es")
  1224. //Get name_en
  1225. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1226. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1227. row.setValue(name_en, forKey: "name_en")
  1228. //Get name_eu
  1229. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1230. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1231. row.setValue(name_eu, forKey: "name_eu")
  1232. //Get price
  1233. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1234. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1235. row.setValue(price, forKey: "price")
  1236. //Save CoreData
  1237. do {
  1238. try context.save()
  1239. } catch let error as NSError {
  1240. NSLog(":SYNC:ERROR: Could not store Festival_day entry \(error.userInfo).")
  1241. } catch {
  1242. NSLog(":SYNC:ERROR: Could not store Festival_day entry.")
  1243. }
  1244. }
  1245. }
  1246. /**
  1247. Saves the data in the table.
  1248. :param: entries Array of strings containing the rows of the table, in JSON format.
  1249. */
  1250. func saveTableFestivalOffer(entries : [String]){
  1251. NSLog(":SYNC:LOG: Saving table FestivalOffer.")
  1252. //Set up context
  1253. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1254. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1255. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1256. let entity = NSEntityDescription.entity(forEntityName: "Festival_offer", in: context)
  1257. var row: NSManagedObject
  1258. //Delete all previous entries
  1259. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_offer")
  1260. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1261. do {
  1262. try context.execute(request)
  1263. } catch let error as NSError {
  1264. NSLog(":SYNC:ERROR: Could not clean up Festival_offer: \(error), \(error.userInfo).")
  1265. } catch {
  1266. NSLog(":SYNC:ERROR: Could not clean up Festival_offer entity.")
  1267. }
  1268. //Loop new entries
  1269. for entry in entries{
  1270. row = NSManagedObject(entity: entity!, insertInto: context)
  1271. // Get id
  1272. var str = entry
  1273. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1274. row.setValue(id, forKey: "id")
  1275. // Get year
  1276. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1277. let year: Int = Int(str.subStr(start : str.indexOf(target : "\"year\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1278. row.setValue(year, forKey: "year")
  1279. // Get name_es
  1280. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1281. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1282. row.setValue(name_es, forKey: "name_es")
  1283. // Get name_en
  1284. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1285. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1286. row.setValue(name_en, forKey: "name_en")
  1287. // Get name_eu
  1288. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1289. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1290. row.setValue(name_eu, forKey: "name_eu")
  1291. // Get description_es
  1292. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1293. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1294. if (description_es != "ul"){ //From "null"
  1295. row.setValue(description_es, forKey: "description_es")
  1296. }
  1297. // Get description_en
  1298. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1299. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1300. if (description_en != "ul"){ //From "null"
  1301. row.setValue(description_en, forKey: "description_en")
  1302. }
  1303. // Get description_eu
  1304. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1305. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1306. if (description_eu != "ul"){ //From "null"
  1307. row.setValue(description_eu, forKey: "description_eu")
  1308. }
  1309. // Get days
  1310. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1311. let days: Int = Int(str.subStr(start : str.indexOf(target : "\"days\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  1312. row.setValue(days, forKey: "days")
  1313. //Get price
  1314. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1315. let price: Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.length - 2))!
  1316. row.setValue(price, forKey: "price")
  1317. //Save CoreData
  1318. do {
  1319. try context.save()
  1320. } catch let error as NSError {
  1321. NSLog(":SYNC:ERROR: Could not store Festival_offer entry \(error.userInfo).")
  1322. } catch {
  1323. NSLog(":SYNC:ERROR: Could not store Festival_offer entry.")
  1324. }
  1325. }
  1326. }
  1327. /**
  1328. Saves the data in the table.
  1329. :param: entries Array of strings containing the rows of the table, in JSON format.
  1330. */
  1331. func saveTableFestivalEvent(entries : [String]){
  1332. NSLog(":SYNC:LOG: Saving table FestivalEvent.")
  1333. let dateFormatter = DateFormatter()
  1334. dateFormatter.timeZone = TimeZone.ReferenceType.local
  1335. //Set up context
  1336. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1337. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1338. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1339. let entity = NSEntityDescription.entity(forEntityName: "Festival_event", in: context)
  1340. var row: NSManagedObject
  1341. //Delete all previous entries
  1342. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Festival_event")
  1343. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1344. do {
  1345. try context.execute(request)
  1346. } catch let error as NSError {
  1347. NSLog(":SYNC:ERROR: Could not clean up Festival_event: \(error), \(error.userInfo).")
  1348. } catch {
  1349. NSLog(":SYNC:ERROR: Could not clean up Festival_event entity.")
  1350. }
  1351. //Loop new entries
  1352. for entry in entries{
  1353. row = NSManagedObject(entity: entity!, insertInto: context)
  1354. //Get id
  1355. var str = entry
  1356. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1357. row.setValue(id, forKey: "id")
  1358. //Get gm
  1359. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1360. let gm: Int = Int(str.subStr(start : str.indexOf(target : "\"gm\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1361. row.setValue(gm, forKey: "gm")
  1362. //Get title_es
  1363. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1364. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1365. row.setValue(title_es, forKey: "title_es")
  1366. //Get title_en
  1367. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1368. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1369. row.setValue(title_en, forKey: "title_en")
  1370. //Get title_eu
  1371. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1372. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  1373. row.setValue(title_eu, forKey: "title_eu")
  1374. //Get description_es
  1375. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1376. let description_es : String = str.subStr(start : str.indexOf(target : "\"description_es\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1377. if (description_es != "ul"){ //From "null"
  1378. row.setValue(description_es, forKey: "description_es")
  1379. }
  1380. //Get description_en
  1381. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1382. let description_en : String = str.subStr(start : str.indexOf(target : "\"description_en\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1383. if (description_en != "ul"){ //From "null"
  1384. row.setValue(description_en, forKey: "description_en")
  1385. }
  1386. //Get description_eu
  1387. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1388. let description_eu : String = str.subStr(start : str.indexOf(target : "\"description_eu\":")! + 18, end : str.indexOf(target : ",\"")! - 2)
  1389. if (description_eu != "ul"){ //From "null"
  1390. row.setValue(description_eu, forKey: "description_eu")
  1391. }
  1392. //Get host
  1393. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1394. let strHost: String = str.subStr(start : str.indexOf(target : "\"host\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1395. if (strHost != "ul"){ //From "null"
  1396. let host: Int = Int(strHost)!
  1397. row.setValue(host, forKey: "host")
  1398. }
  1399. //Get place
  1400. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1401. let place: Int = Int(str.subStr(start : str.indexOf(target : "\"place\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1402. row.setValue(place, forKey: "place")
  1403. //Get start
  1404. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1405. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1406. let start = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  1407. row.setValue(start, forKey: "start")
  1408. // Get day
  1409. // Special item, not in the sync content
  1410. dateFormatter.dateFormat = "yyyy-MM-dd"
  1411. var day = dateFormatter.date(from: str.subStr(start : str.indexOf(target : "\"start\":")! + 9, end : str.indexOf(target : ",\"")! - 11))!
  1412. // If on the first hours of the next day...
  1413. let calendar = Calendar.current
  1414. let hours = calendar.component(.hour, from: day )
  1415. // ... the event belongs to the previous day.
  1416. if hours < 6{
  1417. day = Calendar.current.date(byAdding: .day, value: -1, to: day)!
  1418. }
  1419. row.setValue(day, forKey: "day")
  1420. //Get end
  1421. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1422. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  1423. let strEnd: String = str.subStr(start : str.indexOf(target : "\"end\":")! + 7, end : str.length - 2)
  1424. if (strEnd != "ul"){ //From "null"
  1425. let end = dateFormatter.date(from: strEnd)!
  1426. row.setValue(end, forKey: "end")
  1427. }
  1428. //Save CoreData
  1429. do {
  1430. try context.save()
  1431. } catch let error as NSError {
  1432. NSLog(":SYNC:ERROR: Could not store Festival_event entry \(error.userInfo).")
  1433. } catch {
  1434. NSLog(":SYNC:ERROR: Could not store Festival_event entry.")
  1435. }
  1436. }
  1437. }
  1438. /**
  1439. Saves the data in the table.
  1440. :param: entries Array of strings containing the rows of the table, in JSON format.
  1441. */
  1442. func saveTableSponsor(entries : [String]){
  1443. NSLog(":SYNC:LOG: Saving table Sponsor.")
  1444. //Set up context
  1445. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  1446. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  1447. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  1448. let entity = NSEntityDescription.entity(forEntityName: "Sponsor", in: context)
  1449. var row: NSManagedObject
  1450. //Delete all previous entries
  1451. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Sponsor")
  1452. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  1453. do {
  1454. try context.execute(request)
  1455. } catch let error as NSError {
  1456. NSLog(":SYNC:ERROR: Could not clean up Sponsor: \(error), \(error.userInfo).")
  1457. } catch {
  1458. NSLog(":SYNC:ERROR: Could not clean up Sponsor entity.")
  1459. }
  1460. //Loop new entries
  1461. for entry in entries{
  1462. row = NSManagedObject(entity: entity!, insertInto: context)
  1463. //Get id
  1464. var str = entry
  1465. let id: Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  1466. row.setValue(id, forKey: "id")
  1467. // Get name_es
  1468. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1469. let name_es: String = str.subStr(start : str.indexOf(target : "\"name_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1470. row.setValue(name_es, forKey: "name_es")
  1471. // Get name_en
  1472. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1473. let name_en: String = str.subStr(start : str.indexOf(target : "\"name_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1474. row.setValue(name_en, forKey: "name_en")
  1475. // Get name_eu
  1476. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1477. let name_eu: String = str.subStr(start : str.indexOf(target : "\"name_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1478. row.setValue(name_eu, forKey: "name_eu")
  1479. // Get text_es
  1480. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1481. let text_es: String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1482. row.setValue(text_es, forKey: "text_es")
  1483. // Get text_en
  1484. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1485. let text_en: String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1486. row.setValue(text_en, forKey: "text_en")
  1487. // Get text_eu
  1488. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1489. let text_eu: String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  1490. row.setValue(text_eu, forKey: "text_eu")
  1491. // Get image
  1492. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1493. let image: String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  1494. if (image != "ul"){ //From "null"
  1495. row.setValue(image, forKey: "image")
  1496. }
  1497. // Get address_es
  1498. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1499. let address_es: String = str.subStr(start : str.indexOf(target : "\"address_es\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1500. if (address_es != "ul"){ //From "null"
  1501. row.setValue(address_es, forKey: "address_es")
  1502. }
  1503. // Get address_en
  1504. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1505. let address_en: String = str.subStr(start : str.indexOf(target : "\"address_en\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1506. if (address_en != "ul"){ //From "null"
  1507. row.setValue(address_en, forKey: "address_en")
  1508. }
  1509. // Get address_eu
  1510. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1511. let address_eu: String = str.subStr(start : str.indexOf(target : "\"address_eu\":")! + 14, end : str.indexOf(target : ",\"")! - 2)
  1512. if (address_eu != "ul"){ //From "null"
  1513. row.setValue(address_eu, forKey: "address_eu")
  1514. }
  1515. //Get link
  1516. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1517. let link: String = str.subStr(start : str.indexOf(target : "\"link\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1518. if (link != "ul"){ //From "null"
  1519. row.setValue(link, forKey: "link")
  1520. }
  1521. //Get lat
  1522. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1523. let strLat: String = str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2)
  1524. if (strLat != "ul"){ //From "null"
  1525. let lat: Float = Float(str.subStr(start : str.indexOf(target : "\"lat\":")! + 7, end : str.indexOf(target : ",\"")! - 2))!
  1526. row.setValue(lat, forKey: "lat")
  1527. }
  1528. //Get lon
  1529. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1530. let strLon: String = str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2)
  1531. if (strLon != "ul"){ //From "null"
  1532. let lon: Float = Float(str.subStr(start : str.indexOf(target : "\"lon\":")! + 7, end : str.length - 2))!
  1533. row.setValue(lon, forKey: "lon")
  1534. }
  1535. //Save CoreData
  1536. do {
  1537. try context.save()
  1538. } catch let error as NSError {
  1539. NSLog(":SYNC:ERROR: Could not store Sponsor entry \(error.userInfo).")
  1540. } catch {
  1541. NSLog(":SYNC:ERROR: Could not store Sponsor entry.")
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. Saves the received settings.
  1547. :param: entries Array of strings containing the rows of the table, in JSON format.
  1548. */
  1549. func saveSettings(entries : [String]){
  1550. NSLog(":SYNC:LOG: Saving settings.")
  1551. let defaults = UserDefaults.standard
  1552. // Loop new entries
  1553. for entry in entries{
  1554. // Get setting name
  1555. var str = entry
  1556. let name: String = str.subStr(start : str.indexOf(target : "\"name\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  1557. // Get setting value
  1558. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  1559. let value: Int = Int(str.subStr(start : str.indexOf(target : "\"value\":")! + 9, end : str.length - 2))!
  1560. // Save only the needed settings
  1561. if ["festivals", "comments", "photos"].contains(name){
  1562. NSLog(":SYNC:LOG: Setting \(name) \(value)")
  1563. defaults.set(value, forKey: name)
  1564. }
  1565. }
  1566. }
  1567. }