Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

I translated the python code into javascript, but although it was successful, it does not work, why? [closed]

$
0
0

I translated the python code into javascript, but although it was successful, it does not work, why?

The python code is a bit long, but since the part I need is the request part, I only took the request part. The ones in the python part make extra proxy adjustments and there is no other feature. If you want to examine the python code in detail, you can look at https://github.com/TeaByte/telegram-views from here.

Python Code

 async def request(self, proxy: str, proxy_type: str):        if proxy_type == 'socks4': connector = ProxyConnector.from_url(f'socks4://{proxy}')        elif proxy_type == 'socks5': connector = ProxyConnector.from_url(f'socks5://{proxy}')        elif proxy_type == 'https': connector = ProxyConnector.from_url(f'https://{proxy}')        else: connector = ProxyConnector.from_url(f'http://{proxy}')        jar = aiohttp.CookieJar(unsafe=True)        async with aiohttp.ClientSession(cookie_jar=jar, connector=connector) as session:            try:                async with session.get(                    f'https://t.me/{self.channel}/{self.post}?embed=1&mode=tme',                     headers={'referer': f'https://t.me/{self.channel}/{self.post}','user-agent': user_agent                    }, timeout=aiohttp.ClientTimeout(total=5)                ) as embed_response:                    if jar.filter_cookies(embed_response.url).get('stel_ssid'):                        views_token = search('data-view="([^"]+)"', await embed_response.text())                        if views_token:                            views_response = await session.post('https://t.me/v/?views='+ views_token.group(1),                                 headers={'referer': f'https://t.me/{self.channel}/{self.post}?embed=1&mode=tme','user-agent': user_agent, 'x-requested-with': 'XMLHttpRequest'                                }, timeout=aiohttp.ClientTimeout(total=5)                            )                            if (                                await views_response.text() == "true"                                 and views_response.status == 200                            ): self.sucsess_sent += 1                            else: self.failled_sent += 1                        else: self.token_error += 1                    else: self.cookie_error += 1            except: self.proxy_error += 1            finally: jar.clear()

Javascript Code

class MyClass {  constructor(channel, post, successSent, failedSent, tokenError, cookieError, proxyError) {    this.channel = channel;    this.post = post;    this.successSent = successSent;    this.failedSent = failedSent;    this.tokenError = tokenError;    this.cookieError = cookieError;    this.proxyError = proxyError;  }  async request() {    const proxyUrl = 'socks5://user:pass@ip:port';    const agent = new SocksProxyAgent(proxyUrl);    const jar = new CookieJar();   // const client = wrapper(axios.create({ jar, withCredentials: true, httpAgent: agent, httpsAgent: agent }));   const client = axios.create({ withCredentials: true, httpAgent: agent, httpsAgent: agent })    try {      const embedResponse = await client.get(`https://t.me/${this.channel}/${this.post}?embed=1&mode=tme`, {        headers: {'referer': `https://t.me/${this.channel}/${this.post}`,'user-agent': user_agent        },        timeout: 5000      });        const viewsTokenMatch = /data-view="([^"]+)"/.exec(embedResponse.data);        if (viewsTokenMatch) {          const viewsResponse = await post(            `https://t.me/v/?views=${viewsTokenMatch[1]}`,            {              headers: {'referer': `https://t.me/${this.channel}/${this.post}?embed=1&mode=tme`,'user-agent': user_agent,'x-requested-with': 'XMLHttpRequest','cookie':embedResponse.headers['set-cookie'][0]              },              timeout: 5000            }          );          if (viewsResponse.data === true && viewsResponse.status === 200) {            console.log('OK');            this.successSent += 1;          } else {            console.log('FAIL');            this.failedSent += 1;          }        } else {            console.log('TOKEN');          this.tokenError += 1;        }    } catch (error) {        console.log(error);        console.log('PROXY');      this.proxyError += 1;    } finally {      jar.removeAllCookiesSync();    }  }}const api = new MyClass('channel_username','post_id')for (let index = 0; index < 11; index++) {    api.request();}

Viewing all articles
Browse latest Browse all 13891

Trending Articles