Browse Source

:loud_sound: Ajoute des logs

tags/0.1.0
Shikiryu 1 year ago
parent
commit
c078fc1c68
3 changed files with 97 additions and 6 deletions
  1. 4
    0
      deals/lbcreposter.ini.dev
  2. 63
    3
      library/Actions.php
  3. 30
    3
      library/Deal.php

+ 4
- 0
deals/lbcreposter.ini.dev View File

@@ -1,3 +1,7 @@
1
+[DEBUG]
2
+log=true
3
+verbose=1
4
+
1 5
 [CREDENTIALS]
2 6
 login=xxxxx
3 7
 password=xxxxx

+ 63
- 3
library/Actions.php View File

@@ -13,6 +13,14 @@ class Actions
13 13
     const DELETE_URL = 'https://compteperso.leboncoin.fr/store/main?cmd=adservices';
14 14
     const UPLOAD_URL = 'https://www.leboncoin.fr/ai/photo_upload_ajax/0';
15 15
 
16
+    const VERBOSE_INFO = 1;
17
+    const VERBOSE_PAGE = 2;
18
+    const VERBOSE_REQUEST = 4;
19
+    const VERBOSE_ALL = 8;
20
+
21
+    protected $debug = false;
22
+    protected $verbose = self::VERBOSE_INFO;
23
+
16 24
     /**
17 25
      * @var Account
18 26
      */
@@ -27,6 +35,39 @@ class Actions
27 35
         $this->account = $account;
28 36
     }
29 37
 
38
+    /**
39
+     * @param bool $debug
40
+     * @return Actions
41
+     */
42
+    public function setDebug($debug)
43
+    {
44
+        $this->debug = $debug;
45
+        return $this;
46
+    }
47
+
48
+    /**
49
+     * @param int $verbose
50
+     * @return Actions
51
+     */
52
+    public function setVerbose($verbose)
53
+    {
54
+        $this->verbose = $verbose;
55
+        return $this;
56
+    }
57
+
58
+    /**
59
+     * @param $action
60
+     * @param Crawler $crawler
61
+     * @return bool|int
62
+     */
63
+    private function addPageDebug($action, Crawler $crawler)
64
+    {
65
+        if ($this->debug !== false) {
66
+            return file_put_contents(sprintf('%s/%s-%s.html', $this->debug, date('YmdHi'), $action), $crawler->html());
67
+        }
68
+        return true;
69
+    }
70
+
30 71
 
31 72
     /**
32 73
      * Check if connected, if not, try to connect you
@@ -40,8 +81,10 @@ class Actions
40 81
         if (!$this->account->isConnected()) {
41 82
             // Let's connect to your account (or not)
42 83
             $crawler    = $this->account->getClient()->request('GET', self::LOGIN_URL);
84
+            $this->addPageDebug('connect', $crawler);
43 85
             $form       = $crawler->selectButton('Se connecter')->form();
44 86
             $crawler    = $this->account->getClient()->submit($form, ['st_username' => $this->account->getConfig()->login, 'st_passwd' => $this->account->getConfig()->password]);
87
+            $this->addPageDebug('checkIsConnected', $crawler);
45 88
             $this->account->setConnected($crawler->filter('.account_userinfo')->count() > 0);
46 89
         }
47 90
         return $this->account->isConnected();
@@ -57,6 +100,7 @@ class Actions
57 100
         if ($this->connect()) {
58 101
             // Let's go to our dashboard
59 102
             $crawler    = $this->account->getClient()->request('GET', self::ACCOUNT_URL);
103
+            $this->addPageDebug('retrieve', $crawler);
60 104
             // Let's list our existing deals
61 105
             $deals      = $crawler->filter('#dashboard .list .element')->each(
62 106
                 function (Crawler $node) {
@@ -87,7 +131,18 @@ class Actions
87 131
         try {
88 132
             $crawler = $this->account->getClient()->request('GET', self::ADD_URL);
89 133
             $form = $crawler->selectButton('Valider')->form();
90
-            $image0 = sprintf('%s/%s/image0.jpg', DEALS_DIR, $deal->getId());
134
+            $path = $deal->getPath();
135
+            if (empty($path)) {
136
+                $path = sprintf('%s/%s', DEALS_DIR, $deal->getId());
137
+            }
138
+            $images = [];
139
+            $i = 0;
140
+            foreach (new \DirectoryIterator($path) as $file) {
141
+                if ($file->isFile() && $file->getExtension() === 'jpg') {
142
+                    $images['image'.$i] = $file->getRealPath();
143
+                    $i++;
144
+                }
145
+            }
91 146
             $fields = $form->getPhpValues();
92 147
             $fields = array_merge(
93 148
                 $fields,
@@ -118,12 +173,15 @@ class Actions
118 173
             );
119 174
             $uri = $form->getUri();
120 175
             // It needs to be done twice !!
121
-            $this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
122
-            $crawler = $this->account->getClient()->request('POST', $uri, $fields, ['image0' => $image0]);
176
+            $crawler = $this->account->getClient()->request('POST', $uri, $fields, $images);
177
+            $this->addPageDebug('add-1', $crawler);
178
+            $crawler = $this->account->getClient()->request('POST', $uri, $fields, $images);
179
+            $this->addPageDebug('add-2', $crawler);
123 180
             // TODO need to check if we're in the good page
124 181
             // Let's validate
125 182
             $form = $crawler->selectButton('Valider mon annonce')->form();
126 183
             $crawler = $this->account->getClient()->submit($form, ['accept_rule' => 1]);
184
+            $this->addPageDebug('add-validation', $crawler);
127 185
             // TODO return if it's the validation page or not
128 186
             return true;
129 187
         } catch (\Exception $e) {
@@ -148,9 +206,11 @@ class Actions
148 206
             'continue'                                            => 'Continuer'
149 207
         ];
150 208
         $crawler    = $this->account->getClient()->request('POST', self::DELETE_URL, $fields);
209
+        $this->addPageDebug('delete', $crawler);
151 210
         // confirmation
152 211
         $form       = $crawler->selectButton('Valider')->form();
153 212
         $crawler    = $this->account->getClient()->submit($form, ['delete_reason' => '1']);
213
+        $this->addPageDebug('delete-validation', $crawler);
154 214
         // TODO return if it's the validation page or not
155 215
         return true;
156 216
     }

+ 30
- 3
library/Deal.php View File

@@ -31,6 +31,8 @@ class Deal
31 31
     protected $image2;
32 32
     /** @var \DateTime */
