User:Osamaahmed17/Outreachy-2

import pywikibot
import json
import mwparserfromhell
from pywikibot import pagegenerators
from pywikibot.data import api
import numpy as np
import requests


# You may need to enforce the use of utf-8
enwiki = pywikibot.Site('en', 'wikipedia')
enwiki_repo = enwiki.data_repository()
page = pywikibot.Page(enwiki_repo, 'User:Osamaahmed17')

#print(page.text)

def editarticle(page, mtext):
    text = page.get()
    text = text + "\n"+ mtext
    page.text = text
    try:
        page.save("Saving test edit")
        return 1
    except:
        print("That didn't work!")
        return 0

def getFileDescription(text):
    for i in mwparserfromhell.parse(text).filter_templates():
        if 'information' in i.name.lower():
            text=i.get('description').value
            return text
        
            
    
    
def printwikidata(wd_item,x):
    qid = wd_item.title()
    print(wd_item,x)
    print (qid)
    item_dict = wd_item.get()
    if(x=="P18"):  #For Images
     
        print ('Name: ' + item_dict['labels']['en'])
        for claim in item_dict['claims'][x]:
            value = claim.getTarget()
            item_dict = value.get()
            print (x+ ' value:' + value.title())  
            text= getFileDescription(item_dict)
            print(x+ ' description:') 
            print(text)

    else:
        try:
            print ('Name: ' + item_dict['labels']['en'])
            for claim in item_dict['claims'][x]:
                value = claim.getTarget()
                item_dict = value.get()
                print (x+ ' value:' + value.title())
                print (x+' label:' + item_dict['labels']['en'])
        except:
            print ("There is problem with this type, Kindly check if it exist")

            

    
print("Hello added to the end of the page")   
editarticle(page,"Hello")
print(page.text)
print("----------******----------")

print("Wikidata item (use 'Q36774') Loaded, and print out information from it")
# https://www.wikidata.org/wiki/Q4115189
page = pywikibot.ItemPage(enwiki_repo, 'Q36774')
parray=["P279","P361","P18","P5869","P1482","P1709","P1963","P460"]
for x in parray:
    printwikidata(page,x)
    print("--------------------------")