Importing Comments From flickr.com Into Pixelpost

Disclaimer

These instructions/steps worked for me running CentOS. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.


Following my previous post, about importing comments from flickr.com to my home-made photoblog, buddy Kyle thought it would be useful to have a similar API which can import comments from flickr.com into a photoblog powered by Pixelpost. Although posting the same pictures on two different platforms – once in a personal photoblog and once in flickr.com – can seem painstakingly time-consuming, it does have its advantages. For one, the latter is a multi-user platform with users ranging from novice/beginners to advanced professionals. As such, there is a better chance for attracting useful comments. As easy as it might seem to manually enter comments from flickr.com to photoblog when there are only few comments, it can become quite tedious and even more time consuming with time. To that effect, I did some Google! search to find an XML/RSS parser, modified it to meet Pixelpost-imposed requirements. The procedure/edits follow some MUST DO things for it all to work in a seamless fashion:

  1. The image names in Pixelpost database MUST BE UNIQUE. If you are wondering how to accomplish that, you may refer to one of my previous posts.
  2. Back up the MySQL database used for Pixelpost – if something goes wrong, you will have something to revert back to. Step by step instructions for doing so are here.
  3. When posting images in flickr.com, make sure the title for that post is NOTHING BUT the filename, EXCEPT the extension (jpg or gif or png or something else).

XML/RSS Parser with PHP

flickr.com generates an RSS feed for comments that others (or I) make on my photos. This RSS feed contains all the required information – name, date-time, comment, title of the image, etc. The way I designed my photoblog, the image_name is the unique identifier and when I upload the images to flickr.com, I keep the image name as part of the title. The following script – flickr2pixelpost.php, not originally written by me but parts of it heavily modified, does exactly what I want – extract the information from RSS feed and arrange it in a manner that it can be incorporated into a photoblog powered by Pixelpost (one may refer to the original script & its documentation).

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
#! /usr/bin/php
<!--?php 
# Connect to the database
$host     = "localhost";
$dbuser   = "MYSQL_PIXELPOST_USERID";
$dbpasswd = "MYSQL_PIXELPOST_PASSWD";
$database = "MYSQL_PIXELPOST_DB";
$connect  = mysql_connect($host, $dbuser, $dbpasswd) or die(mysql_error());
mysql_select_db($database,$connect) or die(mysql_error());
 
class FlickrRSSParser {
 
  # In Flickr's RSS file, all the information needed is contained in the &lt;item&gt; 
  # tags in the document. So the first global variable defined will be $insideitem, 
  # which will be set to true when entering an &lt;item&gt; tag and false when exiting one.
  var $insideitem  = false;
  var $tag         = "";
  var $title       = "";
  var $description = "";
  var $link        = "";
  var $pubdate     = "";
 
  # This function will be called by the XML parser whenever an opening tag is 
  # encountered
  # $parser will be passed a reference to the XML parser that is being used to 
  # parse the document
  # $tagName is the ALL-UPPERCASE (the PHP manual calls this 'case-folded')
  # version of the name of the opening tag that triggered the event
  # $attrs is an associative array of the attributes that are present in the tag
  # that triggered the event
  function startElement($parser, $tagName, $attrs) {
    if ($this-&gt;insideitem) {
      $this-&gt;tag = $tagName;
    } elseif ($tagName == "ITEM") {
      $this-&gt;insideitem = true;
    }
  }
 
