document.open();document.writeln('<div onmouseover="document.getElementById(\'snipt-4515\').style.display = \'block\'; return false;" onmouseout="document.getElementById(\'snipt-4515\').style.display = \'none\'; return false;" style="position: relative;">');document.writeln('<div class="highlight" style="background: #202020"><pre style="-moz-border-radius: 5px; border-radius: 5px; -webkit-border-radius: 5px; margin: 0; display: block; font: 11px Monaco, monospace !important; padding: 15px; background-color: #1C1C1C; overflow: auto; color: #D0D0D0;; line-height: 125%"><span style="color: #999999; font-style: italic"># -*- coding: utf-8 -*-</span>');document.writeln('<span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #ed9d13">@requirements:</span>');document.writeln('<span style="color: #ed9d13">    1. mechanize : a stateful programmatic web browsing (http://wwwsearch.sourceforge.net/mechanize/)</span>');document.writeln('<span style="color: #ed9d13">    2. libgmail : a GA reverse engineered library which uses mechanize (http://libgmail.sourceforge.net/)</span>');document.writeln('<span style="color: #ed9d13">    3. python-twitter : a wrapper around the Twitter API (http://code.google.com/p/python-twitter/)</span>');document.writeln('<span style="color: #ed9d13">@author: Radu Boncea</span>');document.writeln('<span style="color: #ed9d13">@contact: http://raduboncea.ro http://twitter.com/raduboncea</span>');document.writeln('<span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">import</span> <span style="color: #447fcf; text-decoration: underline">libgmail</span><span style="color: #d0d0d0">,</span> <span style="color: #447fcf; text-decoration: underline">re</span><span style="color: #d0d0d0">,</span> <span style="color: #447fcf; text-decoration: underline">twitter</span><span style="color: #d0d0d0">,</span> <span style="color: #447fcf; text-decoration: underline">sys</span>');document.writeln('');document.writeln('<span style="color: #d0d0d0">TWITTER_USERNAME</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;TWITTER_USERNAME&#39;</span>');document.writeln('<span style="color: #d0d0d0">TWITTER_PASSWORD</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;TWITTER_PASSWORD&#39;</span>');document.writeln('<span style="color: #d0d0d0">TWITTER_DM</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;Thanks for the following. Feel free to bookmark my blog at http://raduboncea.ro. Many tweets!&#39;</span>');document.writeln('');document.writeln('');document.writeln('<span style="color: #d0d0d0">GMAIL_USERNAME</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;GMAIL_USERNAME@gmail.com&#39;</span>');document.writeln('<span style="color: #d0d0d0">GMAIL_PASSWORD</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;GMAIL_PASSWORD&#39;</span>');document.writeln('<span style="color: #999999; font-style: italic">#you need to label your twitter follow notifications</span>');document.writeln('<span style="color: #d0d0d0">GMAIL_QUERY</span> <span style="color: #d0d0d0">=</span> <span style="color: #ed9d13">&#39;label:TwitterFollow is:unread&#39;</span>');document.writeln('');document.writeln('<span style="color: #999999; font-style: italic"># get a GA autheticated instance and trying to login</span>');document.writeln('<span style="color: #d0d0d0">ga</span> <span style="color: #d0d0d0">=</span> <span style="color: #d0d0d0">libgmail.GmailAccount(GMAIL_USERNAME,</span> <span style="color: #d0d0d0">GMAIL_PASSWORD)</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">try</span><span style="color: #d0d0d0">:</span>');document.writeln('    <span style="color: #d0d0d0">ga.login()</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">except</span> <span style="color: #d0d0d0">libgmail.GmailLoginFailure:</span>');document.writeln('    <span style="color: #6ab825; font-weight: normal">print</span> <span style="color: #ed9d13">&quot;Wrong username/password&quot;</span>');document.writeln('    <span style="color: #d0d0d0">sys.exit()</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">except</span> <span style="color: #bbbbbb">Exception</span><span style="color: #d0d0d0">:</span>');document.writeln('    <span style="color: #d0d0d0">sys.exit()</span>');document.writeln('');document.writeln('<span style="color: #999999; font-style: italic"># query and retrieve all threads</span>');document.writeln('<span style="color: #d0d0d0">threads</span> <span style="color: #d0d0d0">=</span> <span style="color: #d0d0d0">ga.getMessagesByQuery(GMAIL_QUERY,</span> <span style="color: #d0d0d0">allPages=</span><span style="color: #24909d">True</span><span style="color: #d0d0d0">)</span>');document.writeln('');document.writeln('<span style="color: #999999; font-style: italic"># get a twitter authenticated instance</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">try</span><span style="color: #d0d0d0">:</span>');document.writeln('    <span style="color: #d0d0d0">twitter=twitter.Api(TWITTER_USERNAME,TWITTER_PASSWORD)</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">except</span><span style="color: #d0d0d0">:</span>');document.writeln('    <span style="color: #d0d0d0">sys.exit()</span>');document.writeln('');document.writeln('<span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #ed9d13">iterating threads, limited to 10 not to panic GMAIL</span>');document.writeln('<span style="color: #ed9d13">REMEMBER: iterating will result in marking the thread as read</span>');document.writeln('<span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #6ab825; font-weight: normal">for</span> <span style="color: #d0d0d0">thread</span> <span style="color: #6ab825; font-weight: normal">in</span> <span style="color: #d0d0d0">threads[</span><span style="color: #3677a9">0</span><span style="color: #d0d0d0">:</span><span style="color: #3677a9">10</span><span style="color: #d0d0d0">]:</span>');document.writeln('    <span style="color: #d0d0d0">msg</span> <span style="color: #d0d0d0">=</span> <span style="color: #d0d0d0">thread[</span><span style="color: #3677a9">0</span><span style="color: #d0d0d0">]</span>');document.writeln('    <span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #ed9d13">    fetching only the first message from the GA thread</span>');document.writeln('<span style="color: #ed9d13">    getting twitter user screen_name from email extended headers</span>');document.writeln('<span style="color: #ed9d13">    &quot;&quot;&quot;</span>');document.writeln('    <span style="color: #d0d0d0">twitter_sender</span> <span style="color: #d0d0d0">=</span> <span style="color: #d0d0d0">re.findall(</span><span style="color: #ed9d13">r&quot;X-Twittersenderscreenname: (\w+)&quot;</span><span style="color: #d0d0d0">,</span> <span style="color: #d0d0d0">msg.source,re.M)[</span><span style="color: #3677a9">0</span><span style="color: #d0d0d0">].strip(</span><span style="color: #ed9d13">&#39; &#39;</span><span style="color: #d0d0d0">)</span>');document.writeln('    <span style="color: #ed9d13">&quot;&quot;&quot;</span>');document.writeln('<span style="color: #ed9d13">    try to auto-follow the user</span>');document.writeln('<span style="color: #ed9d13">    kill app in case of error as most probably we exceeded twitter rate limit and no need to continue</span>');document.writeln('<span style="color: #ed9d13">    &quot;&quot;&quot;</span>');document.writeln('    <span style="color: #6ab825; font-weight: normal">try</span><span style="color: #d0d0d0">:</span>');document.writeln('        <span style="color: #d0d0d0">twitter.CreateFriendship(twitter_sender)</span>');document.writeln('    <span style="color: #6ab825; font-weight: normal">except</span> <span style="color: #bbbbbb">Exception</span><span style="color: #d0d0d0">,e:</span>');document.writeln('        <span style="color: #999999; font-style: italic"># TODO: find a way to mark thread unred so next time script runs it will fetch it again</span>');document.writeln('        <span style="color: #999999; font-style: italic"># For the moment libgmail doesn&#39;t have a method to accomplish this</span>');document.writeln('        <span style="color: #6ab825; font-weight: normal">print</span> <span style="color: #ed9d13">&quot;Critical error occured while adding %s as friend %s&quot;</span> <span style="color: #d0d0d0">%</span> <span style="color: #d0d0d0">(</span> <span style="color: #d0d0d0">twitter_sender.encode(</span> <span style="color: #ed9d13">&#39;utf-8&#39;</span> <span style="color: #d0d0d0">),e</span> <span style="color: #d0d0d0">)</span>');document.writeln('        <span style="color: #d0d0d0">sys.exit()</span>');document.writeln('    <span style="color: #6ab825; font-weight: normal">try</span><span style="color: #d0d0d0">:</span>');document.writeln('        <span style="color: #999999; font-style: italic"># send a direct message to user to thank for the follow</span>');document.writeln('        <span style="color: #d0d0d0">twitter.PostDirectMessage(twitter_sender.encode(</span> <span style="color: #ed9d13">&#39;utf-8&#39;</span> <span style="color: #d0d0d0">),</span> <span style="color: #d0d0d0">TWITTER_DM)</span>');document.writeln('        <span style="color: #6ab825; font-weight: normal">print</span> <span style="color: #ed9d13">&quot;%s FOLLOW and DM&quot;</span> <span style="color: #d0d0d0">%</span> <span style="color: #d0d0d0">twitter_sender.encode(</span> <span style="color: #ed9d13">&#39;utf-8&#39;</span> <span style="color: #d0d0d0">)</span>');document.writeln('    <span style="color: #6ab825; font-weight: normal">except</span> <span style="color: #bbbbbb">Exception</span><span style="color: #d0d0d0">,e:</span>');document.writeln('        <span style="color: #6ab825; font-weight: normal">print</span> <span style="color: #ed9d13">&quot;Warning could not DM %s %s&quot;</span> <span style="color: #d0d0d0">%</span> <span style="color: #d0d0d0">(</span> <span style="color: #d0d0d0">twitter_sender.encode(</span> <span style="color: #ed9d13">&#39;utf-8&#39;</span> <span style="color: #d0d0d0">),e</span> <span style="color: #d0d0d0">)</span>');document.writeln('</pre></div>');document.writeln('');document.writeln('<div style="background-color: #111; color: #D0D0D0; float: right; padding: 5px 10px; -moz-border-radius-topleft: 5px; -webkit-border-top-left-radius: 5px; border-top-left-radius: 5px; -moz-border-radius-bottomright: 5px; -webkit-border-bottom-right-radius: 5px; border-bottom-right-radius: 5px; font: 11px Arial,Sans-Serif; display: none; position: absolute; bottom: 0; right: 0;" id="snipt-4515">code hosted by <a href="http://snipt.net/raduboncea/twitter-autofollow-using-libgmail-and-python-twitter" style="color: #0084FF; text-decoration: none;">snipt.net</a></div>');document.writeln('</div>');document.close();