Sync.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. /**
  28. Starts the sync process.
  29. */
  30. init(){
  31. let url = buildUrl();
  32. sync(url: url)
  33. }
  34. /**
  35. Builds the URL to perform the sync against.
  36. :returns: The URL.
  37. */
  38. func buildUrl() -> URL{
  39. let url = URL(string: "https://margolariak.com/API/v1/sync.php?client=com.margolariak.app")
  40. //TODO add parameters
  41. return url!
  42. }
  43. /**
  44. Performs an asynchronous sync.
  45. It fethes the info from the server and stores as Core Data
  46. :param: url The url to sync.
  47. */
  48. func sync(url: URL){
  49. //Synchronously get data
  50. let task = URLSession.shared.dataTask(with: url) { data, response, error in
  51. guard error == nil else {
  52. print("Sync error")
  53. return
  54. }
  55. guard let data = data else {
  56. print("Data is empty")
  57. return
  58. }
  59. print("Got the Sync JSON")
  60. var strData = String(data:data, encoding: String.Encoding.utf8)
  61. let dataIdx = strData?.indexOf(target: "{\"data\"")
  62. strData = strData!.subStr(start: dataIdx!, end: strData!.length - 1)
  63. //TODO: do something with versions
  64. //Get tables
  65. let dataActivity : [String] = self.getTable(data: strData!, table: "activity")
  66. let dataActivityComment : [String] = self.getTable(data: strData!, table: "activity_comment")
  67. let dataActivityImage : [String] = self.getTable(data: strData!, table: "activity_image")
  68. let dataActivityTag : [String] = self.getTable(data: strData!, table: "activity_tag")
  69. //let dataAlbum : [String] = self.getTable(data: strData!, table: "album")
  70. //let dataFestival : [String] = self.getTable(data: strData!, table: "festival")
  71. //let dataFestivalDay : [String] = self.getTable(data: strData!, table: "festival_day")
  72. //let dataFestivalEvent : [String] = self.getTable(data: strData!, table: "festival_event")
  73. //let dataFestivalEventImage : [String] = self.getTable(data: strData!, table: "festival_event_image")
  74. //let dataFestivalOffer : [String] = self.getTable(data: strData!, table: "festival_offer")
  75. //let dataPeople : [String] = self.getTable(data: strData!, table: "people")
  76. //let dataPhoto : [String] = self.getTable(data: strData!, table: "photo")
  77. //let dataPhotoAlbum : [String] = self.getTable(data: strData!, table: "photo_album")
  78. //let dataPhotoComment : [String] = self.getTable(data: strData!, table: "photo_comment")
  79. //let dataPlace : [String] = self.getTable(data: strData!, table: "place")
  80. let dataPost : [String] = self.getTable(data: strData!, table: "post")
  81. //let dataPostComment : [String] = self.getTable(data: strData!, table: "post_comment")
  82. //let dataPostImage : [String] = self.getTable(data: strData!, table: "post_image")
  83. //let dataPostTag : [String] = self.getTable(data: strData!, table: "post_tag")
  84. //let dataSponsor : [String] = self.getTable(data: strData!, table: "sponsor")
  85. //One by one, save the received data
  86. self.saveTableActivity(entries : dataActivity)
  87. self.saveTableActivityComment(entries : dataActivityComment)
  88. self.saveTableActivityImage(entries : dataActivityImage)
  89. self.saveTableActivityTag(entries : dataActivityTag)
  90. self.saveTablePost(entries: dataPost)
  91. //TODO: Tester, Debug only
  92. // Test if a entity has entries
  93. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  94. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  95. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  96. let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
  97. do {
  98. //go get the results
  99. let searchResults = try context.fetch(fetchRequest)
  100. print ("Num of results = \(searchResults.count)")
  101. for r in searchResults as [NSManagedObject] {
  102. print("\(r.value(forKey: "id"))")
  103. }
  104. } catch {
  105. print("Error with request: \(error)")
  106. }
  107. //End tester
  108. }
  109. task.resume()
  110. }
  111. func getTable(data: String, table: String) -> [String]{
  112. //Does the table exists?
  113. if data.indexOf(target: "\"\(table)\"") == nil{
  114. print("No data in table \(table)")
  115. return [String]()
  116. }
  117. let iS : Int? = data.indexOf(target: "\"\(table)\"")
  118. var str = data.subStr(start: iS!, end: data.length - 1)
  119. let iE : Int? = str.indexOf(target: "}]}")!
  120. str = str.subStr(start: 0, end: iE!)
  121. str = str.subStr(start: table.length + 3,end: str.length - 1)
  122. //Separate the entries
  123. var entries : [String] = str.components(separatedBy: "},{")
  124. //Modify the first and the last one to remove bracers
  125. entries[0] = entries[0].subStr(start: 1, end: entries[0].length - 1)
  126. entries[entries.count - 1] = entries[entries.count - 1].subStr(start: 0, end: entries[entries.count - 1].length - 2)
  127. //Return the array
  128. return entries
  129. }
  130. func saveTablePost(entries : [String]){
  131. let dateFormatter = DateFormatter()
  132. dateFormatter.timeZone = TimeZone.ReferenceType.local
  133. //Set up context
  134. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  135. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  136. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  137. let entity = NSEntityDescription.entity(forEntityName: "Post", in: context)
  138. var row: NSManagedObject
  139. //Delete all previous entries
  140. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post")
  141. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  142. do {
  143. try context.execute(request)
  144. } catch let error as NSError {
  145. print("Could not clean up Post entity: \(error), \(error.userInfo)")
  146. } catch {
  147. }
  148. //Loop new entries
  149. for entry in entries{
  150. //Get id
  151. var str = entry
  152. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  153. //Get permalink
  154. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  155. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  156. //Get title_es
  157. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  158. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  159. //Get title_en
  160. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  161. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  162. //Get title_eu
  163. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  164. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  165. //Get text_es
  166. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  167. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  168. //Get text_en
  169. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  170. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  171. //Get text_eu
  172. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  173. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  174. //Get comments
  175. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  176. let comments : Int = Int(str.subStr(start : str.indexOf(target : "\"comments\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  177. //Get username
  178. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  179. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  180. //Get dtime
  181. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  182. dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  183. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.length - 2))!
  184. //Save CoreData
  185. row = NSManagedObject(entity: entity!, insertInto: context)
  186. //set the entity values
  187. row.setValue(id, forKey: "id")
  188. row.setValue(permalink, forKey: "permalink")
  189. row.setValue(dtime, forKey: "dtime")
  190. row.setValue(username, forKey: "username")
  191. row.setValue(comments, forKey: "comments")
  192. row.setValue(title_es, forKey: "title_es")
  193. row.setValue(title_en, forKey: "title_en")
  194. row.setValue(title_eu, forKey: "title_eu")
  195. row.setValue(text_es, forKey: "text_es")
  196. row.setValue(text_en, forKey: "text_en")
  197. row.setValue(text_eu, forKey: "text_eu")
  198. do {
  199. try context.save()
  200. } catch let error as NSError {
  201. print("Could not store Post with id \(id) \(error), \(error.userInfo)")
  202. } catch {
  203. }
  204. }
  205. }
  206. func saveTablePostComment(entries : [String]){
  207. let dateFormatter = DateFormatter()
  208. dateFormatter.timeZone = TimeZone.ReferenceType.local
  209. //Set up context
  210. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  211. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  212. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  213. let entity = NSEntityDescription.entity(forEntityName: "Post_comment", in: context)
  214. var row: NSManagedObject
  215. //Delete all previous entries
  216. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_comment")
  217. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  218. do {
  219. try context.execute(request)
  220. } catch let error as NSError {
  221. print("Could not clean up Post_comment entity: \(error), \(error.userInfo)")
  222. } catch {
  223. }
  224. //Loop new entries
  225. for entry in entries{
  226. //Get id
  227. var str = entry
  228. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  229. //Get activity
  230. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  231. let post : String = str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  232. //Get text
  233. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  234. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  235. //Get dtime
  236. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  237. dateFormatter.dateFormat = "yyyy-MM-dd"
  238. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  239. //Get username
  240. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  241. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  242. //Get lang
  243. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  244. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  245. //Save CoreData
  246. row = NSManagedObject(entity: entity!, insertInto: context)
  247. row.setValue(id, forKey: "id")
  248. row.setValue(post, forKey: "post")
  249. row.setValue(text, forKey: "text")
  250. row.setValue(dtime, forKey: "dtime")
  251. row.setValue(username, forKey: "username")
  252. row.setValue(lang, forKey: "lang")
  253. do {
  254. try context.save()
  255. } catch let error as NSError {
  256. print("Could not store Activity_comment with id \(id) \(error), \(error.userInfo)")
  257. } catch {
  258. }
  259. }
  260. }
  261. func saveTablePostImage(entries : [String]){
  262. //Set up context
  263. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  264. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  265. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  266. let entity = NSEntityDescription.entity(forEntityName: "Post_image", in: context)
  267. var row: NSManagedObject
  268. //Delete all previous entries
  269. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_image")
  270. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  271. do {
  272. try context.execute(request)
  273. } catch let error as NSError {
  274. print("Could not clean up Post_comment entity: \(error), \(error.userInfo)")
  275. } catch {
  276. }
  277. //Loop new entries
  278. for entry in entries{
  279. //Get id
  280. var str = entry
  281. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  282. //Get activity
  283. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  284. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  285. //Get image
  286. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  287. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  288. //Get idx
  289. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  290. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  291. //Save CoreData
  292. row = NSManagedObject(entity: entity!, insertInto: context)
  293. row.setValue(id, forKey: "id")
  294. row.setValue(post, forKey: "post")
  295. row.setValue(image, forKey: "image")
  296. row.setValue(idx, forKey: "idx")
  297. do {
  298. try context.save()
  299. } catch let error as NSError {
  300. print("Could not store Post_image with id \(id) \(error), \(error.userInfo)")
  301. } catch {
  302. }
  303. }
  304. }
  305. func saveTablePostTag(entries : [String]){
  306. //Set up context
  307. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  308. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  309. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  310. let entity = NSEntityDescription.entity(forEntityName: "Post_tag", in: context)
  311. var row: NSManagedObject
  312. //Delete all previous entries
  313. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Post_tag")
  314. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  315. do {
  316. try context.execute(request)
  317. } catch let error as NSError {
  318. print("Could not clean up Post_tag entity: \(error), \(error.userInfo)")
  319. } catch {
  320. }
  321. //Loop new entries
  322. for entry in entries{
  323. //Get activity
  324. var str = entry
  325. let post : Int = Int(str.subStr(start : str.indexOf(target : "\"post\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  326. //Get tag
  327. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  328. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  329. //Save CoreData
  330. row = NSManagedObject(entity: entity!, insertInto: context)
  331. row.setValue(post, forKey: "post")
  332. row.setValue(tag, forKey: "tag")
  333. do {
  334. try context.save()
  335. } catch let error as NSError {
  336. print("Could not store Post_tag for post: \(post), tag: \(tag). Error: \(error), \(error.userInfo)")
  337. } catch {
  338. }
  339. }
  340. }
  341. func saveTableActivity(entries : [String]){
  342. let dateFormatter = DateFormatter()
  343. dateFormatter.timeZone = TimeZone.ReferenceType.local
  344. //Set up context
  345. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  346. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  347. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  348. let entity = NSEntityDescription.entity(forEntityName: "Activity", in: context)
  349. var row: NSManagedObject
  350. //Delete all previous entries
  351. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity")
  352. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  353. do {
  354. try context.execute(request)
  355. } catch let error as NSError {
  356. print("Could not clean up Activity entity: \(error), \(error.userInfo)")
  357. } catch {
  358. }
  359. //Loop new entries
  360. for entry in entries{
  361. //Get id
  362. var str = entry
  363. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  364. //Get permalink
  365. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  366. let permalink : String = str.subStr(start : str.indexOf(target : "\"permalink\":")! + 13, end : str.indexOf(target : ",\"")! - 2)
  367. //Get date
  368. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  369. dateFormatter.dateFormat = "yyyy-MM-dd"
  370. let date = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"date\":")! + 8, end : str.indexOf(target : ",\"")! - 2))!
  371. //Get city
  372. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  373. let city : String = str.subStr(start : str.indexOf(target : "\"city\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  374. //Get title_es
  375. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  376. let title_es : String = str.subStr(start : str.indexOf(target : "\"title_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  377. //Get title_en
  378. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  379. let title_en : String = str.subStr(start : str.indexOf(target : "\"title_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  380. //Get title_eu
  381. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  382. let title_eu : String = str.subStr(start : str.indexOf(target : "\"title_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  383. //Get text_es
  384. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  385. let text_es : String = str.subStr(start : str.indexOf(target : "\"text_es\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  386. //Get text_eu
  387. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  388. let text_eu : String = str.subStr(start : str.indexOf(target : "\"text_eu\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  389. //Get text_en
  390. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  391. let text_en : String = str.subStr(start : str.indexOf(target : "\"text_en\":")! + 11, end : str.indexOf(target : ",\"")! - 2)
  392. //Get after_es
  393. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  394. var after_es : String = str.subStr(start : str.indexOf(target : "\"after_es\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  395. if (after_es == "ul"){ //From "null"
  396. after_es = ""
  397. }
  398. //Get after_en
  399. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  400. var after_en : String = str.subStr(start : str.indexOf(target : "\"after_en\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  401. if (after_en == "ul"){ //From "null"
  402. after_en = ""
  403. }
  404. //Get after_eu
  405. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  406. var after_eu : String = str.subStr(start : str.indexOf(target : "\"after_eu\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  407. if (after_eu == "ul"){ //From "null"
  408. after_eu = ""
  409. }
  410. //Get price
  411. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  412. let price : Int = Int(str.subStr(start : str.indexOf(target : "\"price\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  413. //Get inscription
  414. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  415. let inscription : Int = Int(str.subStr(start : str.indexOf(target : "\"inscription\":")! + 15, end : str.indexOf(target : ",\"")! - 2))!
  416. //Get max_people
  417. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  418. let maxPeoplePre : String = str.subStr(start : str.indexOf(target : "\"max_people\":")! + 14, end : str.indexOf(target : ",")! - 2)
  419. var maxPeople = -1
  420. if (maxPeoplePre != "ul"){ //From "null"
  421. maxPeople = Int(maxPeoplePre)!
  422. }
  423. //Get album
  424. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  425. let albumPre = str.subStr(start : str.indexOf(target : "\"album\":")! + 9, end : str.length - 2)
  426. var album = -1
  427. if (albumPre != "ul"){ //From "null"
  428. album = Int(albumPre)!
  429. }
  430. //Skipping dtime
  431. //Skipping comments
  432. //Save CoreData
  433. row = NSManagedObject(entity: entity!, insertInto: context)
  434. //set the entity values
  435. row.setValue(id, forKey: "id")
  436. row.setValue(permalink, forKey: "permalink")
  437. row.setValue(date, forKey: "date")
  438. row.setValue(city, forKey: "city")
  439. row.setValue(title_es, forKey: "title_es")
  440. row.setValue(title_en, forKey: "title_en")
  441. row.setValue(title_eu, forKey: "title_eu")
  442. row.setValue(text_es, forKey: "text_es")
  443. row.setValue(text_en, forKey: "text_en")
  444. row.setValue(text_eu, forKey: "text_eu")
  445. row.setValue(after_es, forKey: "after_es")
  446. row.setValue(after_en, forKey: "after_en")
  447. row.setValue(after_eu, forKey: "after_eu")
  448. row.setValue(price, forKey: "price")
  449. row.setValue(inscription, forKey: "inscription")
  450. row.setValue(maxPeople, forKey: "max_people")
  451. if (album != -1){
  452. row.setValue(album, forKey: "album")
  453. }
  454. do {
  455. try context.save()
  456. } catch let error as NSError {
  457. print("Could not store Activity with id \(id) \(error), \(error.userInfo)")
  458. } catch {
  459. }
  460. }
  461. }
  462. func saveTableActivityComment(entries : [String]){
  463. let dateFormatter = DateFormatter()
  464. dateFormatter.timeZone = TimeZone.ReferenceType.local
  465. //Set up context
  466. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  467. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  468. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  469. let entity = NSEntityDescription.entity(forEntityName: "Activity_comment", in: context)
  470. var row: NSManagedObject
  471. //Delete all previous entries
  472. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_comment")
  473. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  474. do {
  475. try context.execute(request)
  476. } catch let error as NSError {
  477. print("Could not clean up Activity_comment entity: \(error), \(error.userInfo)")
  478. } catch {
  479. }
  480. //Loop new entries
  481. for entry in entries{
  482. //Get id
  483. var str = entry
  484. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  485. //Get activity
  486. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  487. let activity : String = str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  488. //Get text
  489. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  490. let text : String = str.subStr(start : str.indexOf(target : "\"text\":")! + 8, end : str.indexOf(target : ",\"")! - 2)
  491. //Get dtime
  492. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  493. dateFormatter.dateFormat = "yyyy-MM-dd"
  494. let dtime = dateFormatter.date(from : str.subStr(start : str.indexOf(target : "\"dtime\":")! + 9, end : str.indexOf(target : ",\"")! - 2))!
  495. //Get username
  496. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  497. let username : String = str.subStr(start : str.indexOf(target : "\"username\":")! + 12, end : str.indexOf(target : ",\"")! - 2)
  498. //Get lang
  499. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  500. let lang : String = str.subStr(start : str.indexOf(target : "\"lang\":")! + 8, end : str.length - 1)
  501. //Save CoreData
  502. row = NSManagedObject(entity: entity!, insertInto: context)
  503. row.setValue(id, forKey: "id")
  504. row.setValue(activity, forKey: "activity")
  505. row.setValue(text, forKey: "text")
  506. row.setValue(dtime, forKey: "dtime")
  507. row.setValue(username, forKey: "username")
  508. row.setValue(lang, forKey: "lang")
  509. do {
  510. try context.save()
  511. } catch let error as NSError {
  512. print("Could not store Activity_comment with id \(id) \(error), \(error.userInfo)")
  513. } catch {
  514. }
  515. }
  516. }
  517. func saveTableActivityImage(entries : [String]){
  518. //Set up context
  519. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  520. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  521. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  522. let entity = NSEntityDescription.entity(forEntityName: "Activity_image", in: context)
  523. var row: NSManagedObject
  524. //Delete all previous entries
  525. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_image")
  526. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  527. do {
  528. try context.execute(request)
  529. } catch let error as NSError {
  530. print("Could not clean up Activity_comment entity: \(error), \(error.userInfo)")
  531. } catch {
  532. }
  533. //Loop new entries
  534. for entry in entries{
  535. //Get id
  536. var str = entry
  537. let id : Int = Int(str.subStr(start : str.indexOf(target : "\"id\":")! + 6, end : str.indexOf(target : ",\"")! - 2))!
  538. //Get activity
  539. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  540. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  541. //Get image
  542. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  543. let image : String = str.subStr(start : str.indexOf(target : "\"image\":")! + 9, end : str.indexOf(target : ",\"")! - 2)
  544. //Get idx
  545. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  546. let idx : Int = Int(str.subStr(start : str.indexOf(target : "\"idx\":")! + 7, end : str.length - 2))!
  547. //Save CoreData
  548. row = NSManagedObject(entity: entity!, insertInto: context)
  549. row.setValue(id, forKey: "id")
  550. row.setValue(activity, forKey: "activity")
  551. row.setValue(image, forKey: "image")
  552. row.setValue(idx, forKey: "idx")
  553. do {
  554. try context.save()
  555. } catch let error as NSError {
  556. print("Could not store Activity_image with id \(id) \(error), \(error.userInfo)")
  557. } catch {
  558. }
  559. }
  560. }
  561. func saveTableActivityTag(entries : [String]){
  562. //Set up context
  563. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  564. let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
  565. context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
  566. let entity = NSEntityDescription.entity(forEntityName: "Activity_tag", in: context)
  567. var row: NSManagedObject
  568. //Delete all previous entries
  569. let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Activity_tag")
  570. let request = NSBatchDeleteRequest(fetchRequest: fetch)
  571. do {
  572. try context.execute(request)
  573. } catch let error as NSError {
  574. print("Could not clean up Activity_tag entity: \(error), \(error.userInfo)")
  575. } catch {
  576. }
  577. //Loop new entries
  578. for entry in entries{
  579. //Get activity
  580. var str = entry
  581. let activity : Int = Int(str.subStr(start : str.indexOf(target : "\"activity\":")! + 12, end : str.indexOf(target : ",\"")! - 2))!
  582. //Get tag
  583. str = str.subStr(start : str.indexOf(target : ",\"")! + 1, end : str.length - 1)
  584. let tag : String = str.subStr(start : str.indexOf(target : "\"tag\":")! + 7, end : str.length - 1)
  585. //Save CoreData
  586. row = NSManagedObject(entity: entity!, insertInto: context)
  587. row.setValue(activity, forKey: "activity")
  588. row.setValue(tag, forKey: "tag")
  589. do {
  590. try context.save()
  591. } catch let error as NSError {
  592. print("Could not store Activity_tag for activity: \(activity), tag: \(tag). Error: \(error), \(error.userInfo)")
  593. } catch {
  594. }
  595. }
  596. }
  597. }