Code Snippet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env stack
{- stack
   --resolver lts-12.0
   --install-ghc
   script
   --ghc-options -Werror
   --ghc-options -Wall
   --package bytestring
   --package containers
   --package http-client
   --package http-client-tls
   --package tagsoup
   --
-}

--------------------------------------------------------------------------------
--
-- (c) 2019 SPISE MISU ApS, opensource.org/licenses/LGPL-3.0
--
--------------------------------------------------------------------------------

{-# LANGUAGE OverloadedStrings #-}

--------------------------------------------------------------------------------

module Main (main) where

--------------------------------------------------------------------------------

import           Control.Arrow
  ( second
  )
import qualified Data.ByteString.Lazy.Char8 as L8
import           Data.List
  ( sortOn
  )
import qualified Data.Map                   as Dict
import           Data.Maybe
  ( catMaybes
  )
import           Data.String
  ( IsString
  )
import           Network.HTTP.Client
  ( Manager
  , httpLbs
  , newManager
  , parseRequest
  , requestHeaders
  , responseBody
  )
import           Network.HTTP.Client.TLS
  ( tlsManagerSettings
  )
import           Text.HTML.TagSoup
  ( Tag (TagOpen)
  , parseTags
  , (~/=)
  )

--------------------------------------------------------------------------------

-- HTTP

headers
  :: ( IsString a, IsString b )
  => [ (a, b) ]
headers =
  [ ( "Host"
    , "saveyourinternet.eu"
    )
  , ( "User-Agent"
    , "Links (2.14; Linux x86_64; text)"
    )
  ]

get :: Manager -> String -> IO L8.ByteString
get manager url =
  do
    req <- parseRequest url
    res <- httpLbs req { requestHeaders = headers } manager
    return $ responseBody res

--------------------------------------------------------------------------------

-- PARSE

data MEP
  = Pledged
  | Against
  | Neutral
  | For
  deriving (Eq, Ord, Show)

parse :: L8.ByteString -> [ MEP ]
parse bs =
  concat $ catMaybes $ map f $ rows
  where
    tags = parseTags bs
    rows =
      takeWhile (~/= ("</tbody>" :: String)) $
      drop 1 $
      dropWhile (~/= ("<tbody>"  :: String)) tags
    f ( TagOpen "tr" xs) = Just $ catMaybes $ map (position . snd) xs
    f __________________ = Nothing
    position x =
      case x of
        "tshowcase-mepspledged" -> Just Pledged
        "tshowcase-mepsagainst" -> Just Against
        "tshowcase-mepsneutral" -> Just Neutral
        "tshowcase-mepsfor"     -> Just For
        _______________________ -> Nothing

--------------------------------------------------------------------------------

-- MISC

data EU
  = Austria
  | Belgium
  | Bulgaria
  | Croatia
  | Cyprus
  | CzechRepublic
  | Denmark
  | Estonia
  | Finland
  | France
  | Germany
  | Greece
  | Hungary
  | Ireland
  | Italy
  | Latvia
  | Lithuania
  | Luxembourg
  | Malta
  | Netherlands
  | Slovenia
  | Spain
  | Poland
  | Portugal
  | Romania
  | Slovakia
  | Sweden
  | UnitedKingdom
  | Total
  deriving (Eq, Ord, Show)

ccTLD :: EU -> String
ccTLD Austria       = "at"
ccTLD Belgium       = "be"
ccTLD Bulgaria      = "bg"
ccTLD Croatia       = "hr"
ccTLD Cyprus        = "cy"
ccTLD CzechRepublic = "cz"
ccTLD Denmark       = "dk"
ccTLD Estonia       = "ee"
ccTLD Finland       = "fi"
ccTLD France        = "fr"
ccTLD Germany       = "de"
ccTLD Greece        = "gr"
ccTLD Hungary       = "hu"
ccTLD Ireland       = "ie"
ccTLD Italy         = "it"
ccTLD Latvia        = "lv"
ccTLD Lithuania     = "lt"
ccTLD Luxembourg    = "lu"
ccTLD Malta         = "mt"
ccTLD Netherlands   = "nl"
ccTLD Poland        = "pl"
ccTLD Portugal      = "pt"
ccTLD Romania       = "ro"
ccTLD Slovakia      = "sk"
ccTLD Slovenia      = "si"
ccTLD Spain         = "es"
ccTLD Sweden        = "se"
ccTLD UnitedKingdom = "uk"
ccTLD Total         = "--"

base :: String
base =
  "https://saveyourinternet.eu/"

urls :: [ EU ] -> [ String ]
urls =
  map (base ++) . map ccTLD

groupBy
  :: Ord k
  => (a -> k)
  -> [a]
  -> [(k,[a])]
groupBy f =
  -- https://hoogle.haskell.org/?hoogle=Ord k => (a -> k) -> [a] -> [(k, [a])]
  Dict.toAscList . Dict.fromListWith (++) . map (\a -> (f a, [a]))

--------------------------------------------------------------------------------

main :: IO ()
main =
  do
    manager   <- newManager tlsManagerSettings
    responses <- mapM (get manager) $ urls $ cts
    let meps = map parse responses
    putStrLn $ "# EU MEPs with regard of Article 13 (aka `meme ban`):"
    putStrLn $ ""
    putStrLn $ replicate len '-'
    putStrLn $ "Country       | ccTLD "
      ++ " | Pledged   | Against   | Neutral   | For       | Total    "
      ++ " | Pledged % | Against % | Neutral % | For %     | Total %   |"
    putStrLn $ replicate len '-'
    mapM_ (putStrLn . ppd)
      $ sortOn snd
      $ zip cts
      $ aux
      $ meps
    putStrLn $ replicate len '-'
    mapM_ (putStrLn . ppd)
      $ [ (Total, concat $ aux $ meps) ]
    putStrLn $ replicate len '-'
    putStrLn $ ""
    putStrLn $ "Source: " ++ base ++ " and https://pledge2019.eu/"
    where
      len = 144
      ppd (c,ps) =
        cn ++ (rep $ (13 -) $ length cn) ++ " | " ++
        cc ++ (rep    04               ) ++ " | " ++
        (pad $ (09 -) $ length mp) ++ mp ++ " | " ++
        (pad $ (09 -) $ length ma) ++ ma ++ " | " ++
        (pad $ (09 -) $ length mn) ++ mn ++ " | " ++
        (pad $ (09 -) $ length mf) ++ mf ++ " | " ++
        (pad $ (09 -) $ length mt) ++ mt ++ " | " ++
        (pad $ (09 -) $ length pp) ++ pp ++ " | " ++
        (pad $ (09 -) $ length pa) ++ pa ++ " | " ++
        (pad $ (09 -) $ length pn) ++ pn ++ " | " ++
        (pad $ (09 -) $ length pf) ++ pf ++ " | " ++
        (pad $ (09 -) $ length pt) ++ pt ++ " | "
        where
          cn = show  c
          cc = ccTLD c
          p  = sum $ snd <$> filter ((== Pledged) . fst) ps
          a  = sum $ snd <$> filter ((== Against) . fst) ps
          n  = sum $ snd <$> filter ((== Neutral) . fst) ps
          f  = sum $ snd <$> filter ((== For)     . fst) ps
          t  = p + a + n + f
          mp = show  p
          ma = show  a
          mn = show  n
          mf = show  f
          mt = show  t
          t' =                      fromIntegral t
          p' = (/ t') $ (* 100.0) $ fromIntegral p
          a' = (/ t') $ (* 100.0) $ fromIntegral a
          n' = (/ t') $ (* 100.0) $ fromIntegral n
          f' = (/ t') $ (* 100.0) $ fromIntegral f
          pp = show $ dec p' 1
          pa = show $ dec a' 1
          pn = show $ dec n' 1
          pf = show $ dec f' 1
          pt = "100.0"
      dec :: Double -> Int -> Double
      dec x n = y / z
        where
          y = fromIntegral $ ((round $ x * z) :: Integer)
          z = 10 ^ n
      rep = (flip replicate) ' '
      pad = (flip replicate) ' '
      aux = map (map (second length)) . map (groupBy id)
      cts =
        [ Austria
        , Belgium
        , Bulgaria
        , Croatia
        , Cyprus
        , CzechRepublic
        , Denmark
        , Estonia
        , Finland
        , France
        , Germany
        , Greece
        , Hungary
        , Ireland
        , Italy
        , Latvia
        , Lithuania
        , Luxembourg
        , Malta
        , Netherlands
        , Slovenia
        , Spain
        , Poland
        , Portugal
        , Romania
        , Slovakia
        , Sweden
        , UnitedKingdom
        ]

Code Output:

user@personal:~/../saveyourinternet$ ./Main.hs 
Code Output saved as a picture for better visibility

References: