Sync.swift 28 KB

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