33 33
     protected $datecreation;
34
+    /** @var string */
35
+    protected $path;
34 36
 
35 37
     /**
36 38
      * Deal constructor.
@@ -47,10 +49,10 @@ class Deal
47 49
         $crawler    = $client->request('GET', $url);
48 50
         $deal       = new self($crawler->filter('[data-savead-id]')->attr('data-savead-id'));
49 51
         $deal->setAccount($account);
50
-        $deal->setSubject($crawler->filter('h1')->first()->text());
52
+        $deal->setSubject(trim($crawler->filter('h1')->first()->text()));
51 53
         $deal->setCategory(array_search($crawler->filter('.breadcrumbsNav >ul >li')->eq(2)->text(), Categories::$categories));
52 54
         $deal->setType(self::TYPE_OFFER);
53
-        $deal->setBody($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text());
55
+        $deal->setBody(trim($crawler->filter('.properties_description')->first()->filter('p')->eq(1)->text()));
54 56
         $deal->setPrice($crawler->filter('[itemprop=price]')->first()->attr('content'));
55 57
         $date_node = $crawler->filter('[itemprop=availabilityStarts]')->first();
56 58
         $date = \DateTime::createFromFormat('Y-m-d', $date_node->attr('content'));
@@ -254,16 +256,39 @@ class Deal
254 256
     }
255 257
 
256 258
     /**
257
-     * @param \DateTime $price
259
+     * @param array|\DateTime|string $datecreation
258 260
      * @return Deal
259 261
      */
260 262
     public function setDateCreation($datecreation)
261 263
     {
264
+        if (is_array($datecreation)) {
265
+            $datecreation = new \DateTime($datecreation['date']);
266
+        } elseif (is_string($datecreation)) {
267
+            $datecreation = new \DateTime($datecreation);
268
+        }
262 269
         $this->datecreation = $datecreation;
263 270
         return $this;
264 271
     }
265 272
 
266 273
     /**
274
+     * @return string
275
+     */
276
+    public function getPath()
277
+    {
278
+        return $this->path;
279
+    }
280
+
281
+    /**
282
+     * @param string $path
283
+     * @return Deal
284
+     */
285
+    public function setPath($path)
286
+    {
287
+        $this->path = $path;
288
+        return $this;
289
+    }
290
+
291
+    /**
267 292
      * @return Account
268 293
      */
269 294
     public function getAccount()
@@ -333,12 +358,14 @@ class Deal
333 358
      */
334 359
     public static function fromJSON($json)
335 360
     {
361
+        $path = dirname(realpath($json));
336 362
         $json = \json_decode(file_get_contents($json), true);
337 363
         $deal = new self();
338 364
         foreach ($json as $property => $value) {
339 365
             $method = sprintf('set%s', ucfirst($property));
340 366
             $deal->$method($value);
341 367
         }
368
+        $deal->setPath($path);
342 369
         return $deal;
343 370
     }
344 371
 }

Loading…
Cancel
Save