📜 ⬆️ ⬇️

Posting data from the script in Vkontakte to the public page


Recently, an idea occurred to me to publish weather data for a day on VKontakte, since in the summer before work I often forgot to look at the site with the weather and did not dress like the weather forecasters. In this topic there will be reasoning about the implementation of posting on VKontakte and the example with profit from what I managed to do.

Idea
It all started with an idea - at first it seemed a bit boring to me, and I didn’t see the point of implementation, but after thinking twice (or even three times) I realized that it could bring some profit to my skills. But more on that below :)

Python or PHP
First of all, it was necessary to choose what I would write on - the choice was not great and limited: python, which I have known for just a couple of months or php - with which I have been practicing various small scripts for 4 years now. About a few hours, I decided that I was not old enough for implementation in python :) Therefore, the choice was obvious - php with its simple and sometimes excessive clumsiness in my implementation (I will be grateful if you specify errors in the scripts - the link will be at the end of the article).

Authorization in Vkontakte
I, of course, did not want to reinvent the bicycle and for a long time and persistently tried to google a good class for authorization on VKontakte, but there were also some ancient scripts or classes that did not work through the contact API. The second option did not suit me for the reason that it would not be possible to post entries on behalf of the group - I thought so at the very beginning, relying on someone's old comment from the habr ... And in vain! Already after implementation, I went into the official API documentation and read what is already possible, but the bike was already written ... Now we are planning to rewrite it under the VKontakte API. The only good news is that the bike turned out to be completely unwieldy and working in the harsh realities of the current authorization implementation on the site.
Authorization and posting code:
define('SCR_DIR', dirname(__FILE__)); //   include_once(SCR_DIR . '/config.php'); //  (, ,   ) include_once(SCR_DIR . '/classes/minicurl.class.php'); //    curl,     include_once(SCR_DIR . '/classes/vk_auth.class.php'); //    $vk = new vk_auth($VKEMAIL, $VKPWD, $VKPPID, $SLEEPTIME); //    , $VKEMAIL -    , $VKPWD - , $VKPPID -   , $SLEEPTIME -      //       ,    ,   ,     /data/logfile.txt if(!$vk->check_auth()) { exit('Error! See logfile.'); } //   $message     if (!$vk->post_to_wall($message)) { exit('Error! Not Posted!'); } else { echo 'Posted!'; } 

')
Weather parser
Having finished writing the authorization, I switched to writing a weather parser - I didn’t want to use the regulars as I did before, because I would have to invent them for a very long time and, if desired, change the site where the weather comes from ... At this stage, Dmitry Rodin's blog post about data parsing through XPath helped me a lot. I have already heard about XPath a couple of times, but I never understood what it is — and it’s time to get into this convenient way of getting data from any site. After testing his class and slightly rewriting it for myself, I decided that this was exactly what I needed. All XPath paths can be easily obtained from the FireBug, but some will be incorrect, because the parser uses tidy, which clears non-valid html-code, therefore it is better to use the add-on for FireBug - FirePath.
A couple of XPath requests to parse the weather with gismeteo:
 include_once(SCR_DIR . '/classes/htmlparser.class.php'); //   $parser = new HTMLParser('http://www.gismeteo.ru/city/hourly/' . $CITY); // $CITY -   ,   $weather_temperature = $parser->getConvDataFromXPath(".//*[@id='weather']/div[1]/div/div[2]"); //          UTF-8 $weather_type = $parser->getConvDataFromXPath(".//*[@id='weather']/div[1]/div/dl/dd"); //    (, ,  ..) 


What I got from the implementation of this small script:

  1. I wrote my own small bike for authorization in VKontakte (in plans to rewrite under API);
  2. Acquainted with XPath and learned to parse sites with much faster productivity than through regulars;
  3. More closely acquainted with git;
  4. Made a small useful "service" for your city.
  5. Soon I find out whether all visitors to the contact are such vegetables, that they are not interested in useful information, but only pictures, but funny statuses;


You can see my implementation on this page: weather in Vologda .
Look at the source code or make the same public weather page for your city: github.com/saippuakauppias/vkweather

PS: if someone needs, I can make a separate fork only with the authorization class of VKontakte - write in a personal.

Source: https://habr.com/ru/post/127952/


All Articles