  # $parser will be passed a reference to the XML parser that is being used to
  # parse the document
  # $tagName is the case-folded name of the closing tag that triggered the event
  function endElement($parser, $tagName) {
    if ($tagName == "ITEM") {
 
      # Image name: flickr.com displays the title as 'Comment on dsc_100-1234'
      # The filename is the 3rd array element
      $title        = htmlspecialchars(trim($this-&gt;title));
      $title        = explode(" ", $title);
      $filename     = $title[2];
 
      # Date/Time the comment was made (yyyy-mm-dd hh:mm:ss format)
      $pubdate      = htmlspecialchars(trim($this-&gt;pubdate));
      $pubdate      = strtotime($pubdate);
      $pubdate      = date("Y-m-d H:i:s", $pubdate);
 
      $paragraphs   = htmlspecialchars(trim($this-&gt;description));
      $paragraphs   = explode("&amp;lt;/p&amp;gt;", $paragraphs);
 
      # The description contains the link to comment-author's flickr profile and 
      # comment-author's name (first &lt;/p&gt;
&lt;p&gt; section)
      $paragraph0   = $paragraphs[0];
      $authorurl    = explode("&amp;quot;", $paragraph0);
      $author_url   = $authorurl[1];
 
      $authorname0  = explode("&amp;gt;", $paragraph0);
      $authorname1  = $authorname0[2];
      $authorname2  = explode("&amp;lt;", $authorname1);
      $author_name  = $authorname2[0];
 
      # The description also contains the comment-text (second &lt;/p&gt;
&lt;p&gt; section)
      # Basic substitutions are done, via ereg_replace(), to get the appropriate part
      # mysql_real_escape_string() is used to make sure comment_text is in MySQL friendly fashion
      $paragraph1   = $paragraphs[1];
      $commenttext  = explode("&amp;lt;p&amp;gt;", $paragraph1);
      $comment_text = $commenttext[1];
      $comment_text = ereg_replace("&amp;lt;br /&amp;gt;", "&lt;br&gt;\r\n", $comment_text);
      $comment_text = mysql_real_escape_string($comment_text);
 
      # The description also contains a link to image-thumbnail (second &lt;/p&gt;
&lt;p&gt; section)
      # but it's not required in this process - as such, it's ignored
 
      # flickr.com's IP address
      $flickr_ip    = gethostbyname('www.flickr.com');
 
      # Table structure for table `pixelpost_comments`
      #
      # CREATE TABLE IF NOT EXISTS `pixelpost_comments` (
      #   `id` int(11) NOT NULL auto_increment,
      #   `parent_id` int(11) NOT NULL default '0',
      #   `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
      #   `ip` varchar(20) NOT NULL default '',
      #   `message` text NOT NULL,
      #   `name` varchar(30) default NULL,
      #   `url` varchar(70) default NULL,
      #   `email` varchar(100) default NULL,
      #   `publish` char(3) NOT NULL default 'yes',
      #   `spaminess` float default NULL,
      #   `signature` varchar(55) default NULL,
      #   PRIMARY KEY  (`id`),
      #   KEY `parent_id` (`parent_id`)
      # ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
      #
      # To make sure that the comments from flickr.com are not duplicated,
      # add the following to 'pixelpost_comments' table - via command-line or
      # PHPMyAdmin.
      #
      # CREATE UNIQUE INDEX author_datetime ON `pixelpost_comments` (`name`,`datetime`);
      #

 
      # First, check if the flickr-image is in PIXELPOST database 
      # If exists, then get the corresponding POST ID (PARENT ID)
      $sql_q0       = "SELECT id, image FROM `MYSQL_DB`.`pixelpost_pixelpost` ";
      $sql_q0      .= "WHERE image='$filename.jpg' ORDER BY id DESC LIMIT 1 ";
      $result0      = mysql_query($sql_q0);
 
      # Proceed further, to make an entry into comments database, iff the
      # above query was successful
      if (!$result0) {
        die('Invalid query: ' . mysql_error());
      } else {
 
        while ($myrow = mysql_fetch_array($result0)) {
          $parent_id = $myrow['id'];
        }
 
        # Enter into the comments_table in database
        # INSERT IGNORE makes sure that duplicate entries, when exist, 
        # are ignored during insertion
        $sql_q1       = "INSERT IGNORE INTO `MYSQL_DB`.`pixelpost_comments` ";
        $sql_q1      .= "VALUES ('', '$parent_id', '$pubdate', '$flickr_ip', ";
        $sql_q1      .= "'$comment_text', '$author_name', '$author_url', ";
        $sql_q1      .= "'flickr@your-domain.com', 'yes', '', ''); ";
        $result1      = mysql_query($sql_q1);
 
        if (!$result1) {
          die('Invalid query: ' . mysql_error());
        }
 
        $this-&gt;title       = "";
        $this-&gt;description = "";
        $this-&gt;link        = "";
        $this-&gt;pubdate     = "";
        $this-&gt;insideitem  = false;
      }
    }
  }
 
  # $parser will be passed a reference to the XML parser that is being used to
  # parse the document
  # $data is a string of text appearing between XML tags in the document. 
  # The text between two tags will not necessarily trigger a single event. 
  # Blocks of text spread over multiple lines will cause one event per line, 
  # with each event being passed the $data for that line.
  function characterData($parser, $data) {
    if ($this-&gt;insideitem) {
      switch ($this-&gt;tag) {
        case "TITLE":
        $this-&gt;title .= $data;
        break;
        case "DESCRIPTION":
        $this-&gt;description .= $data;
        break;
        case "PUBDATE":
        $this-&gt;pubdate .= $data;
        break;
        case "LINK":
        $this-&gt;link .= $data;
        break;
      }
    }
  }
}
 
