-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
285 lines (257 loc) · 8.74 KB
/
Main.hs
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
{-# LANGUAGE OverloadedStrings, DeriveGeneric #-}
import System.Environment
import Network.HTTP
import Network.HTTP.Base
import Network.HTTP.Headers
import Network.HTTP.Conduit
import Network.URI (parseURI)
import System.IO.Unsafe
import Data.Either
import Text.Parsec.Prim
import Text.Parsec.Token
import Text.Parsec.Combinator
import Data.Char (isLetter, isDigit)
import qualified Data.Text as T
import Data.Text.Internal
import Data.Aeson
import GHC.Generics
import Control.Monad
import Control.Applicative
import Text.Printf
import qualified Data.List.Split as S
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy as L
chopoff :: L.ByteString -> L.ByteString
chopoff str
| lenStr < lenWrp = str
| otherwise = L.init $ L.drop lenWrp str
where
lenStr = L.length str
lenWrp = L.length "jsonFlickrApi("
-- Data types
data Size = Size
{ label :: Text
, source :: Text
, url :: Text
, media :: Text
} deriving (Show, Generic)
data Sizes = Sizes
{ canblog :: Int
, canprint :: Int
, candownload :: Int
, size :: [Size]
} deriving (Show, Generic)
data PhotoSizes = PhotoSizes
{ sizes :: Sizes
, stats :: Text
} deriving (Show, Generic)
data Photo = Photo
{ photoid :: Text
, secret :: Text
, server :: Text
, farm :: Int
, title :: Text
, isprimary :: Text
, ispublic :: Int
, isfriend :: Int
, isfamily :: Int
} deriving (Show, Generic)
data Photoset = Photoset
{ photosetid :: Text
, primary :: Text
, owner :: Text
, ownername :: Text
, photo :: [Photo]
, page :: Int
, per_page :: Int
, perpage :: Int
, pages :: Int
, total :: Text
, name :: Text
} deriving (Show, Generic)
data FlickrResponse = FlickrResponse
{ photoset :: Photoset
, stat :: Text
} deriving (Show, Generic)
--
-- Data types handling
instance FromJSON Size where
parseJSON (Object v) =
Size <$> v .: "label"
<*> v .: "source"
<*> v .: "url"
<*> v .: "media"
parseJSON _ = mzero
instance ToJSON Size where
toJSON (Size label source url media) =
object [ "label" .= label
, "source" .= source
, "url" .= url
, "media" .= media
]
instance FromJSON Sizes where
parseJSON (Object v) =
Sizes <$> v .: "canblog"
<*> v .: "canprint"
<*> v .: "candownload"
<*> v .: "size"
parseJSON _ = mzero
instance ToJSON Sizes where
toJSON (Sizes canblog canprint candownload size) =
object [ "canblog" .= canblog
, "canprint" .= canprint
, "candownload" .= candownload
, "size" .= size
]
instance FromJSON PhotoSizes where
parseJSON (Object v) =
PhotoSizes <$> v .: "sizes"
<*> v .: "stat"
parseJSON _ = mzero
instance ToJSON PhotoSizes where
toJSON (PhotoSizes sizes stats) =
object [ "sizes" .= sizes
, "stat" .= stats
]
instance FromJSON Photo where
parseJSON (Object v) =
Photo <$> v .: "id"
<*> v .: "secret"
<*> v .: "server"
<*> v .: "farm"
<*> v .: "title"
<*> v .: "isprimary"
<*> v .: "ispublic"
<*> v .: "isfriend"
<*> v .: "isfamily"
parseJSON _ = mzero
instance ToJSON Photo where
toJSON (Photo photoid secret server farm title
isprimary ispublic isfriend isfamily) =
object [ "id" .= photoid
, "secret" .= secret
, "server" .= server
, "farm" .= farm
, "title" .= title
, "isprimary" .= isprimary
, "ispublic" .= ispublic
, "isfriend" .= isfriend
, "isfamily" .= isfamily
]
instance FromJSON Photoset where
parseJSON (Object o) =
Photoset <$> o .: "id"
<*> o .: "primary"
<*> o .: "owner"
<*> o .: "ownername"
<*> o .: "photo"
<*> o .: "page"
<*> o .: "per_page"
<*> o .: "perpage"
<*> o .: "pages"
<*> o .: "total"
<*> o .: "title"
parseJSON _ = mzero
instance ToJSON Photoset where
toJSON (Photoset photosetid primary owner ownername photo
page per_page perpage pages total name) =
object [ "id" .= photosetid
, "primary" .= primary
, "owner" .= owner
, "ownername" .= ownername
, "photo" .= photo
, "page" .= page
, "per_page" .= per_page
, "perpage" .= perpage
, "pages" .= pages
, "total" .= total
, "title" .= name
]
instance FromJSON FlickrResponse where
parseJSON (Object v) =
FlickrResponse <$> v .: "photoset"
<*> v .: "stat"
parseJSON _ = mzero
instance ToJSON FlickrResponse where
toJSON (FlickrResponse photoset stat) =
object [ "photoset" .= photoset
, "stat" .= stat
]
--
-- Makes a simple request to an URL (HTTPS supported)
request :: String -> L.ByteString
request url = unsafePerformIO $ simpleHttp url
-- TODO Replace `unsafePerformIO`
jsonToData :: String -> Either String FlickrResponse
jsonToData url = do
d <- eitherDecode $ chopoff $ request url :: Either String FlickrResponse
return d
getPhotos :: FlickrResponse -> [Photo]
getPhotos (FlickrResponse (Photoset _ _ _ _ photos _ _ _ _ _ _) _) = photos
--handleJsonFailure :: String -> String
handleJsonFailure msg = do
return "Failure"
--handleJsonSuccess :: FlickrResponse -> String
handleJsonSuccess k rsp = do
let photoset = getPhotos rsp
let sizesUrl = map (getPhotoSizesUrl k) photoset
let sizes = map (getPhotoSizes . T.unpack) sizesUrl
let urls = map handleSizes sizes
--downloadPhoto $ head urls
sequence_ $ map downloadPhoto urls
return "Done!"
handleSizes :: Either String PhotoSizes -> Text
handleSizes (Left err) = T.pack err
handleSizes (Right (PhotoSizes (Sizes _ _ _ sizes) _)) =
Main.source . head $ filter (\x -> Main.label x == "Original") sizes
getPhotoSizes :: String -> Either String PhotoSizes
getPhotoSizes url =
eitherDecode $ chopoff $ request url :: Either String PhotoSizes
-- Inspired by http://stackoverflow.com/a/11514868/590525
downloadPhoto :: Text -> IO ()
downloadPhoto url = do
jpg <- get $ T.unpack $ (T.replace "https" "http" url)
B.writeFile filename jpg
where
--filename = "9774244001_82ec04b2a2_s.jpg"
filename = T.unpack $ last $ T.split (== '/') url
get url = simpleHTTP (defaultGETRequest_ $ uri) >>= getResponseBody
where
uri = case parseURI url of
Nothing -> error $ "Invalid URI: " ++ url
Just u -> u
-- TODO
getPhotoSizesUrl :: String -> Photo -> Text
getPhotoSizesUrl apiKey (Photo id _ _ _ _ _ _ _ _) = T.pack $ printf "https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key=%s&photo_id=%s" apiKey (T.unpack id)
-- Given an URL from the method flickr.photos.getSizes, returns the JSON string
fetchPhotoSizes :: Text -> L.ByteString
fetchPhotoSizes url = request $ show url
-- Finds arguments and their value
findArg :: String -> [String] -> Maybe String
findArg k [] = Nothing
findArg k [_] = Nothing
findArg k (k':a:as) | k == k' = Just a
findArg k (k':a:as) = findArg k as
-- Gets an API key & the photoset ID, and returns a proper Flickr API link
getSetUrl :: String -> String -> String
getSetUrl key id = printf "https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&format=json&api_key=%s&photoset_id=%s" key id
-- Gets a proper Flickr API link and tries to download it.
download :: String -> String -> IO String
download k i = do
either (handleJsonFailure) (handleJsonSuccess k) response
where
url = getSetUrl k i
response = jsonToData url
main :: IO ()
main = do
args <- getArgs
-- Get the arguments you need: API key, API secret and the Flickr set ID
let apiKey = findArg "-k" args
let setId = findArg "-i" args
-- Make sure you have the necessary elements
case (apiKey, setId) of
(Nothing, _) -> error "No API key provided!"
(_, Nothing) -> error "No set ID provided!"
(Just k, Just i) -> download k i
return ()