a330365c530ceb534951135686237df2d0a59dd8
[telefricabot.git/.git] / api.py
1
2 import requests,io,random,re
3
4 ua = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101'
5 ua += ' Firefox/60.0'
6 HEADERS = {'user-agent': ua}
7
8 class FriendApi():
9         def __init__(self,url,user,password):
10                 self.session=requests.session()
11                 r=self.session.post(url+"login/",data={'username':user,'password':password,'auth-params':'login','submit':'Acceder','openid-url':''},headers=HEADERS)
12                 if 'Login failed. Please check your credentials.' in r.text:
13                         raise ValueError('Error con la contraseƱa o usuario')
14                 print(r.json)
15                 self.url=url
16                 self.user=user
17                 self.password=password
18         def share(self,title,text):
19                 r=self.session.post(self.url.replace('login/','')+'item/',data={\
20                 'title':title\
21                 ,'body':text\
22                 ,'preview':'0'\
23                 ,'post_id':''\
24                 ,'post_id_random':str(random.randint(1,100000000000))}\
25                 ,headers=HEADERS)
26                 if '<title>Forbidden</title>' in r.text:
27                         print(r.text)
28                         print('API')
29                         raise ValueError('Error con la contraseƱa o usuario')
30                 print(r.json,r.text,'gere')
31         def notifications(self,limit):
32                 r=self.session.get(self.url+'/notifications/system',headers=HEADERS)
33                 cont=0
34                 out=''
35                 for i in re.findall('https://friendicarg.nsupdate.info/notification/.*',r.text):
36                         cont=cont+1
37                         if cont-1==limit:
38                                 break
39                         else:
40                                 out=out+'\n'+'\n'+i.replace(re.findall('"><img src=".*" aria-hidden="true" class="notif-image">',i)[0],' ').replace('<span class="notif-when">','').replace('</span></a>','')
41                 return out
42         def network(self,limit):
43                 texto=self.session.get('https://friendicarg.nsupdate.info/network').text
44                 exp=re.findall('<div class="wall-item-body e-content p-name">.*</div>',texto)
45                 exp1=re.findall('"display/.*"',texto)
46                 out=[]
47                 for i in re.findall('<a href=".*" class="tag" title=".*">',texto):
48                         texto=texto.replace(i,'')
49                 for i in re.findall('<img src=".*" alt=".*" title=".*">',texto):
50                         texto.replace(i,'')
51                 for i in range(0,limit):
52                         try:
53                                 out.append(exp[i].replace('</a>','').replace('</div>','').replace('<div class="wall-item-body e-content p-name">','').replace('<br>','')+'\n'+self.url+exp1[i].replace('"','').replace('><i class=icon-link icon-large><span class=ser-only','')+'\n\n')
54                         except:
55                                 break
56                 return out