# Create an XML parser
# Just as one must create a database connection in PHP to interact with a database, 
# one must create an XML parser to read in an XML file. In this case, a reference to 
# the parser is stored in $xml_parser.
$xml_parser = xml_parser_create();
 
$rss_parser = new FlickrRSSParser();
 
xml_set_object($xml_parser,&amp;$rss_parser);
 
# This function specifies the functions that an XML parser should 
# use to process the events generated opening and closing tags. 
# In this case, the parser is the one stored in our $xml_parser variable, 
# while the functions are called startElement() and endElement()
 
xml_set_element_handler($xml_parser, "startElement", "endElement");
 
# This function specifies the function that the XML parser should use 
# to process character data appearing between tags in an XML document. 
# The function chosen to process character data is called characterData()
xml_set_character_data_handler($xml_parser, "characterData");
 
# Flickr's RSS Feed Comments URL must be entered here
$rss = "FLICKR_RSS_FEED_FOR_COMMENTS";
 
# Open the specified URL for reading
$fp = fopen($rss, "r")
  or die("Error reading RSS data.");
 
while ($data = fread($fp, 4096)) {
  # This function sends all or part of an XML document to the parser for it 
  # to process. The endOfDocument parameter should be set to true if the 
  # data marks the end of of XML document, or false if more of the document 
  # will follow in a subsequent call to xml_parse(). This allows the parser to 
  # correctly catch unclosed tags at the end of the document and so forth. 
  # In this case, the parser is once again $xml_parser. The $data variable 
  # (up to 4KB in size) retrieved from the file with fread() is passed as the 
  # data to be processed, while the feof() is used to determine whether 
  # PHP has reached the end of the XML file or not, thus providing the 
  # required endOfDocument parameter. If an error occurs in the parsing of 
  # the document, the error message is printed out along with the line of the 
  # file at which it occurs with xml_error_string, xml_get_error_code() and 
  # xml_get_current_line_number()
  xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xml_parser)),
      xml_get_current_line_number($xml_parser)));
}
 
fclose($fp);
 
# Although all memory resources are freed at the end of a PHP script, 
# one may wish to free up the memory used by the XML parser if the 
# script will perform other potentially memory-intensive tasks after it 
# parses the XML data. This function destroys the specified XML parser, 
# thus freeing up resources and memory it may have allocated for parsing.
xml_parser_free($xml_parser);
 
?-->


To make sure that one doesn’t miss any comments, this above script may be run via a cron-job, twice a day or so. I no longer use Pixelpost (except on a test server) and as such, cannot demonstrate it – but if someone who uses this can send me links, I will most certainly update this post with those links. As always, if you find bugs/errors, do post them as comments and I will work on them.

7 Replies to “Importing Comments From flickr.com Into Pixelpost”

  1. hi…
    I want to write a addon that will help me fetch comments from my photos at http://www.gfxartist.com
    I know the div tags that hold,
    Total number of comments,
    Comment Authors name,
    Date & Time of comment,
    And comment text itself.

    So will this code work for my needs, also i am not a coder, if i mail you the details will be able to spare a few minutes for doing it for me …

    Regards
    Rus

  2. @Rus:
    As long as http://www.gfxartist.com/ provides you a RSS Feed with all your comments, it should not be a big problem to modify my API and make it work for you. However, please note that all image names must be unique; otherwise, it may not work properly.

    Please do send me the details and I will be more than happy to take a look & help you out :)

    1. Please do send me the code and I might have some free time this weekend to take a look at it. Please do explain, in short/detail, as to what you are trying to accomplish with this code – so that I can get a better idea.

Comments are closed.