Created
July 12, 2016 19:51
-
-
Save PifyZ/fda277bf5f12d4a97da5fe44bf580f52 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- JSON correspondant (à parser) : | |
-- { | |
-- "keywords": [ | |
-- { "uid": 6, "url": "...", "keyword": "..." }, | |
-- { "uid": 55, "url": "...", "keyword": "..." }, | |
-- { "uid": 462, "url": "...", "keyword": "..." } | |
-- ], | |
-- | |
-- "records": { | |
-- UN_TIMESTAMP: [ | |
-- { "keyword": 6, "position": 4 }, | |
-- { "keyword": 44, "position": 3 } | |
-- ], | |
-- | |
-- UN_AUTRE_TIMESTAMP: [ | |
-- { "keyword": 6, "position": 2 }, | |
-- { "keyword": 44, "position": 6 }, | |
-- { "keyword": 504, "position": 13 } | |
-- ] | |
-- } | |
-- } | |
-- | |
-- Manquant dans l'Elm : un identifiant unique pour le mot-clé, qui servira | |
-- de référence | |
type alias Keyword = | |
{ url : String | |
, keyword : String | |
} | |
type alias Json = | |
{ keywords : List Keyword | |
, records : List | |
} | |
keywordDecoder : Decoder Keyword | |
keywordDecoder = | |
object3 Keyword | |
("url" := string) | |
("keyword" := string) | |
positionDecoder : Decoder Position | |
positionDecoder = | |
object2 Position | |
("keyword" := string) | |
("" := string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Fonctionnement : | |
-- - Enregistrement de la liste des mots-clés ; | |
-- - Passage par Google, enregistre la position de chaque mot-clé ; | |
-- - Associe les positions à une date. | |
import Date exposing (Date) | |
import Array exposing (Array) | |
import Json.Decode exposing (Decoder, object2, object3, string, int, bool, (:=)) | |
-- Un mot-clé = une URL et mot-clé | |
type alias Keyword = | |
{ url : String | |
, keyword : String | |
} | |
-- Une position = un mot-clé et une position | |
type alias Position = (Keyword, Int) | |
-- Un enregistrement = une date et une liste de positions | |
type alias Record = (Date, List Position) | |
keywordDecoder : Decoder Keyword | |
keywordDecoder = | |
object3 Keyword | |
("url" := string) | |
("keyword" := string) | |
type alias PositionId = (String, Int) | |
positionDecoder : Decoder PositionId | |
positionDecoder = | |
keyValuePairs int | |
-- Liste des mots-clés | |
keywords : Array Keyword | |
keywords = Array.fromList | |
[ { url = "www.trainarriere.com", keyword = "train arrière 206" } | |
, { url = "www.trainarriere.com", keyword = "train arrière 306" } | |
, { url = "www.trainarriere.com", keyword = "essieu arrière 206" } | |
] | |
-- Récupérer un mot-clé dans la liste | |
getKeyword position keyword = | |
case Array.get position keywords of | |
Just k -> | |
k | |
Nothing -> | |
{ url = "empty.com", keyword = "-invalid" } | |
-- Liste des positions à un instant donné | |
positions1 : List Position | |
positions1 = | |
[ (getKeyword 0 keywords, 5) | |
, (getKeyword 1 keywords, 4) | |
] | |
-- Liste des positions à un instant donné | |
positions2 : List Position | |
positions2 = | |
[ (getKeyword 0 keywords, 1) | |
, (getKeyword 1 keywords, 2) | |
] | |
-- Liste des positions datées (une date => la position des mots-clés | |
-- enregistrés) | |
records : List Record | |
records = | |
[ (Date.fromTime 0, positions1) | |
, (Date.fromTime 1, positions2) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment